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 MB
Nội dung
Stacks and Queues Dr Nguyen Ho Man Rang Chapter Stacks and Queues Data Structures and Algorithms Basic operations of Stacks Implementation of Stacks Linked-list implementation Array implementation Applications of Stack Dr Nguyen Ho Man Rang 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 6.1 Outcomes Stacks and Queues Dr Nguyen Ho Man Rang • 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 6.2 Outcomes Stacks and Queues Dr Nguyen Ho Man Rang • 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 6.3 Contents Stacks and Queues Dr Nguyen Ho Man Rang 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 6.4 Stacks and Queues Dr Nguyen Ho Man Rang 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 6.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 Dr Nguyen Ho Man Rang 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 6.6 Linear list concepts Stacks and Queues Dr Nguyen Ho Man Rang 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 6.7 Stack Definition Stacks and Queues Dr Nguyen Ho Man Rang A stack of elements of type T is a finite, ordered 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 6.8 Basic operations of Stacks Stacks and Queues Dr Nguyen Ho Man Rang 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 6.9 Basic operations of Stacks Stacks and Queues Dr Nguyen Ho Man Rang 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 6.10 Queue Front Stacks and Queues Dr Nguyen Ho Man Rang 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 6.79 Queue Rear Stacks and Queues Dr Nguyen Ho Man Rang 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 6.80 Destroy Queue Stacks and Queues Dr Nguyen Ho Man Rang 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 6.81 Destroy Queue Stacks and Queues Dr Nguyen Ho Man Rang 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 6.82 Destroy Queue Stacks and Queues Dr Nguyen Ho Man Rang 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 6.83 Queue Empty Stacks and Queues Dr Nguyen Ho Man Rang 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 6.84 Print Queue Stacks and Queues Dr Nguyen Ho Man Rang 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 6.86 Array-based queue implementation Stacks and Queues Dr Nguyen Ho Man Rang #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 6.87 Array-based queue implementation Stacks and Queues Dr Nguyen Ho Man Rang ~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 6.88 Array-based queue implementation Stacks and Queues Dr Nguyen Ho Man Rang 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 6.89 Array-based queue implementation Stacks and Queues Dr Nguyen Ho Man Rang 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 + == capacity ); } int getSize () { return r e a r − f r o n t + 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 6.90 Using Array-based queue Stacks and Queues Dr Nguyen Ho Man Rang 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 6.91 Stacks and Queues Dr Nguyen Ho Man Rang 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 6.92 Applications of Queue Stacks and Queues Dr Nguyen Ho Man Rang • • • • • 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 6.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