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
2,03 MB
Nội dung
Stacks and Queues Luong The Nhan, Tran Giang Son Chapter Stacks and Queues Data Structures and Algorithms Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Luong The Nhan, Tran Giang Son Faculty of Computer Science and Engineering University of Technology, VNU-HCM Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.1 Outcomes Stacks and Queues Luong The Nhan, Tran Giang Son • 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++ Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.2 Outcomes Stacks and Queues Luong The Nhan, Tran Giang Son • 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) Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.3 Contents Stacks and Queues Luong The Nhan, Tran Giang Son Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue Applications of Queue 5.4 Stacks and Queues Luong The Nhan, Tran Giang Son Basic operations of Stacks Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.5 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 Stacks and Queues Luong The Nhan, Tran Giang Son Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Restricted list: • • Only some operations can be used on the list Data can be inserted/deleted only at the ends of the list Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.6 Linear list concepts Stacks and Queues Luong The Nhan, Tran Giang Son Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.7 Stack Definition Stacks and Queues Luong The Nhan, Tran Giang Son 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 Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.8 Basic operations of Stacks Stacks and Queues Luong The Nhan, Tran Giang Son 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 Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.9 Basic operations of Stacks Stacks and Queues Luong The Nhan, Tran Giang Son 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 Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.10 Queue Front Stacks and Queues Luong The Nhan, Tran Giang Son template i n t Queue : : GetQueueFront ( L i s t _ I t e m T y p e &dataOut ) { i f ( c o u n t == ) return 0; dataOut = t h i s −>f r o n t −>d a t a ; return 1; } Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.79 Queue Rear Stacks and Queues Luong The Nhan, Tran Giang Son template i n t Queue : : GetQueueRear ( L i s t _ I t e m T y p e &dataOut ) { i f ( c o u n t == ) return 0; dataOut = t h i s −>r e a r −>d a t a ; return 1; } Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.80 Destroy Queue Stacks and Queues Luong The Nhan, Tran Giang Son Algorithm destroyQueue(ref queue ) Deletes all data from a queue Basic operations of Stacks Implementation of Stacks Pre: queue is a metadata structure of a valid queue Post: queue empty and all nodes recycled Linked-list implementation Array implementation Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Return nothing Array implementation Applications of Queue 5.81 Destroy Queue Stacks and Queues Luong The Nhan, Tran Giang Son 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 Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.82 Destroy Queue Stacks and Queues Luong The Nhan, Tran Giang Son 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 = ; } Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.83 Queue Empty Stacks and Queues Luong The Nhan, Tran Giang Son 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 == ) ; } Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation 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 ; } Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.84 Print Queue Stacks and Queues Luong The Nhan, Tran Giang Son 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; } Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.86 Array-based queue implementation Stacks and Queues Luong The Nhan, Tran Giang Son #i n c l u d e u s i n g namespace s t d ; c l a s s ArrayQueue { private : int capacity ; int front ; int rear ; int ∗ storage ; Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack 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; } Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.87 Array-based queue implementation Stacks and Queues Luong The Nhan, Tran Giang Son ~ArrayQueue ( ) { delete [ ] storage ; } Basic operations of v o i d enQueue ( i n t v a l u e ) { Stacks of i f ( i s F u l l ( ) ) throw s t r i n g ( " Queue ␣ i s ␣ f u l l " Implementation ); Stacks i f ( f r o n t == −1) f r o n t = ; r e a r ++; Applications of Stack storage [ rear % capacity ] = value ; Basic operations of } Linked-list implementation Array implementation Queues v o i d deQueue ( i n t &v a l u e O u t ) { i f ( isEmpty ( ) ) throw 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 ++; Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.88 Array-based queue implementation Stacks and Queues Luong The Nhan, Tran Giang Son int getFront () { i f ( isEmpty ( ) ) throw s t r i n g ( " Queue ␣ i s ␣ empty " ) ; return storage [ f r o n t % c a p a c i t y ] ; } Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation int getRear () { i f ( isEmpty ( ) ) throw s t r i n g ( " Queue ␣ i s ␣ empty " ) ; return storage [ r e a r % c a p a c i t y ] ; } Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.89 Stacks and Queues Array-based queue implementation Luong The Nhan, Tran Giang Son bool i s E m p t y ( ) { r e t u r n ( f r o n t > r e a r | | 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 + == } int getSize () { return r e a r − f r o n t + 1; } capacity ); Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation }; Applications of Queue 5.90 Using Array-based queue Stacks and Queues Luong The Nhan, Tran Giang Son i n t main ( i n t a r g c , char ∗ a r g v [ ] ) { ArrayQueue ∗myQueue = new ArrayQueue ( ) ; int val ; myQueue−>enQueue ( ) ; myQueue−>enQueue ( ) ; myQueue−>enQueue ( ) ; myQueue−>enQueue ( ) ; myQueue−>deQueue ( v a l ) ; d e l e t e myQueue ; return 1; } Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.91 Stacks and Queues Luong The Nhan, Tran Giang Son Applications of Queue Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.92 Applications of Queue Stacks and Queues Luong The Nhan, Tran Giang Son • • • • • Polynomial Arithmetic Categorizing Data Evaluate a Prefix Expression Radix Sort Queue Simulation Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Basic operations of Queues Implementation of Queue Linked-list implementation Array implementation Applications of Queue 5.93 ... 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. .. 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... 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