SlideShare uma empresa Scribd logo
1 de 48
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

Mais conteúdo relacionado

Mais procurados

Bai10 stack queue
Bai10 stack queueBai10 stack queue
Bai10 stack queueHồ Lợi
 
3 Function
3 Function3 Function
3 FunctionCuong
 
Hàm và Chuỗi
Hàm và ChuỗiHàm và Chuỗi
Hàm và Chuỗipnanhvn
 
Giao trinh bai tap c va c++
Giao trinh bai tap c va c++Giao trinh bai tap c va c++
Giao trinh bai tap c va c++Congdat Le
 
Bài tập CTDL và GT 11
Bài tập CTDL và GT 11Bài tập CTDL và GT 11
Bài tập CTDL và GT 11Hồ Lợi
 
LAP TRINH C - SESSION 2
LAP TRINH C - SESSION 2LAP TRINH C - SESSION 2
LAP TRINH C - SESSION 2pnanhvn
 
Cấp phát bộ nhớ động trong C
Cấp phát bộ nhớ động trong CCấp phát bộ nhớ động trong C
Cấp phát bộ nhớ động trong CIam Me
 
Lập trình C cơ bản cho vi điều khiển
Lập trình C cơ bản cho vi điều khiểnLập trình C cơ bản cho vi điều khiển
Lập trình C cơ bản cho vi điều khiểnMr Giap
 
Các cấu trúc lệnh trong C
Các cấu trúc lệnh trong CCác cấu trúc lệnh trong C
Các cấu trúc lệnh trong Cpnanhvn
 
Kiểu Mảng 1 chiều
Kiểu Mảng 1 chiềuKiểu Mảng 1 chiều
Kiểu Mảng 1 chiềuHoaCat1
 
Phong cach lap trinh c++
Phong cach lap trinh c++Phong cach lap trinh c++
Phong cach lap trinh c++ptquang160492
 
Huong dan su dung va debug voi dev c++
Huong dan su dung va debug voi dev c++Huong dan su dung va debug voi dev c++
Huong dan su dung va debug voi dev c++tuandong_ptit
 

Mais procurados (19)

Bai10 stack queue
Bai10 stack queueBai10 stack queue
Bai10 stack queue
 
3 Function
3 Function3 Function
3 Function
 
Pointer
PointerPointer
Pointer
 
Hàm và Chuỗi
Hàm và ChuỗiHàm và Chuỗi
Hàm và Chuỗi
 
Giao trinh bai tap c va c++
Giao trinh bai tap c va c++Giao trinh bai tap c va c++
Giao trinh bai tap c va c++
 
Bài tập CTDL và GT 11
Bài tập CTDL và GT 11Bài tập CTDL và GT 11
Bài tập CTDL và GT 11
 
9 bash
9 bash9 bash
9 bash
 
LAP TRINH C - SESSION 2
LAP TRINH C - SESSION 2LAP TRINH C - SESSION 2
LAP TRINH C - SESSION 2
 
Cấp phát bộ nhớ động trong C
Cấp phát bộ nhớ động trong CCấp phát bộ nhớ động trong C
Cấp phát bộ nhớ động trong C
 
Huong danthuchanhmang
Huong danthuchanhmangHuong danthuchanhmang
Huong danthuchanhmang
 
Lập trình C cơ bản cho vi điều khiển
Lập trình C cơ bản cho vi điều khiểnLập trình C cơ bản cho vi điều khiển
Lập trình C cơ bản cho vi điều khiển
 
Các cấu trúc lệnh trong C
Các cấu trúc lệnh trong CCác cấu trúc lệnh trong C
Các cấu trúc lệnh trong C
 
Kiểu Mảng 1 chiều
Kiểu Mảng 1 chiềuKiểu Mảng 1 chiều
Kiểu Mảng 1 chiều
 
Phong cach lap trinh c++
Phong cach lap trinh c++Phong cach lap trinh c++
Phong cach lap trinh c++
 
Lesson 2 lý thuyết
Lesson 2 lý thuyếtLesson 2 lý thuyết
Lesson 2 lý thuyết
 
Session 09
Session 09Session 09
Session 09
 
Huong dan su dung va debug voi dev c++
Huong dan su dung va debug voi dev c++Huong dan su dung va debug voi dev c++
Huong dan su dung va debug voi dev c++
 
String c++
String c++String c++
String c++
 
Session 06
Session 06Session 06
Session 06
 

Destaque (12)

Expo aves
Expo avesExpo aves
Expo aves
 
Question 1 pp
Question 1 ppQuestion 1 pp
Question 1 pp
 
Sap b1 novedades 8.81
Sap b1 novedades 8.81Sap b1 novedades 8.81
Sap b1 novedades 8.81
 
Carta descriptiva para recurso webquest
Carta descriptiva para recurso webquestCarta descriptiva para recurso webquest
Carta descriptiva para recurso webquest
 
Road book sabato 2012
Road book sabato 2012Road book sabato 2012
Road book sabato 2012
 
Rad Tech Week is November 3rd
Rad Tech Week is November 3rdRad Tech Week is November 3rd
Rad Tech Week is November 3rd
 
Powerpointpas
Powerpointpas Powerpointpas
Powerpointpas
 
Building Inspections
Building InspectionsBuilding Inspections
Building Inspections
 
Boletín#3
Boletín#3Boletín#3
Boletín#3
 
5 Hacks to Succeed in Talent Sourcing
5 Hacks to Succeed in Talent Sourcing  5 Hacks to Succeed in Talent Sourcing
5 Hacks to Succeed in Talent Sourcing
 
