SlideShare a Scribd company logo
NỘI DUNG
 Kiểu dữ liệu Stack & Queue
 Cài đặt Stack & Queue
 Các cơ chế an toàn
 Ứng dụng thực tiễn
DANH SÁCH
 Danh sách
 Dãy hữu hạn phần tử
 Các thao tác: tạo mới, hủy, thêm, xóa, …
 Danh sách và mảng
 Ví dụ minh họa
DANH SÁCH ĐẶC,
DANH SÁCH LIÊN KẾT
 Danh sách đặc: dùng mảng cài đặt
 Danh sách liên kết: dùng cấu trúc liên kết
 So sánh về mặt sử dụng bộ nhớ
 So sánh về mặt thao tác
1 3 5 8 …
1 o 3 o 5 o 8 o
NULL
max_count = 100
1
DANH SÁCH HẠN CHẾ
 Danh sách: Thêm, xóa ở vị trí bất kỳ
 Stack: Thêm, xóa từ một đầu
 Queue: Thêm đầu này, xóa đầu kia
 Dùng List như Stack hoặc Queue?
3 7 9
5
1 3 7 9571 3 9
NGĂN XẾP
 Stack: Thêm, xóa phần tử từ một đầu (top)
 Stack là cấu trúc LIFO (Last In First Out)
HÀNG ĐỢI
 Queue: Thêm vào đầu này, lấy ra đầu kia
 Queue là cấu trúc FIFO (First In First Out)
1234
4
125 345
CÀI ĐẶT
 Danh sách đặc
 Danh sách liên kết
CÀI ĐẶT NGĂN XẾP BẰNG
DANH SÁCH ĐẶC (Stack.h)
const max_stack = 100; // kích thước tối đa
template <class Stack_Element>
// Stack_Element=int, double, string, Employee
class Stack
{
private: // định nghĩa cấu trúc dữ liệu
int count; // vị trí top, số phần tử
Stack_Entry entry[max_stack];
public: // định nghĩa các thao tác trừu tượng
Stack(); // hàm khởi tạo mặc định
bool empty() const;
bool full() const;
bool push(const Stack_Entry &item);
bool pop();
};
count
CÀI ĐẶT NGĂN XẾP BẰNG
DANH SÁCH ĐẶC (Stack.cpp)
template <class Stack_Element>
Stack<Stack_Element>::Stack()
{
count = 0;
}
bool Stack<Stack_Element>::push(const Stack_Entry &item)
{
bool outcome = true;
if full()
outcome = false;
else
entry[count++] = item;
return outcome;
}
count
count
item
count
item
item
CÀI ĐẶT NGĂN XẾP BẰNG
DANH SÁCH ĐẶC (Stack.cpp)
template <class Stack_Element>
bool Stack<Stack_Element>::pop()
{
bool outcome = true;
if empty()
outcome = false;
else
count--;
return outcome;
}
count
count
count
CÀI ĐẶT NGĂN XẾP BẰNG
DANH SÁCH ĐẶC (Stack.cpp)
template <class Stack_Element>
bool Stack<Stack_Element>::empty() const
{
return (count == 0);
}
template <class Stack_Element>
bool Stack<Stack_Element>::full() const
{
return (count == max_stack);
}
count
count=max_stack
.
.
.
CÀI ĐẶT NGĂN XẾP BẰNG
DANH SÁCH LIÊN KẾT
template <class Node_Entry>
struct Node
{
Node_Entry entry;
Node<Node_Entry> *next;
Node(); // hàm khởi tạo mặc định
Node(Node_Entry item, Node<Node_Entry> *link=NULL);
};
template <class Node_Entry>
Node<Node_Entry>::Node()
{
next = NULL;
}
template <class Node_Entry>
Node<Node_Entry>::Node(Node_Entry item, Node<Node_Entry> *link)
{
entry = item;
next = link;
}
entry
next
item entry
CÀI ĐẶT NGĂN XẾP BẰNG
DANH SÁCH LIÊN KẾT
template <class Node_Entry>
class Stack
{
private:
Node<Node_Entry> *top_node;
public:
Stack(); // hàm khởi tạo mặc định
bool empty() const;
//bool full() const;
bool push(const Node_Entry &item);
bool pop();
};
entry
CÀI ĐẶT NGĂN XẾP BẰNG
DANH SÁCH LIÊN KẾT
template <class Node_Entry>
Stack<Node_Entry>::Stack()
{
top_node = NULL;
}
template <class Node_Entry>
bool Stack<Node_Entry>::empty() const
{
return (top_node == NULL);
}
entry
CÀI ĐẶT NGĂN XẾP BẰNG
DANH SÁCH LIÊN KẾT
template <class Node_Entry>
bool Stack<Node_Entry>::push(const Node_Entry &item)
{
Node<Node_Entry> *new_top = new Node<Node_Entry>(item, top_node);
if (new_top == NULL)
return false;
top_node = new_top;
return true;
}
entry entry entryitem
CÀI ĐẶT NGĂN XẾP BẰNG
DANH SÁCH LIÊN KẾT
template <class Node_Entry>
bool Stack<Node_Entry>::pop()
{
Node<Node_Entry> *old_top = top_node;
if (empty())
return false;
top_node = old_top->next; // new top_node
delete old_top;
return true;
}
entry entry entryentry
CÀI ĐẶT HÀNG ĐỢI BẰNG
DANH SÁCH ĐẶC
 Mảng tuyến tính (linear array)
 Mảng vòng (circular array)
 Hàng đợi trong thực tế?
