Data Structure and Algorithms [CO2003] Chapter 5 - Stack and Queue Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn Faculty of Computer Science and Engineering Hochiminh city University of Technology Contents 1. Basic operations of Stacks 2. Implementation of Stacks 3.
Applications of Stack 4. Basic operations of Queues 5. Implementation of Queue a. Applications of Queue Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 1/86 L.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.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.3 - List necessary methods supplied for list, stack, and queue, and describe them using pseudocode.4 - Implement list, stack, and queue using C/C++. Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 2/86 L.5 - Use list, stack, and queue for problems in real-life, and choose an appropriate implementation type (array vs.6 - Analyze the complexity and develop experiment (program) to evaluate the efficiency of methods supplied for list, stack, and queue.4 - Develop recursive implementations for methods supplied for the following structures: list, tree, heap, searching, and graphs.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@hemut.vn] Data Structure and Algorithms [CO2003] 3/86 Basic operations of Stacks General list: e No restrictions on which operation can be used on the list. e No restrictions on where data can be inserted/deleted. Restricted list: e Only some operations can be used on the list. e Data can be inserted/deleted only at the ends of the list.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 4/86 Linear list concepts Linear lists Restricted | | FIFO LIFO Unordered§ | Ordered (queue) (stack) Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 5/86 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@hemut.vn] Data Structure and Algorithms [CO2003] 6/86 asic operations of Stacks Basic operations: e Construct a stack, leaving it empty. e Push an element: put a new element on to the top of the stack.
e Pop an element: remove the top element from the top of the stack. e Top an element: retrieve the top element. Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 7/86 Basic operations of Stacks Extended operations: e Determine whether the stack is empty or not.
e Determine whether the stack is full or not. e Find the size of the stack. e Clear the stack to make it empty. Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 8/86 Basic operations of Stacks: Push Push Data | [SS | Top pi | mm fC] E—] Cd Stack Stack Figure 1: Successful Push operation Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 9/86 Basic operations of Stacks: Push Overflow Data Stack Figure 2: Unsuccessful Push operation. Stack remains unchanged. Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 10 / 86 Basic operations of Stacks: Pop ES ong Top | E— ——> LÍ Top CO Cd Stack Stack Figure 3: Successful Pop operation Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 11/86 ic operations of Stacks: Pop Underflow Top Stack Figure 4: Unsuccessful Pop operation. Stack remains unchanged. Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 12/86 Basic operations of Stacks: Top Siack Top Eee Top | L—— 1 Top Crs |C— tL] Lo] Stack Stack Figure 5: Successful Top operation. Stack remains unchanged. rere Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn| Data Structure and Algorithms [CO2003] 13 / 86 ic operations of Stacks: Top Underflow Top Stack Figure 6: Unsuccessful Top operation.
Stack remains unchanged. Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 14/86 Implementation of Stacks Linked-list implementation Stack structure top Top|L —_] L5 lMƒE>_ 1= «= JUL Conceptual Physical rer: Duc Dung Nguyen, PhD.vn| Data Structure and Algorithms [CO2003] 15 / 86 Linked-list implementation Stack structure count top Stack node structure data next Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] stack count <integer> top <node pointer> end stack node data <dataType> next <node pointer> end node Data Structure and Algorithms [CO2003] 16 / 86 Linked-list implementation in C++ template <class ltemType> struct Node { ItemType data; Node<ItemType> *next; ti Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 17 / 86 Linked-list implementation in C++ template <class List ltemType> class Stack { public: Stack (); ~Stack(); void Push(List ItemType dataln); int Pop(List_ ItemType &dataOut ); int GetStackTop(List_ItemType &dataOut); void Clear(); int IsEmpty(); int GetSize(); Stack<List_ItemType>* Clone(); void Print2Console(); private: Node<List_ItemType>* top; int count; ti Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 18 / 86 Create an empty Linked Stack Before After ? ? 0 count top count top (no stack) (empty stack) rer: Duc Dung Nguyen, PhD.vn| Data Structure and Algorithms [CO2003] 19 / 86 eate an empty Linked Stack Algorithm createStack(ref stack <metadata>) Initializes the metadata of a stack Pre: stack is a metadata structure of a stack Post: metadata initialized stack.top = null return End createStack Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 20 / 86 Create an empty Linked Stack template <class List ltemType> Stack<List_ItemType >::Stack(){ is—>top = NULL; —>count = 0; template <class List ltemType> Stack<List_ItemType >::~ Stack (){ this—>Clear(); } Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 21 / 86 Push data into a Linked Stac Before After oC] BC Bore data next Cd data / next stacl stacl : uA L: ]EÍl le] count top data / next count top data / next ⁄ˆ L [seen] DI [sen] Di data next data next 1.
Allocate memory for the new node and set up data. Update pointers: e Point the new node to the top node (before adding the new node). e Point top to the new node. Update count Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 22 / 86 Push data into a Linked Stack Algorithm pushStack(ref stack <metadata>, val data <dataType>) Inserts (pushes) one item into the stack Pre: stack is a metadata structure to a valid stack data contains value to be pushed into the stack Post: data have been pushed in stack Return true if successful; false if memory overflow Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.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 = pNew stack.count + 1 success = true end return success End pushStack Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 24 / 86 Push data into a Linked Stack template <class List ltemType> void Stack<List_ItemType >::Push (List_ItemType value){ Node<List_ItemType>* pNew = new Node<List_ItemType >(); pNew—>data = value; pNew—>next this —>top; this—>top = pNew; this —>count++; Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 25 / 86 Push data into a Linked Stac e Push is successful when allocation memory for the new node is successful.
e There is no difference between push data into a stack having elements and push data into an empty stack (top having NULL value is assigned to pNew->next: that’s corresponding to a list having only one element). Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 26 / 86 Pop Linked Stack Before After ar [Bs Ja next data ⁄ˆ next count top data count top data next data__next data Lo (ver) next 1. dltPtr holds the element on the top of the stack.
top points to the next element. Decrease count by 1. Lecturer: Duc Dung Nguyen, PhD.vn| Data Structure and Algorithms [CO2003] 27 / 86 Pop Linked Stack AIgorithm popStack(ref stack <metadata>, ref dataOut <dataType>) Pops the item on the top of the stack and returns it to caller Pre: stack is a metadata structure to a valid stack dataOut is to receive the popped data Post: data have been returned to caller Return true if successful; false if stack is empty Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 28 / 86 Pop Linked Stack if stack empty then | success = false else ditPtr = stack.top dataOut = stack.top -> data stack.top -> next stack.count - 1 recycle(dltPtr) success = true end return success End popStack Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 29 / 86 Pop Linked Stack template <class List ltemType> int Stack<List_ItemType >::Pop (List_ItemType &dataOut){ if (this—>GetSize() = 0) return 0; Node<List_ItemType>* dIltPtr = this—>top; dataOut = dltPtr->data; t —>top = ditPtr—>next; this —>count —-; delete ditPtr; return 1; Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.vn| Data Structure and Algorithms [CO2003] 30 / 86 Pop Linked Stack e Pop is successful when the stack is not empty. e There is no difference between pop an element from a stack having elements and pop the only-one element in the stack (d1tPtr->next having NULL value is assigned to top: that’s corresponding to an empty stack). Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 31/86 AIgorithm stackTop(ref stack <metadata>, ref dataOut <dataType>) Retrieves the data from the top of the stack without changing the stack Pre: stack is a metadata structure to a valid stack dataOut is to receive top stack data Post: data have been returned to caller Return true if successful; false if stack is empty Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 32/86 if stack empty then | success = false else dataOut = stack.top -> data success = true end return success End stackTop Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 33 / 86 Stack Top template <class List ltemType> int Stack<List_ItemType >::GetStackTop (List_ItemType &dataOut){ if (this->GetSize() = 0) return 0; dataOut = this—>top—>data; return 1; Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 34/86 Algorithm destroyStack(ref stack <metadata>) Releases all nodes back to memory Pre: stack is a metadata structure to a valid stack Post: stack empty and all nodes recycled Lecturer: Duc Dung Nguyen, PhD.
Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 35 / 86 if stack not empty then while stack.top not null do temp = stack.top -> next recycle(temp) end end stack.count = 0 return End destroyStack Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 36 / 86 Destroy Stack template <class List ltemType> void Stack<List_ItemType >::Clear() { Node<List_ItemType>* temp; while (this—>top != NULL){ temp = this—>top; this—>top = this—>top—>next ; delete temp; } this—>count = 0; Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.vn] Data Structure and Algorithms [CO2003] 37 / 86 mpty Linked Stack Algorithm isEmpty(ref stack <metadata>) Determines if the stack is empty Pre: stack is a metadata structure to a valid stack Post: return stack status Return true if the stack is empty, false otherwise if count = 0 then | Return true else | Return false end End isEmpty Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hemut.