Apresentação bbom
Apresentação bbomApresentação bbom
Apresentação bbom
 
Aborto
AbortoAborto
Aborto
 

Semelhante a Stack &amp; queue

Ctdl C03
Ctdl C03Ctdl C03
Ctdl C03giang
 
C3 stack queue
C3 stack queueC3 stack queue
C3 stack queuehiep0109
 
Bài tập CTDL và GT 12
Bài tập CTDL và GT 12Bài tập CTDL và GT 12
Bài tập CTDL và GT 12Hồ Lợi
 
Chapter04_Array_chinhsua
Chapter04_Array_chinhsuaChapter04_Array_chinhsua
Chapter04_Array_chinhsuaThnhNguynHong5
 
C10 generic algorithms
C10 generic algorithmsC10 generic algorithms
C10 generic algorithmsHồ Lợi
 
Ctdl C06
Ctdl C06Ctdl C06
Ctdl C06giang
 
Bai5 dsachlket
Bai5 dsachlketBai5 dsachlket
Bai5 dsachlketHồ Lợi
 
Stl vector nguyen_trihai_11520094_khmt06
Stl vector nguyen_trihai_11520094_khmt06Stl vector nguyen_trihai_11520094_khmt06
Stl vector nguyen_trihai_11520094_khmt06Quach Long
 
Oop unit 09 lập trình tổng quát
Oop unit 09 lập trình tổng quátOop unit 09 lập trình tổng quát
Oop unit 09 lập trình tổng quátTráng Hà Viết
 
Lap trinh huong_doi_tuong_cpp_dhct_lesson08
Lap trinh huong_doi_tuong_cpp_dhct_lesson08Lap trinh huong_doi_tuong_cpp_dhct_lesson08
Lap trinh huong_doi_tuong_cpp_dhct_lesson08xcode_esvn
 
Lap trinh huong_doi_tuong_cpp_dhct_lesson07
Lap trinh huong_doi_tuong_cpp_dhct_lesson07Lap trinh huong_doi_tuong_cpp_dhct_lesson07
Lap trinh huong_doi_tuong_cpp_dhct_lesson07xcode_esvn
 
Java ease learning(1)
Java ease learning(1)Java ease learning(1)
Java ease learning(1)Nguyen Xuan
 
04 chuong4-capnhatdulieu-140404115156-phpapp02
04 chuong4-capnhatdulieu-140404115156-phpapp0204 chuong4-capnhatdulieu-140404115156-phpapp02
04 chuong4-capnhatdulieu-140404115156-phpapp02huynhtrong774129
 
04 chuong 4 - cap nhat du lieu
04   chuong 4 - cap nhat du lieu04   chuong 4 - cap nhat du lieu
04 chuong 4 - cap nhat du lieutruong le hung
 

Semelhante a Stack &amp; queue (20)

Ctdl C03
Ctdl C03Ctdl C03
Ctdl C03
 
C6 stack queue
C6 stack queueC6 stack queue
C6 stack queue
 
C3 stack queue
C3 stack queueC3 stack queue
C3 stack queue
 
Bài tập CTDL và GT 12
Bài tập CTDL và GT 12Bài tập CTDL và GT 12
Bài tập CTDL và GT 12
 
Chapter04_Array_chinhsua
Chapter04_Array_chinhsuaChapter04_Array_chinhsua
Chapter04_Array_chinhsua
 
C10 generic algorithms
C10 generic algorithmsC10 generic algorithms
C10 generic algorithms
 
C10 generic algorithms
C10 generic algorithmsC10 generic algorithms
C10 generic algorithms
 
Ctdl C06
Ctdl C06Ctdl C06
Ctdl C06
 
Bai5 dsachlket
Bai5 dsachlketBai5 dsachlket
Bai5 dsachlket
 
Stl vector nguyen_trihai_11520094_khmt06
Stl vector nguyen_trihai_11520094_khmt06Stl vector nguyen_trihai_11520094_khmt06
Stl vector nguyen_trihai_11520094_khmt06
 
Oop unit 09 lập trình tổng quát
Oop unit 09 lập trình tổng quátOop unit 09 lập trình tổng quát
Oop unit 09 lập trình tổng quát
 
Lap trinh huong_doi_tuong_cpp_dhct_lesson08
Lap trinh huong_doi_tuong_cpp_dhct_lesson08Lap trinh huong_doi_tuong_cpp_dhct_lesson08
Lap trinh huong_doi_tuong_cpp_dhct_lesson08
 
Lap trinh huong_doi_tuong_cpp_dhct_lesson07
Lap trinh huong_doi_tuong_cpp_dhct_lesson07Lap trinh huong_doi_tuong_cpp_dhct_lesson07
Lap trinh huong_doi_tuong_cpp_dhct_lesson07
 
DS&A Reviews
DS&A ReviewsDS&A Reviews
DS&A Reviews
 
Java ease learning(1)
Java ease learning(1)Java ease learning(1)
Java ease learning(1)
 
Chuong5 (2)
Chuong5 (2)Chuong5 (2)
Chuong5 (2)
 
Session 11
Session 11Session 11
Session 11
 
Session 11
Session 11Session 11
Session 11
 
04 chuong4-capnhatdulieu-140404115156-phpapp02
04 chuong4-capnhatdulieu-140404115156-phpapp0204 chuong4-capnhatdulieu-140404115156-phpapp02
04 chuong4-capnhatdulieu-140404115156-phpapp02
 
04 chuong 4 - cap nhat du lieu
04   chuong 4 - cap nhat du lieu04   chuong 4 - cap nhat du lieu
04 chuong 4 - cap nhat du lieu
 

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