0 1 2 3
front
4 5 6
rear
0 1 2 3
front
4 5 6 7 8 9
rear
7 8 9
rear
rear
0
CÀI ĐẶT HÀNG ĐỢI BẰNG
DANH SÁCH ĐẶC
 Hàng đợi rỗng
 Hàng đợi đầy
front rear front
frontrear rear
CÀI ĐẶT HÀNG ĐỢI BẰNG
DANH SÁCH ĐẶC
const max_queue = 100;
template <class Entry_Element>
class Queue
{
private:
int count; // số phần tử
int front; // vị trí đầu hàng đợi
int rear; // vị trị cuối hàng đợi
Entry_Element entry[max_queue];
public:
Queue(); // hàm khởi tạo mặc định
bool empty() const;
bool full() const;
bool serve(); // dequeue
bool append(const Queue_Entry &item); // enqueue
};
item item item item
count = 4
front
rear
CÀI ĐẶT HÀNG ĐỢI BẰNG
DANH SÁCH ĐẶC
template <class Queue_Element>
Queue<Queue_Element>::Queue()
{
count = 0;
front = 0;
rear = max_queue - 1;
}
front rear
CÀI ĐẶT HÀNG ĐỢI BẰNG
DANH SÁCH ĐẶC
template <class Queue_Element>
bool Queue<Queue_Element>::empty() const
{
return (count == 0);
}
template <class Queue_Element>
bool Queue<Queue_Element>::full() const
{
return (count == max_queue);
}
front rear
CÀI ĐẶT HÀNG ĐỢI BẰNG
DANH SÁCH ĐẶC
template <class Queue_Element>
bool Queue<Queue_Element>::append(const Queue_Element &item)
{
if (full()) // queue đầy
return false;
count++;
//rear = ((rear + 1) == max_queue) ? 0 : (rear + 1);
rear = (rear + 1) % max_queue;
entry[rear] = item;
return true;
}
front rear
item
rear
item
rear rear
CÀI ĐẶT HÀNG ĐỢI BẰNG
DANH SÁCH ĐẶC
template <class Queue_Element>
bool Queue<Queue_Element>::serve()
{
if (empty())
return false;
count--;
//front = ((front + 1) == max_queue) ? 0 : (front + 1);
front = (front + 1) % max_queue;
return true;
}
front rearfront front
CÀI ĐẶT HÀNG ĐỢI BẰNG
DANH SÁCH LIÊN KẾT
template <class Node_Entry>
class Queue
{
private:
Node<Node_Entry> *front;
Node<Node_Entry> *rear;
public:
Queue(); // hàm khởi tạo mặc định
bool empty() const;
//bool full() const;
bool append(const Node_Entry &item);
bool serve();
};
Node_Entry
front rear
CÀI ĐẶT HÀNG ĐỢI BẰNG
DANH SÁCH LIÊN KẾT
template <class Node_Entry>
Queue<Node_Entry>::Queue()
{
front = NULL;
rear = NULL;
}
template <class Node_Entry>
bool Queue<Node_Entry>::empty()
{
return (front == NULL);
}
CÀI ĐẶT HÀNG ĐỢI BẰNG
DANH SÁCH LIÊN KẾTtemplate <class Node_Entry>
bool Queue<Node_Entry>::append(const Node_Entry &item)
{
Node<Node_Entry> *new_rear = new Node<Node_Entry>(item);
if (new_rear == NULL)
return false;
if (rear == NULL)
{
front = new_rear;
rear = new_rear;
}
else
{
rear->next = new_rear;
rear = new_rear;
}
}
Node_Entry
front rear
item
item
CÀI ĐẶT HÀNG ĐỢI BẰNG
DANH SÁCH LIÊN KẾT
template <class Node_Entry>
bool Queue<Node_Entry>::serve()
{
if (empty())
return false;
Node<Node_Entry> *old_front = front;
front = old_front->next;
if (front == NULL) // danh sách có 1 phần tử
rear = NULL;
delete old_front;
return true;
}
Node_Entry
Node_Entry
LỖI THƯỜNG GẶP
 ‘Không thể lấy nước từ bình rỗng’
 Con trỏ đến đối tượng đã bị hủy
 Chưa qua cầu đã rút ván
