Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 93 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
93
Dung lượng
3,03 MB
Nội dung
Data Structure and Algorithms [CO2003] Chapter - Stack and Queue Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Faculty of Computer Science and Engineering Hochiminh city University of Technology Contents Basic operations of Stacks Implementation of Stacks Applications of Stack Basic operations of Queues Implementation of Queue Applications of Queue Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 86 Outcomes • L.O.2.1 - Depict the following concepts: (a) array list and linked list, including single link and double links, and multiple links; (b) stack; and (c) queue and circular queue • L.O.2.2 - Describe storage structures by using pseudocode for: (a) array list and linked list, including single link and double links, and multiple links; (b) stack; and (c) queue and circular queue • L.O.2.3 - List necessary methods supplied for list, stack, and queue, and describe them using pseudocode • L.O.2.4 - Implement list, stack, and queue using C/C++ Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 86 Outcomes • L.O.2.5 - Use list, stack, and queue for problems in real-life, and choose an appropriate implementation type (array vs link) • L.O.2.6 - Analyze the complexity and develop experiment (program) to evaluate the efficiency of methods supplied for list, stack, and queue • L.O.8.4 - Develop recursive implementations for methods supplied for the following structures: list, tree, heap, searching, and graphs • L.O.1.2 - Analyze algorithms and use Big-O notation to characterize the computational complexity of algorithms composed by using the following control structures: sequence, branching, and iteration (not recursion) Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 86 Basic operations of Stacks Linear List Concepts General list: • No restrictions on which operation can be used on the list • No restrictions on where data can be inserted/deleted Restricted list: • Only some operations can be used on the list • Data can be inserted/deleted only at the ends of the list Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 86 Linear list concepts Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 86 Stack Definition A stack of elements of type T is a finite sequence of elements of T, in which all insertions and deletions are restricted to one end, called the top Stack is a Last In - First Out (LIFO) data structure LIFO: The last item put on the stack is the first item that can be taken off Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 86 Basic operations of Stacks Basic operations: • Construct a stack, leaving it empty • Push an element: put a new element on to the top of the stack • Pop an element: remove the top element from the top of the stack • Top an element: retrieve the top element Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 86 Basic operations of Stacks Extended operations: • Determine whether the stack is empty or not • Determine whether the stack is full or not • Find the size of the stack • Clear the stack to make it empty Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 86 Queue Front template i n t Queue : : G e t Q u e u e F r o n t ( L i s t _ I t e m T y p e &d a t a O u t ) { i f ( c o u n t == ) return 0; d a t a O u t = t h i s −>f r o n t −>d a t a ; return 1; } Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 73 / 86 Queue Rear template i n t Queue : : GetQueueRear ( L i s t _ I t e m T y p e &d a t a O u t ) { i f ( c o u n t == ) return 0; d a t a O u t = t h i s −>r e a r −>d a t a ; return 1; } Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 74 / 86 Destroy Queue Algorithm destroyQueue(ref queue ) Deletes all data from a queue Pre: queue is a metadata structure of a valid queue Post: queue empty and all nodes recycled Return nothing Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 75 / 86 Destroy Queue if queue not empty then while queue.front not null temp = queue.front queue.front = queue.front->next recycle(temp) end end queue.front = NULL queue.rear = NULL queue.count = return End destroyQueue Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 76 / 86 Destroy Queue template v o i d Queue : : C l e a r ( ) { Node∗ temp ; w h i l e ( t h i s −>f r o n t != NULL) { temp = t h i s −>f r o n t ; t h i s −>f r o n t= t h i s −>f r o n t −>n e x t ; d e l e t e temp ; } t h i s −>f r o n t = NULL ; t h i s −>r e a r = NULL ; t h i s −>c o u n t = ; } Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 77 / 86 Queue Empty template i n t Queue : : I s E m p t y ( ) { r e t u r n ( t h i s −>c o u n t == ) ; } template i n t Queue : : G e t S i z e ( ) { r e t u r n t h i s −>c o u n t ; } Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 78 / 86 Print Queue template v o i d Queue : : P r i n t C o n s o l e ( ) { Node∗ p ; p = t h i s −>f r o n t ; c o u t Enqueue ( ) ; myQueue−>Enqueue ( ) ; myQueue−>Enqueue ( ) ; myQueue−>P r i n t C o n s o l e ( ) ; myQueue−>Dequeue ( v a l ) ; myQueue−>P r i n t C o n s o l e ( ) ; d e l e t e myQueue ; return 1; } Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 80 / 86 Array-based queue implementation #i n c l u d e < s t r i n g > u s i n g namespace s t d ; c l a s s ArrayQueue { private : int capacity ; int front ; int rear ; int ∗storage ; public : ArrayQueue ( i n t c a p a c i t y ) { s t o r a g e = new i n t [ c a p a c i t y ] ; t h i s −>c a p a c i t y = c a p a c i t y ; f r o n t = −1; r e a r = −1; } Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 81 / 86 Array-based queue implementation ~ArrayQueue ( ) { delete [ ] storage ; } v o i d enQueue ( i n t v a l u e ) { i f ( i s F u l l ( ) ) t h r o w s t r i n g ( " Queue ␣ i s ␣ f u l l " ) ; i f ( f r o n t == −1) f r o n t = ; r e a r ++; storage [ rear % capacity ] = value ; } v o i d deQueue ( i n t &v a l u e O u t ) { i f ( isEmpty ( ) ) t h r o w s t r i n g ( " Queue ␣ i s ␣ empty " ) ; valueOut = storage [ f r o n t % c a p a c i t y ] ; f r o n t ++; } Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 82 / 86 Array-based queue implementation int getFront () { i f ( isEmpty ( ) ) t h r o w s t r i n g ( " Queue ␣ i s ␣ empty " ) ; return storage [ front % capacity ] ; } i n t getRear () { i f ( isEmpty ( ) ) t h r o w s t r i n g ( " Queue ␣ i s ␣ empty " ) ; return storage [ rear % capacity ] ; } Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 83 / 86 Array-based queue implementation bool isEmpty ( ) { return ( front > rear } || f r o n t == −1); bool i s F u l l () { r e t u r n ( r e a r − f r o n t + == } capacity ); int getSize () { return rear − front + 1; } }; Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 84 / 86 Using Array-based queue i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] ) { A r r a y Q u e u e ∗myQueue = new A r r a y Q u e u e ( ) ; int val ; myQueue−>enQueue ( ) ; myQueue−>enQueue ( ) ; myQueue−>enQueue ( ) ; myQueue−>enQueue ( ) ; myQueue−>deQueue ( v a l ) ; d e l e t e myQueue ; return 1; } Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 85 / 86 Applications of Queue Applications of Queue • Polynomial Arithmetic • Categorizing Data • Evaluate a Prefix Expression • Radix Sort • Queue Simulation Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 86 / 86 ... nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 14 / 86 Implementation of Stacks Linked-list implementation Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms. .. nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 23 / 86 Push data into a Linked Stack if stack full then success = false else allocate (pNew) pNew -> data = data pNew -> next = stack.top... Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 86 Linear list concepts Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] / 86