Download presentation
Presentation is loading. Please wait.
Published byArline Perry Modified over 9 years ago
1
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
2
C++ Programming: Program Design Including Data Structures, Fourth Edition 2 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
7
C++ Programming: Program Design Including Data Structures, Fourth Edition 7 Default Constructor Initializes the stack to an empty state when a stack object is declared −Sets stackTop to NULL
8
C++ Programming: Program Design Including Data Structures, Fourth Edition 8 Empty Stack and Full Stack In the linked implementation of stacks, the function isFullStack does not apply −Logically, the stack is never full
9
C++ Programming: Program Design Including Data Structures, Fourth Edition 9 Initialize Stack
10
C++ Programming: Program Design Including Data Structures, Fourth Edition 10 Push The newElement is added at the beginning of the linked list pointed to by stackTop
11
C++ Programming: Program Design Including Data Structures, Fourth Edition 11 Push (continued)
12
C++ Programming: Program Design Including Data Structures, Fourth Edition 12 Push (continued)
13
C++ Programming: Program Design Including Data Structures, Fourth Edition 13 Push (continued)
14
C++ Programming: Program Design Including Data Structures, Fourth Edition 14 Push (continued) We do not need to check whether the stack is full before we push an element onto the stack
15
C++ Programming: Program Design Including Data Structures, Fourth Edition 15 Return the Top Element
16
C++ Programming: Program Design Including Data Structures, Fourth Edition 16. Pop Node pointed to by stackTop is removed
18
C++ Programming: Program Design Including Data Structures, Fourth Edition 18 Pop (continued)
19
C++ Programming: Program Design Including Data Structures, Fourth Edition 19 Copy Stack
20
C++ Programming: Program Design Including Data Structures, Fourth Edition 20 Copy Stack (continued) Notice that this function is similar to the definition of copyList for linked lists
21
C++ Programming: Program Design Including Data Structures, Fourth Edition 21 Constructors and Destructors
22
C++ Programming: Program Design Including Data Structures, Fourth Edition 22 Overloading the Assignment Operator (=)
23
C++ Programming: Program Design Including Data Structures, Fourth Edition 23 Application of Stacks: Postfix Expressions Calculator Infix notation: usual notation for writing arithmetic expressions −The operator is written between the operands −Example: a + b −The operators have precedence Parentheses can be used to override precedence
24
C++ Programming: Program Design Including Data Structures, Fourth Edition 24 Application of Stacks: Postfix Expressions Calculator (continued) Prefix (Polish) notation: the operators are written before the operands −Introduced by the Polish mathematician Jan Lukasiewicz Early 1920s −The parentheses can be omitted −Example: + a b
25
C++ Programming: Program Design Including Data Structures, Fourth Edition 25 Application of Stacks: Postfix Expressions Calculator (continued) Reverse Polish notation: the operators follow the operands (postfix operators) −Proposed by the Australian philosopher and early computer scientist Charles L. Hamblin Late 1950's −Advantage: the operators appear in the order required for computation −Example: a + b * c In a postfix expression: a b c * +
26
C++ Programming: Program Design Including Data Structures, Fourth Edition 26 Application of Stacks: Postfix Expressions Calculator (continued)
27
C++ Programming: Program Design Including Data Structures, Fourth Edition 27 Application of Stacks: Postfix Expressions Calculator (continued) Postfix notation has important applications in computer science −Many compilers first translate arithmetic expressions into postfix notation and then translate this expression into machine code Evaluation algorithm: −Scan expression from left to right −When an operator is found, back up to get the operands, perform the operation, and continue
28
C++ Programming: Program Design Including Data Structures, Fourth Edition 28 Application of Stacks: Postfix Expressions Calculator (continued) Example: 6 3 + 2 * = −Read first symbol 6 is a number push it onto the stack −Read next symbol 3 is a number push it onto the stack
29
C++ Programming: Program Design Including Data Structures, Fourth Edition 29 Application of Stacks: Postfix Expressions Calculator (continued) Example: 6 3 + 2 * = −Read next symbol + is an operator (two operands) pop stack twice, perform operation, put result back onto stack
30
C++ Programming: Program Design Including Data Structures, Fourth Edition 30 Example: 6 3 + 2 * = −Read next symbol 2 is a number push it onto the stack Application of Stacks: Postfix Expressions Calculator (continued)
31
C++ Programming: Program Design Including Data Structures, Fourth Edition 31 Example: 6 3 + 2 * = −Read next symbol * is an operator pop stack twice, perform operation, put result back onto stack Application of Stacks: Postfix Expressions Calculator (continued)
32
C++ Programming: Program Design Including Data Structures, Fourth Edition 32 Example: 6 3 + 2 * = −Read next symbol = is an operator indicates end of expression Print the result (pop stack first) Application of Stacks: Postfix Expressions Calculator (continued)
33
C++ Programming: Program Design Including Data Structures, Fourth Edition 33 Application of Stacks: Postfix Expressions Calculator (continued) Symbols can be numbers or anything else: −+, -, *, and / are operators Pop stack twice and evaluate expression If stack has less than two elements error −If symbol is =, the expression ends Pop and print answer from stack If stack has more than one element error −If symbol is anything else Expression contains an illegal operator
34
C++ Programming: Program Design Including Data Structures, Fourth Edition 34 Application of Stacks: Postfix Expressions Calculator (continued) Examples: 7 6 + 3 ; 6 - = ; is an illegal operator 14 + 2 3 * = Does not have enough operands for + 14 2 3 + = Error: stack will have two elements when we encounter equal ( = ) sign
35
C++ Programming: Program Design Including Data Structures, Fourth Edition 35 Application of Stacks: Postfix Expressions Calculator (continued) We assume that the postfix expressions are in the following form: #6 #3 + #2 * = −If symbol scanned is #, next input is a number −If the symbol scanned is not #, then it is: An operator (may be illegal) or An equal sign (end of expression) We assume expressions contain only +, -, *, and / operators
36
C++ Programming: Program Design Including Data Structures, Fourth Edition 36 Main Algorithm Pseudocode: We will write four functions: − evaluateExpression, evaluateOpr, discardExp, and printResult
37
C++ Programming: Program Design Including Data Structures, Fourth Edition 37 Function evaluateExpression
38
C++ Programming: Program Design Including Data Structures, Fourth Edition 38 Function evaluateOpr
39
39
40
C++ Programming: Program Design Including Data Structures, Fourth Edition 40 Function discardExp This function is called whenever an error is discovered in the expression
41
C++ Programming: Program Design Including Data Structures, Fourth Edition 41 Function printResult If the postfix expression contains no errors, the function printResult prints the result −Otherwise, it outputs an appropriate message The result of the expression is in the stack and the output is sent to a file
42
42
43
C++ Programming: Program Design Including Data Structures, Fourth Edition 43 Removing Recursion: Nonrecursive Algorithm to Print a Linked List Backward To print the list backward, first we need to get to the last node of the list −Problem: how do we get back to previous node? Links go in only one direction −Solution: save a pointer to each of the nodes with info 5, 10, and 15 Use a stack (LIFO)
44
44
45
45
46
C++ Programming: Program Design Including Data Structures, Fourth Edition 46 Let us now execute the following statements: Output: 20 15 10 5 Removing Recursion (continued)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.