p  NULL
del
top
top
CƠ CHẾ AN TOÀN
 Tại sao cấu trúc liên kết không an toàn?
 Bộ nhớ không tự hủy
 Truyền tham biến
 Các cơ chế an toàn
 Cài đặt hàm hủy tạo destructor
 Định nghĩa chồng toán tử gán
 Cung cấp hàm khởi tạo mặc định
HÀM HỦY TẠO (DESTRUCTOR)
 Tại sao cần hàm hủy tạo?
for (int i = 0; i < 1000000; i++)
{
Stack small;
small.push(some_data);
}
some_data
top_node
some_data
some_data
some_data
some_data
HÀM HỦY TẠO (DESTRUCTOR)
Stack::~Stack()
{
while (!empty())
pop(); // hủy Node
}
TOÁN TỬ GÁN
(ASSIGNMENT OPERATOR)
Stack outer_stack;
for (int i = 0; i < 1000000; i++)
{
Stack inner_stack;
inner_stack.push(some_data);
inner_stack = outer_stack;
}
outer_stack.top_node
some_data
inner_stack.top_node
some_data some_data
TOÁN TỬ GÁN
(ASSIGNMENT OPERATOR)
void Stack::operator=(const Stack &original)
{
Node *new_top;
Node *new_copy;
Node *original_node = original.top_node;
if (original_node == NULL)
new_top = NULL;
else
{
new_top = new Node(original_node->entry);
while (original_node->next != NULL)
{
original_node = original_node->next;
new_copy->next = new Node(original_node->entry);
new_copy = new_copy->next;
}
}
TOÁN TỬ GÁN
(ASSIGNMENT OPERATOR)
original_node
new_top
new_copy
original_top
top_nodetop_node
HÀM KHỞI TẠO SAO CHÉP
(COPY CONSTRUCTOR)
 Hàm khởi tạo mặc định
 Hàm khởi tạo sao chép
 Tại sao phải dùng hàm khởi tạo sao chép?
void destroy_the_stack(Stack copy)
{
}
int main()
{
Stack vital_data;
destroy_the_stack(vital_data);
}
out_top
in_top
HÀM KHỞI TẠO SAO CHÉP
(COPY CONSTRUCTOR)
template <class Node_Entry>
Stack<Node_Entry>::Stack(const Stack<Node_Entry> &copy)
{
Node<Node_Entry> *new_copy;
Node<Node_Entry> *original_node = original.top_node;
if (original_node == NULL)
top_node = NULL;
else
{
new_copy = new Node<Node_Entry>(original_node->entry);
top_node = new_copy;
while (original_node = original_node->next != NULL)
{
original_node = original_node->next;
new_copy->next = new Node<Node_Entry>(original->entry);
new_copy = new_copy->next;
}
}
}
HÀM KHỞI TẠO SAO CHÉP
(COPY CONSTRUCTOR)
original_node
top_node
new_copy
original_top
ỨNG DỤNG THỰC TIỄN
T T TT T CPUT
Giả lặp xử lý song song: cơ chế Round Robin
ỨNG DỤNG THỰC TIỄN
BEGIN
.
.
.
CALL A
.
.
.
RETURN
END
MAIN
MAIN
BEGIN
.
.
.
CALL B
.
.
.
RETURN
END
SUB A
SUB A
BEGIN
.
.
.
.
RETURN
END
SUB B
ỨNG DỤNG THỰC TiỄN
TỔNG KẾT
 Ứng dụng đa dạng
 Hàng đợi: công việc có kể thứ tự trước
sau
 Ngăn xếp: công việc có tính chất quay
lui
THAM KHẢO
 Tìm kiếm chiều rộng
 Tìm kiếm chiều sâu
 Gọi chương trình con
 Cú pháp hậu tố
 Lỗi cài toán tử gán
 Danh sách liên kết vòng
 Danh sách liên kết kép
ỨNG DỤNG THỰC TIỄN
10 3
5
2 7 12
8 5
5
10
3
2
10 3 2 7 12
7
12
Tìm kiếm theo chiều rộng
ỨNG DỤNG THỰC TIỄN
10 3
5
2 7 12
8
5
5
10
2
7
10 32 7 12
3
12
Tìm kiếm theo chiều rộng
ỨNG DỤNG THỰC TIỄN
2 * 4 - (9 + 5) (= -6)
2 * 4 - 9 + 5 (= 4)
2 4 * 9 5 + -
8
2
44
2
8
9
5
5
9
1414
-6
Cú pháp hậu tố Ban Lan
HÀM HỦY TẠO (DESTRUCTOR)
 Tại sao cần hàm hủy tạo?
 Xét ví dụ:
for (int i = 0; i < 1000000; i++)
{
Stack small;
small.push(some_data);
}
some_data
top_node
some_data
some_data
some_data
some_data
HÀM KHỞI TẠO SAO CHÉP
(COPY CONSTRUCTOR)
top_node
original_top
x  x
LIÊN KẾT KÉP, LIÊN KẾT VÒNG
min max
List

More Related Content

PDF
PPT
Ctdl C02
PPTX
Bai giang stack c++
PPT
Ctdl C04
PDF
02 stack queue
PDF
C đến C++ phần 1
PDF
Control structure in C
PDF
Basic C programming
Ctdl C02
Bai giang stack c++
Ctdl C04
02 stack queue
C đến C++ phần 1
Control structure in C
Basic C programming

What's hot (18)

PDF
Bai10 stack queue
PPT
3 Function
PPTX
Pointer
PDF
Hàm và Chuỗi
PDF
Giao trinh bai tap c va c++
DOC
Bài tập CTDL và GT 11
PDF
9 bash
PDF
LAP TRINH C - SESSION 2
PDF
Cấp phát bộ nhớ động trong C
DOCX
Huong danthuchanhmang
PDF
Lập trình C cơ bản cho vi điều khiển
PDF
Các cấu trúc lệnh trong C
PPT
Kiểu Mảng 1 chiều
PDF
Phong cach lap trinh c++
PPT
Session 09
PDF
Huong dan su dung va debug voi dev c++
PDF
String c++
PPT
Session 06
Bai10 stack queue
3 Function
Pointer
Hàm và Chuỗi
Giao trinh bai tap c va c++
Bài tập CTDL và GT 11
9 bash
LAP TRINH C - SESSION 2
Cấp phát bộ nhớ động trong C
Huong danthuchanhmang
Lập trình C cơ bản cho vi điều khiển
Các cấu trúc lệnh trong C
Kiểu Mảng 1 chiều
Phong cach lap trinh c++
Session 09
Huong dan su dung va debug voi dev c++
String c++
Session 06
Ad

Viewers also liked (12)

PPTX
PPTX
Question 1 pp
PDF
Sap b1 novedades 8.81
DOCX
Carta descriptiva para recurso webquest
PDF
Road book sabato 2012
DOCX
Rad Tech Week is November 3rd
PPTX
Powerpointpas
PDF
Building Inspections
PPTX
5 Hacks to Succeed in Talent Sourcing
PDF
Apresentação bbom
Question 1 pp
Sap b1 novedades 8.81
Carta descriptiva para recurso webquest
Road book sabato 2012
Rad Tech Week is November 3rd
Powerpointpas
Building Inspections
5 Hacks to Succeed in Talent Sourcing
Apresentação bbom
Ad

Similar to Stack &amp; queue (20)

PPT
Ctdl C03
PPTX
CauTrucDuLieu_BaiGiang5_Stack_Queue.pptx
PDF
C6 stack queue
PPT
C3 stack queue
PPT
bjjjkkjhjkhkjhjhkjgkjghfghjgklhukhuk.ppt
DOC
Bài tập CTDL và GT 12
PPTX
Giao trinh Stack va Queue trong DSA.pptx
PPT
Chapter04_Array_chinhsua
PDF
w8-StackQueue.pdfgfgdgfffđfđgfgdgfgdfgdgdf
PDF
C10 generic algorithms
PDF
C10 generic algorithms
PPT
Ctdl C06
PDF
Bai5 dsachlket
DOCX
Stl vector nguyen_trihai_11520094_khmt06
PDF
Oop unit 09 lập trình tổng quát
PPT
Lap trinh huong_doi_tuong_cpp_dhct_lesson08
PPTX
Project - Ham - Chuong trinh con trong C++ v2.pptx
PPT
Lap trinh huong_doi_tuong_cpp_dhct_lesson07
PDF
PDF
Java ease learning(1)
Ctdl C03
CauTrucDuLieu_BaiGiang5_Stack_Queue.pptx
C6 stack queue
C3 stack queue
bjjjkkjhjkhkjhjhkjgkjghfghjgklhukhuk.ppt
Bài tập CTDL và GT 12
Giao trinh Stack va Queue trong DSA.pptx
Chapter04_Array_chinhsua
w8-StackQueue.pdfgfgdgfffđfđgfgdgfgdfgdgdf
C10 generic algorithms
C10 generic algorithms
Ctdl C06
Bai5 dsachlket
Stl vector nguyen_trihai_11520094_khmt06
Oop unit 09 lập trình tổng quát
Lap trinh huong_doi_tuong_cpp_dhct_lesson08
Project - Ham - Chuong trinh con trong C++ v2.pptx
Lap trinh huong_doi_tuong_cpp_dhct_lesson07
Java ease learning(1)

Stack &amp; queue

  • 1. NỘI DUNG  Kiểu dữ liệu Stack & Queue  Cài đặt Stack & Queue  Các cơ chế an toàn  Ứng dụng thực tiễn
  • 2. DANH SÁCH  Danh sách  Dãy hữu hạn phần tử  Các thao tác: tạo mới, hủy, thêm, xóa, …  Danh sách và mảng  Ví dụ minh họa
  • 3. DANH SÁCH ĐẶC, DANH SÁCH LIÊN KẾT  Danh sách đặc: dùng mảng cài đặt  Danh sách liên kết: dùng cấu trúc liên kết  So sánh về mặt sử dụng bộ nhớ  So sánh về mặt thao tác 1 3 5 8 … 1 o 3 o 5 o 8 o NULL max_count = 100
  • 4. 1 DANH SÁCH HẠN CHẾ  Danh sách: Thêm, xóa ở vị trí bất kỳ  Stack: Thêm, xóa từ một đầu  Queue: Thêm đầu này, xóa đầu kia  Dùng List như Stack hoặc Queue? 3 7 9 5 1 3 7 9571 3 9
  • 5. NGĂN XẾP  Stack: Thêm, xóa phần tử từ một đầu (top)  Stack là cấu trúc LIFO (Last In First Out)
  • 6. HÀNG ĐỢI  Queue: Thêm vào đầu này, lấy ra đầu kia  Queue là cấu trúc FIFO (First In First Out) 1234 4 125 345
  • 7. CÀI ĐẶT  Danh sách đặc  Danh sách liên kết
  • 8. CÀI ĐẶT NGĂN XẾP BẰNG DANH SÁCH ĐẶC (Stack.h) const max_stack = 100; // kích thước tối đa template <class Stack_Element> // Stack_Element=int, double, string, Employee class Stack { private: // định nghĩa cấu trúc dữ liệu int count; // vị trí top, số phần tử Stack_Entry entry[max_stack]; public: // định nghĩa các thao tác trừu tượng Stack(); // hàm khởi tạo mặc định bool empty() const; bool full() const; bool push(const Stack_Entry &item); bool pop(); }; count
  • 9. CÀI ĐẶT NGĂN XẾP BẰNG DANH SÁCH ĐẶC (Stack.cpp) template <class Stack_Element> Stack<Stack_Element>::Stack() { count = 0; } bool Stack<Stack_Element>::push(const Stack_Entry &item) { bool outcome = true; if full() outcome = false; else entry[count++] = item; return outcome; } count count item count item item
  • 10. CÀI ĐẶT NGĂN XẾP BẰNG DANH SÁCH ĐẶC (Stack.cpp) template <class Stack_Element> bool Stack<Stack_Element>::pop() { bool outcome = true; if empty() outcome = false; else count--; return outcome; } count count count
  • 11. CÀI ĐẶT NGĂN XẾP BẰNG DANH SÁCH ĐẶC (Stack.cpp) template <class Stack_Element> bool Stack<Stack_Element>::empty() const { return (count == 0); } template <class Stack_Element> bool Stack<Stack_Element>::full() const { return (count == max_stack); } count count=max_stack . . .
  • 12. CÀI ĐẶT NGĂN XẾP BẰNG DANH SÁCH LIÊN KẾT template <class Node_Entry> struct Node { Node_Entry entry; Node<Node_Entry> *next; Node(); // hàm khởi tạo mặc định Node(Node_Entry item, Node<Node_Entry> *link=NULL); }; template <class Node_Entry> Node<Node_Entry>::Node() { next = NULL; } template <class Node_Entry> Node<Node_Entry>::Node(Node_Entry item, Node<Node_Entry> *link) { entry = item; next = link; } entry next item entry
  • 13. CÀI ĐẶT NGĂN XẾP BẰNG DANH SÁCH LIÊN KẾT template <class Node_Entry> class Stack { private: Node<Node_Entry> *top_node; public: Stack(); // hàm khởi tạo mặc định bool empty() const; //bool full() const; bool push(const Node_Entry &item); bool pop(); }; entry
  • 14. CÀI ĐẶT NGĂN XẾP BẰNG DANH SÁCH LIÊN KẾT template <class Node_Entry> Stack<Node_Entry>::Stack() { top_node = NULL; } template <class Node_Entry> bool Stack<Node_Entry>::empty() const { return (top_node == NULL); } entry
  • 15. CÀI ĐẶT NGĂN XẾP BẰNG DANH SÁCH LIÊN KẾT template <class Node_Entry> bool Stack<Node_Entry>::push(const Node_Entry &item) { Node<Node_Entry> *new_top = new Node<Node_Entry>(item, top_node); if (new_top == NULL) return false; top_node = new_top; return true; } entry entry entryitem
  • 16. CÀI ĐẶT NGĂN XẾP BẰNG DANH SÁCH LIÊN KẾT template <class Node_Entry> bool Stack<Node_Entry>::pop() { Node<Node_Entry> *old_top = top_node; if (empty()) return false; top_node = old_top->next; // new top_node delete old_top; return true; } entry entry entryentry
  • 17. CÀI ĐẶT HÀNG ĐỢI BẰNG DANH SÁCH ĐẶC  Mảng tuyến tính (linear array)  Mảng vòng (circular array)  Hàng đợi trong thực tế? 0 1 2 3 front 4 5 6 rear 0 1 2 3 front 4 5 6 7 8 9 rear 7 8 9 rear rear 0
  • 18. CÀI ĐẶT HÀNG ĐỢI BẰNG DANH SÁCH ĐẶC  Hàng đợi rỗng  Hàng đợi đầy front rear front frontrear rear
  • 19. CÀI ĐẶT HÀNG ĐỢI BẰNG DANH SÁCH ĐẶC const max_queue = 100; template <class Entry_Element> class Queue { private: int count; // số phần tử int front; // vị trí đầu hàng đợi int rear; // vị trị cuối hàng đợi Entry_Element entry[max_queue]; public: Queue(); // hàm khởi tạo mặc định bool empty() const; bool full() const; bool serve(); // dequeue bool append(const Queue_Entry &item); // enqueue }; item item item item count = 4 front rear
  • 20. CÀI ĐẶT HÀNG ĐỢI BẰNG DANH SÁCH ĐẶC template <class Queue_Element> Queue<Queue_Element>::Queue() { count = 0; front = 0; rear = max_queue - 1; } front rear
  • 21. CÀI ĐẶT HÀNG ĐỢI BẰNG DANH SÁCH ĐẶC template <class Queue_Element> bool Queue<Queue_Element>::empty() const { return (count == 0); } template <class Queue_Element> bool Queue<Queue_Element>::full() const { return (count == max_queue); } front rear
  • 22. CÀI ĐẶT HÀNG ĐỢI BẰNG DANH SÁCH ĐẶC template <class Queue_Element> bool Queue<Queue_Element>::append(const Queue_Element &item) { if (full()) // queue đầy return false; count++; //rear = ((rear + 1) == max_queue) ? 0 : (rear + 1); rear = (rear + 1) % max_queue; entry[rear] = item; return true; } front rear item rear item rear rear
  • 23. CÀI ĐẶT HÀNG ĐỢI BẰNG DANH SÁCH ĐẶC template <class Queue_Element> bool Queue<Queue_Element>::serve() { if (empty()) return false; count--; //front = ((front + 1) == max_queue) ? 0 : (front + 1); front = (front + 1) % max_queue; return true; } front rearfront front
  • 24. CÀI ĐẶT HÀNG ĐỢI BẰNG DANH SÁCH LIÊN KẾT template <class Node_Entry> class Queue { private: Node<Node_Entry> *front; Node<Node_Entry> *rear; public: Queue(); // hàm khởi tạo mặc định bool empty() const; //bool full() const; bool append(const Node_Entry &item); bool serve(); }; Node_Entry front rear
  • 25. CÀI ĐẶT HÀNG ĐỢI BẰNG DANH SÁCH LIÊN KẾT template <class Node_Entry> Queue<Node_Entry>::Queue() { front = NULL; rear = NULL; } template <class Node_Entry> bool Queue<Node_Entry>::empty() { return (front == NULL); }
  • 26. CÀI ĐẶT HÀNG ĐỢI BẰNG DANH SÁCH LIÊN KẾTtemplate <class Node_Entry> bool Queue<Node_Entry>::append(const Node_Entry &item) { Node<Node_Entry> *new_rear = new Node<Node_Entry>(item); if (new_rear == NULL) return false; if (rear == NULL) { front = new_rear; rear = new_rear; } else { rear->next = new_rear; rear = new_rear; } } Node_Entry front rear item item
  • 27. CÀI ĐẶT HÀNG ĐỢI BẰNG DANH SÁCH LIÊN KẾT template <class Node_Entry> bool Queue<Node_Entry>::serve() { if (empty()) return false; Node<Node_Entry> *old_front = front; front = old_front->next; if (front == NULL) // danh sách có 1 phần tử rear = NULL; delete old_front; return true; } Node_Entry Node_Entry
  • 28. LỖI THƯỜNG GẶP  ‘Không thể lấy nước từ bình rỗng’  Con trỏ đến đối tượng đã bị hủy  Chưa qua cầu đã rút ván p  NULL del top top
  • 29. CƠ CHẾ AN TOÀN  Tại sao cấu trúc liên kết không an toàn?  Bộ nhớ không tự hủy  Truyền tham biến  Các cơ chế an toàn  Cài đặt hàm hủy tạo destructor  Định nghĩa chồng toán tử gán  Cung cấp hàm khởi tạo mặc định
  • 30. HÀM HỦY TẠO (DESTRUCTOR)  Tại sao cần hàm hủy tạo? for (int i = 0; i < 1000000; i++) { Stack small; small.push(some_data); } some_data top_node some_data some_data some_data some_data
  • 31. HÀM HỦY TẠO (DESTRUCTOR) Stack::~Stack() { while (!empty()) pop(); // hủy Node }
  • 32. TOÁN TỬ GÁN (ASSIGNMENT OPERATOR) Stack outer_stack; for (int i = 0; i < 1000000; i++) { Stack inner_stack; inner_stack.push(some_data); inner_stack = outer_stack; } outer_stack.top_node some_data inner_stack.top_node some_data some_data
  • 33. TOÁN TỬ GÁN (ASSIGNMENT OPERATOR) void Stack::operator=(const Stack &original) { Node *new_top; Node *new_copy; Node *original_node = original.top_node; if (original_node == NULL) new_top = NULL; else { new_top = new Node(original_node->entry); while (original_node->next != NULL) { original_node = original_node->next; new_copy->next = new Node(original_node->entry); new_copy = new_copy->next; } }
  • 34. TOÁN TỬ GÁN (ASSIGNMENT OPERATOR) original_node new_top new_copy original_top top_nodetop_node
  • 35. HÀM KHỞI TẠO SAO CHÉP (COPY CONSTRUCTOR)  Hàm khởi tạo mặc định  Hàm khởi tạo sao chép  Tại sao phải dùng hàm khởi tạo sao chép? void destroy_the_stack(Stack copy) { } int main() { Stack vital_data; destroy_the_stack(vital_data); } out_top in_top
  • 36. HÀM KHỞI TẠO SAO CHÉP (COPY CONSTRUCTOR) template <class Node_Entry> Stack<Node_Entry>::Stack(const Stack<Node_Entry> &copy) { Node<Node_Entry> *new_copy; Node<Node_Entry> *original_node = original.top_node; if (original_node == NULL) top_node = NULL; else { new_copy = new Node<Node_Entry>(original_node->entry); top_node = new_copy; while (original_node = original_node->next != NULL) { original_node = original_node->next; new_copy->next = new Node<Node_Entry>(original->entry); new_copy = new_copy->next; } } }
  • 37. HÀM KHỞI TẠO SAO CHÉP (COPY CONSTRUCTOR) original_node top_node new_copy original_top
  • 38. ỨNG DỤNG THỰC TIỄN T T TT T CPUT Giả lặp xử lý song song: cơ chế Round Robin
  • 39. ỨNG DỤNG THỰC TIỄN BEGIN . . . CALL A . . . RETURN END MAIN MAIN BEGIN . . . CALL B . . . RETURN END SUB A SUB A BEGIN . . . . RETURN END SUB B
  • 41. TỔNG KẾT  Ứng dụng đa dạng  Hàng đợi: công việc có kể thứ tự trước sau  Ngăn xếp: công việc có tính chất quay lui
  • 42. THAM KHẢO  Tìm kiếm chiều rộng  Tìm kiếm chiều sâu  Gọi chương trình con  Cú pháp hậu tố  Lỗi cài toán tử gán  Danh sách liên kết vòng  Danh sách liên kết kép
  • 43. ỨNG DỤNG THỰC TIỄN 10 3 5 2 7 12 8 5 5 10 3 2 10 3 2 7 12 7 12 Tìm kiếm theo chiều rộng
  • 44. ỨNG DỤNG THỰC TIỄN 10 3 5 2 7 12 8 5 5 10 2 7 10 32 7 12 3 12 Tìm kiếm theo chiều rộng
  • 45. ỨNG DỤNG THỰC TIỄN 2 * 4 - (9 + 5) (= -6) 2 * 4 - 9 + 5 (= 4) 2 4 * 9 5 + - 8 2 44 2 8 9 5 5 9 1414 -6 Cú pháp hậu tố Ban Lan
  • 46. HÀM HỦY TẠO (DESTRUCTOR)  Tại sao cần hàm hủy tạo?  Xét ví dụ: for (int i = 0; i < 1000000; i++) { Stack small; small.push(some_data); } some_data top_node some_data some_data some_data some_data
  • 47. HÀM KHỞI TẠO SAO CHÉP (COPY CONSTRUCTOR) top_node original_top x  x
  • 48. LIÊN KẾT KÉP, LIÊN KẾT VÒNG min max List