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
1,75 MB
Nội dung
DataStructureandAlgorithms [CO2003] Chapter - StackandQueue Lecturer: Duc Dung Nguyen, PhD Contact: nddung@hcmut.edu.vn September 19, 2016 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 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) queueand 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) queueand 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, andqueue using C/C++ Outcomes • L.O.2.5 - Use list, stack, andqueue 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, andqueue • 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 algorithmsand 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 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 Linear list concepts 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) datastructure LIFO: The last item put on the stack is the first item that can be taken off 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 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 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 &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; } 73 Queue Rear 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; } 74 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 75 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 76 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 = ; } 77 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 ; } 78 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; } 80 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; } 81 Array-based queue implementation ~A r r a y Q u e u e ( ) { delete [ ] storage ; } v o i d enQueue ( i n t v a l u e ) { i f ( i s F u l l ( ) ) throw 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 ( ) ) 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 ++; } 82 Array-based queue implementation int getFront () { i f ( isEmpty ( ) ) throw s t r i n g ( " Queue ␣ i s ␣ empty " ) ; return storage [ front % capacity ] ; } i n t getRear () { i f ( isEmpty ( ) ) throw s t r i n g ( " Queue ␣ i s ␣ empty " ) ; return storage [ rear % capacity ] ; } 83 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; } }; 84 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; } 85 Applications of Queue Applications of Queue • Polynomial Arithmetic • Categorizing Data • Evaluate a Prefix Expression • Radix Sort • Queue Simulation 86