Download presentation
Presentation is loading. Please wait.
Published byLoren Henry Modified over 9 years ago
1
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues
2
C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 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
3
C++ Programming: From Problem Analysis to Program Design, Fourth Edition3 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
4
C++ Programming: From Problem Analysis to Program Design, Fourth Edition4 Stacks Stack: list of homogenous elements −Addition and deletion occurs 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
5
C++ Programming: From Problem Analysis to Program Design, Fourth Edition5 Stacks (continued)
7
C++ Programming: From Problem Analysis to Program Design, Fourth Edition7 Stack Operations In the abstract class stackADT : − initializeStack − isEmptyStack − isFullStack − push − top − pop
9
C++ Programming: From Problem Analysis to Program Design, Fourth Edition9 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
10
C++ Programming: From Problem Analysis to Program Design, Fourth Edition10 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
13
C++ Programming: From Problem Analysis to Program Design, Fourth Edition13 Implementation of Stacks as Arrays (continued)
14
C++ Programming: From Problem Analysis to Program Design, Fourth Edition14 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
15
C++ Programming: From Problem Analysis to Program Design, Fourth Edition15 Implementation of Stacks as Arrays (continued)
16
C++ Programming: From Problem Analysis to Program Design, Fourth Edition16 Initialize Stack
17
C++ Programming: From Problem Analysis to Program Design, Fourth Edition17 Empty Stack If stackTop is 0, the stack is empty
18
C++ Programming: From Problem Analysis to Program Design, Fourth Edition18 Full Stack The stack is full if stackTop is equal to maxStackSize
19
C++ Programming: From Problem Analysis to Program Design, Fourth Edition19 Push Store the newItem in the array component indicated by stackTop Increment stackTop Must avoid an overflow
20
C++ Programming: From Problem Analysis to Program Design, Fourth Edition20 Push (continued)
21
C++ Programming: From Problem Analysis to Program Design, Fourth Edition21 Return the Top Element
22
C++ Programming: From Problem Analysis to Program Design, Fourth Edition22 Pop Simply decrement stackTop by 1 Must check for underflow condition
24
C++ Programming: From Problem Analysis to Program Design, Fourth Edition24 Copy Stack
25
C++ Programming: From Problem Analysis to Program Design, Fourth Edition25 Constructor and Destructor
26
C++ Programming: From Problem Analysis to Program Design, Fourth Edition26 Constructor and Destructor (continued)
27
C++ Programming: From Problem Analysis to Program Design, Fourth Edition27 Copy Constructor
28
C++ Programming: From Problem Analysis to Program Design, Fourth Edition28 Overloading the Assignment Operator (=)
29
C++ Programming: From Problem Analysis to Program Design, Fourth Edition29 Stack Header File Put definitions of class and functions (stack operations) together in a file
30
…
33
C++ Programming: From Problem Analysis to Program Design, Fourth Edition33 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
34
C++ Programming: From Problem Analysis to Program Design, Fourth Edition34 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
37
C++ Programming: From Problem Analysis to Program Design, Fourth Edition37 Linked Implementation of Stacks (This topic continues in file 02096_PPT_ch19-2.ppt )
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.