1 / 42

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. 7 – Stack and Queue. Overview:. A Stack Collection Stack Implementation 3. Stack applications A queue Collection Queue Implementation.

astro
Download Presentation

7 – Stack and Queue

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 7 – Stack and Queue

  2. Overview: • A Stack Collection • Stack Implementation 3. Stack applications • A queue Collection • Queue Implementation

  3. push pop Other operations: isEmptypeeksize A Stack (of plates) 1. The Stack Collection • A stack is a sequence of items that are accessible only from the top of the stack.

  4. 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.

  5. 1.2 Stack Operations : Push 1. 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.

  6. 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.

  7. 1.2 Stack Operations : Pop Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

  8. 1.2 Stack Operations : Stack Top 3. 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.

  9. 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.

  10. Stack operations Example (2/2) • stack top =? • pop • pop Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

  11. 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.

  12. 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 datanode 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.

  13. Stack head node structure and data node structure Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

  14. 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.

  15. Stack operations Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

  16. Create Stack Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

  17. PushStack Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

  18. PopStack Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

  19. Stack Top Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

  20. Empty StackChecking Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

  21. Full Stack Checking Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

  22. Stack Count Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

  23. Stack Destruction Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

  24. 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.

  25. 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.

  26. 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.

  27. 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.

  28. 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.

  29. 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. BegindecimalToBinary 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.

  30. 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) EnddecimalToBinary Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

  31. 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

  32. 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

  33. 3 5 4 4 12 12 17 Postfix Evaluation (2/4) • Example: evaluate "4 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

  34. 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

  35. 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

  36. Algorithm for coverting Infix to Postfix(1/2) Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

  37. Algorithm for coverting Infix to Postfix(2/2) 3 end loop 4 pushStack (Stack, token) 4 elseCharacter is operand 1 Concatenate token to postFix 5 end if3 end loopInput formula empty. Pop stack to postfix4 loop ( not emptyStack(Stack) ) 1 popStack(Stack, character) 2 concatenate token to postFixExpr5 end loop6 return postFixend intoPostFix Original slides from Data Structures a pseudocode approach with C by Gilberg, R. and Forouzan, B.

  38. front rear 4. Queue Collection • Like a stack, aqueue 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). Original slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt

  39. Remove(Dequeue) Insert (Enqueue) front rear 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 Original slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt

  40. 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 StructureCount (number of elements) Front (pointer to front node) Rear (pointer to rear node) • Queue Node StructureData (node data) Link (pointer to next node) Original slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt

  41. 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

  42. 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 • And more… Original slides from http://course.cs.ust.hk/comp171/Spring09/tut/T3.ppt

More Related