1 / 89

C++ Programming: Program Design Including Data Structures, Fourth Edition

C++ Programming: Program Design Including Data Structures, Fourth Edition. Chapter 18: Stacks and Queues. Objectives. In this chapter, you will: Learn about stacks Examine various stack operations Learn how to implement a stack as an array Learn how to implement a stack as a linked list

brenna
Download Presentation

C++ Programming: Program Design Including Data Structures, Fourth Edition

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. C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues

  2. Objectives In this chapter, you will: • Learn about stacks • Examine various stack operations • Learn how to implement a stack as an array • Learn how to implement a stack as a linked list • Discover stack applications • Learn how to use a stack to remove recursion C++ Programming: Program Design Including Data Structures, Fourth Edition

  3. Objectives (continued) • Learn about queues • Examine various queue operations • Learn how to implement a queue as an array • Learn how to implement a queue as a linked list • Discover queue applications C++ Programming: Program Design Including Data Structures, Fourth Edition

  4. Stacks • Stack: list of homogenous elements • Addition and deletion occur only at one end, called the top of the stack • Example: in a cafeteria, the second tray can be removed only if first tray has been removed • Last in first out (LIFO) data structure • Operations: • Push: to add an element onto the stack • Pop: to remove an element from the stack C++ Programming: Program Design Including Data Structures, Fourth Edition

  5. Stacks (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition

  6. Stack Operations • In the abstract class stackADT: • initializeStack • isEmptyStack • isFullStack • push • top • pop C++ Programming: Program Design Including Data Structures, Fourth Edition

  7. Stack Application Stack applications classified into 4 broad categories: • Reversing data – e.g. reverse a list & convert decimal to binary. • Eg. 26 = 110102 • Parsing data – e.g. translate a source program to machine language. • Postponing data usage – e.g. evaluation, transformation. • Backtracking – e.g. computer gaming, decision analysis, expert systems.

  8. Implementation of Stacks as Arrays • First element can go in first array position, the second in the second position, etc. • The top of the stack is the index of the last element added to the stack • Stack elements are stored in an array • Stack element is accessed only through top • To keep track of the top position, use a variable called stackTop C++ Programming: Program Design Including Data Structures, Fourth Edition

  9. Implementation of Stacks as Arrays (continued) • Because stack is homogeneous • You can use an array to implement a stack • Can dynamically allocate array • Enables user to specify size of the array • The class stackType implements the functions of the abstract class stackADT C++ Programming: Program Design Including Data Structures, Fourth Edition

  10. Implementation of Stacks as Arrays (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition

  11. Implementation of Stacks as Arrays (continued) • C++ arrays begin with the index 0 • Must distinguish between: • The value of stackTop • The array position indicated by stackTop • If stackTop is 0, the stack is empty • If stackTop is nonzero, the stack is not empty • The top element is given by stackTop - 1 C++ Programming: Program Design Including Data Structures, Fourth Edition

  12. Implementation of Stacks as Arrays (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition

  13. Initialize Stack C++ Programming: Program Design Including Data Structures, Fourth Edition

  14. Empty Stack • If stackTop is 0, the stack is empty C++ Programming: Program Design Including Data Structures, Fourth Edition

  15. Full Stack • The stack is full if stackTop is equal to maxStackSize C++ Programming: Program Design Including Data Structures, Fourth Edition

  16. Push • Store the newItem in the array component indicated by stackTop • Increment stackTop • Must avoid an overflow C++ Programming: Program Design Including Data Structures, Fourth Edition

  17. Push (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition

  18. Return the Top Element C++ Programming: Program Design Including Data Structures, Fourth Edition

  19. Pop • Simply decrement stackTop by 1 • Must check for underflow condition C++ Programming: Program Design Including Data Structures, Fourth Edition

  20. Stacks Example Example Suppose the following 6 elements are pushed, in order, onto an empty stacks. AAA, BBB, CCC, DDD, EEE, FFF We write the stack: STACK: AAA, BBB, CCC, DDD, EEE, FFF

  21. Pseudocode for Array of Stacks Procedure 6.1 PUSH(STACK, TOP, MAXSTK, ITEM) This procedure pushes an ITEM onto a stack. 1. [Stack already filled ?] If TOP = MAXSTK, then: Print:OVERFLOW, and Return. 2. Set TOP := TOP + 1. [Increses TOP by 1] 3. Set STACK[TOP] :=ITEM. [Inserts ITEM in new TOP position. 4. Return.

  22. Pseudocode for Array of Stacks Procedure 6.2 POP(STACK, TOP, ITEM) This procedures deletes the top element of STACK and assigns it to the variable ITEM. 1. [Stacks has an item to be removed?] If TOP = 0, the Print: UNDERFLOW, and RETURN. 2. Set ITEM := STACK[TOP].[Assigns TOP elements to ITEM.] 3. Set TOP := TOP – 1.[ Decreases TOP by 1] 4. Return.

  23. Stack Exercise Exercise 6.1 Consider the following stack of characters, where STACK is allocated N = 8 memory cells : STACK : A,C,D, F, K, _, _ , _ ( For notational convenience, we use “_” to denote an empty memory cell ). Describe the stack as the following operations take place : (a) POP (STACK, ITEM ) (e) POP (STACK, ITEM) (b) POP (STACK,ITEM) (f) PUSH(STACK, R) (c) PUSH (STACK, L) (g) PUSH(STACK, S) (d) PUSH (STACK, P) (h) POP(STACK, ITEM)

  24. Stack Exercise 6.2 Consider the data in Prob. 6.1 (a) When overflow occur ? (b) When will C be deleted before D ? 6.3 Consider the following stack, where STACK is allocated N = 6 memory cells : STACK : AAA, DDD, EEE, FFF, GGG, _______. (a) PUSH(STACK, KKK) (b) POP(STACK, ITEM ) (c) PUSH(STACK, LLL) (d) PUSH(STACK,SSS) (e) POP(STACK, ITEM) (f) PUSH(STACK, ITEM)

  25. Copy Stack C++ Programming: Program Design Including Data Structures, Fourth Edition

  26. Constructor and Destructor C++ Programming: Program Design Including Data Structures, Fourth Edition

  27. Constructor and Destructor (continued) C++ Programming: Program Design Including Data Structures, Fourth Edition

  28. Copy Constructor C++ Programming: Program Design Including Data Structures, Fourth Edition

  29. Overloading the Assignment Operator (=) C++ Programming: Program Design Including Data Structures, Fourth Edition

  30. Stack Header File • Place definitions of class and functions (stack operations) together in a file C++ Programming: Program Design Including Data Structures, Fourth Edition

  31. Programming Example: Highest GPA • Input: program reads an input file with each student’s GPA and name 3.5 Bill 3.6 John 2.7 Lisa 3.9 Kathy 3.4 Jason 3.9 David 3.4 Jack • Output: the highest GPA and all the names associated with the highest GPA C++ Programming: Program Design Including Data Structures, Fourth Edition

  32. Problem Analysis and Algorithm Design • Read the first GPA and name of the student • This is the highest GPA so far • Read the second GPA and student name • Compare this GPA with highest GPA so far • New GPA is greater than highest GPA so far • Update highest GPA, initialize stack, add to stack • New GPA is equal to the highest GPA so far • Add name to stack • New GPA is smaller than the highest GPA • Discard C++ Programming: Program Design Including Data Structures, Fourth Edition

  33. Linked Implementation of Stacks Array only allows fixed number of elements If number of elements to be pushed exceeds array size Program may terminate Linked lists can dynamically organize data In a linked representation, stackTop is pointer to top element in stack 43 C++ Programming: Program Design Including Data Structures, Fourth Edition

  34. Default Constructor Initializes the stack to an empty state when a stack object is declared Sets stackTop to NULL 48 C++ Programming: Program Design Including Data Structures, Fourth Edition

  35. Empty Stack and Full Stack In the linked implementation of stacks, the function isFullStack does not apply Logically, the stack is never full 49 C++ Programming: Program Design Including Data Structures, Fourth Edition

  36. Initialize Stack 50 C++ Programming: Program Design Including Data Structures, Fourth Edition

More Related