Objectives of these slides: 7 – Stack and Queue Objectives of these slides: Explain the design, use, and operation of a stack and a queue Implement a stack and a queue using a linked list structure
A Stack Collection Stack Implementation 3. Stack applications Overview: A Stack Collection Stack Implementation 3. Stack applications A queue Collection Queue Implementation
1. The Stack Collection A stack is a sequence of items that are accessible only from the top of the stack. A Stack (of plates) push pop Other operations: isEmpty peek size
1. The Stack Collection: Stack Operations A stack is a Last In, First Out (LIFO) data structure in which all insertions and deletion are restricted to one end called a top Three basic stack operations - Push - Pop - Stack Top Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
1.2 Stack Operations : Push adds an item at the top of the stack after the push, the new item becomes the top the stack is in an overflow state if there is no room for the new item Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
1.2 Stack Operations : Pop 2. Pop when a stack is popped, the item at the top of the stack is removed and return it to the user as the top item has been removed, the next older item in the stack becomes the top when the last item in the stack is deleted, it must be set to its empty state if pop is called when the stack is empty, then it is in an underflow state Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
1.2 Stack Operations : Pop Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
1.2 Stack Operations : Stack Top copies the item at the top of the stack it returns the data in the top element to the user but does not delete it stack top can also result in underflow if the stack is empty Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Stack operations Example (1/2) 0. with an empty stack push green into stack push blue into stack pop push red into stack Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Stack operations Example (2/2) stack top =? pop Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
2. Stack implementation There are several data structures that could be used to implement a stack, e.g. array or linked list In this section, we implement it as a linked list To implement the linked-list stack, we need two different structures: - a head node - a data node Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
2.1 Stack Head and Stack Data Node stack head requires at least 2 attributes a count of the number of elements in the stack a top pointer other stack attributes can be placed in a stack head such as the time the stack was created stack data node also requires at least 2 attributes data pointer to the next node Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Stack head node structure and data node structure Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
2.2 Stack Algorithms 8 operations are defined to solve any basic stack problem create stack destroy stack push stack pop stack Checking stack status full stack empty stack a number of member (stack count) stack top there may be additional stack operations Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Stack operations Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Create Stack Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Push Stack Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Pop Stack Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Stack Top Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Empty Stack Checking Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Full Stack Checking Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Stack Count Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Stack Destruction Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
3. Stack Applications 3.1 Reversing Data 3.2 Converting Decimal to Binary 3.3 Converting Infix to Postfix (optional) Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
3.1 Reversing Data given a set of data, the first and last elements are exchanged with all of the positions between the first and last being relatively exchanged also for example: {1, 2, 3, 4} becomes {4, 3, 2, 1} we examine 2 different reversing applications: reveres a list convert decimal to binary Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Algorithm Reverse a number series (1/2) Algorithm reverseNumber This program reverses a list of integers read from the keyboard by pushing them into a stack and retrieving them one by one Begin reverseNumber 1 stack = createStack 2 print(Enter a number) 3 read(number) Continued… Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Algorithm Reverse a number series (2/2) /*---- Fill stack ---* / 4 loop ( not end of data AND stack not full) 1 pushStack (number) 2 prompt(Enter next number: <EOF> to stop) 3 read( number ) /*---- Print numbers in reverse ---*/ 5 loop (not emptyStack (stack) 1 popStack (stack, dataOut) 2 print (dataOut) 6 stack = destroyStack (stack) End reverseNumber Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
3.2 Convert Decimal to Binary 1 read (number) 2 loop (number > 0) 1 digit = number modulo 2 2 print (digit) 3 number = number / 2 Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Algorithm Convert decimal to binary (1/2) algorithm decimalToBinary This algorithm reads an integer from the keyboard and print its binary equivalent. It uses a stack to reverse the order of 0’s and 1’s produces. Begin decimalToBinary 1 stack = createStack 2 prompt(Enter a decimal to convert to binary) 3 read (number) 4 loop (number > 0) 1 digit = number modulo 2 2 pushOK = push (stack, digit) 3 if (pushOK ≠ false) 1 print (Stack overflow creating digit) 2 quit algorithm 4 number = number/2 Continued… Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Algorithm Convert decimal to binary (2/2) /* Binary number create in stack. Now print it. */ 5 loop (not emptyStack(stack)) 1 popStack (stack, digit) 2 print(digit) /* Binary number create. Destroy stack and return */ 6 destroy(stack) End decimalToBinary Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
3.3 Convert Infix to Postfix (arithmetic expression) Prefix Notation (Polish notation): operator is always placed before its operands e.g. + 1 2 , * 3 – 4 5 , / 9 + 1 – 8 6 In the Postfix evaluation format for an expression, an operator comes after its operands. also called Reverse Polish Notation (RPN) Examples: Infix notation Postfix notation a + b RPN: a b + a * b + c RPN: a b * c + a + (b * c) RPN: a b c * + (a + b) * c RPN: a b + c * (a*b + c) / d RPN: a b * c + d / Original slides from http://fivedots.coe.psu.ac.th/Software.coe/ADSA/Slides/08.%20Stacks.ppt
Postfix Evaluation (1/4) To evaluate a postfix expression, execute the following steps until the end of the expression. If current input is an operand, push it on the stack. If current input is an operator, pop its two operands, apply the operator, and push the result onto the stack. At the end of input, the value of the postfix expression is on the top of the stack. Postfix and prefix (Polish) notations do not need the parenthesis and priority of operators. The order of evaluation is not ambiguous. One expression can only be evaluated in one way. Original slides from http://fivedots.coe.psu.ac.th/Software.coe/ADSA/Slides/08.%20Stacks.ppt
Postfix Evaluation (2/4) Example: evaluate "4 3 * 5 +" 4 12 17 3 5 Evaluate these postfix expressions: 8 2 – 3 / 4 2 * + 28 7 13 5 6 + - * / Original slides from http://fivedots.coe.psu.ac.th/Software.coe/ADSA/Slides/08.%20Stacks.ppt
Postfix evalutation: Error: too many operators (3/4) e.g. 3 8 + * 9 The stack will contain only one element when we reach "*". Original slides from http://fivedots.coe.psu.ac.th/Software.coe/ADSA/Slides/08.%20Stacks.ppt
Postfix evalutation: Error: too many operands (4/4) e.g. 9 8 + 7 The stack will contain too many operands at end of input. Original slides from http://fivedots.coe.psu.ac.th/Software.coe/ADSA/Slides/08.%20Stacks.ppt
Algorithm for coverting Infix to Postfix (1/2) Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
Algorithm for coverting Infix to Postfix (2/2) 3 end loop 4 pushStack (Stack, token) 4 else Character is operand 1 Concatenate token to postFix 5 end if 3 end loop Input formula empty. Pop stack to postfix 4 loop ( not emptyStack(Stack) ) 1 popStack(Stack, character) 2 concatenate token to postFixExpr 5 end loop 6 return postFix end intoPostFix Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.
4. Queue Collection Like a stack, a queue is also a list. However, with a queue, insertion is done at one end, while deletion is performed at the other end. Accessing the elements of queues follows a First In, First Out (FIFO) order. Like customers standing in a check-out line in a store, the first customer in is the first customer served (first come first serve). rear front Original slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt
4.1 Enqueue and Dequeue Primary queue operations: Enqueue and Dequeue Like check-out lines in a store, a queue has a front and a rear. Enqueue insert an element at the rear of the queue Dequeue remove an element from the front of the queue Insert (Enqueue) Remove (Dequeue) rear front Original slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt
5. Implementation of Queue Just as stacks can be implemented as arrays or linked lists, so with queues. Dynamic queues have the same advantages over static queues as dynamic stacks have over static stacks Example: Queue implemented with linked list data structure Queue Head Structure Count (number of elements) Front (pointer to front node) Rear (pointer to rear node) Queue Node Structure Data (node data) Link (pointer to next node) Original slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt
5.1 Application of Queues In a multitasking operating system, the CPU time is shared between multiple processes. At a given time, only one process is running, all the others are ‘sleeping’. The CPU time is administered by the scheduler. The scheduler keeps all current processes in a queue with the active process at the front of the queue. Original slides from http://isg.cs.tcd.ie/giangt/Stack-Queue.pdf
5.2 Stack and Queue comparison List out one similarity and one difference between Stack and Queue Similarity Both ADTs can be implemented using array or linked list Both support fast insertion and extraction (although the rule to govern the insertion and extraction is different) And more… Difference Stack is a LIFO data structure, while Queue is a FIFO data structure Original slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt