C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 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
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
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
C++ Programming: Program Design Including Data Structures, Fourth Edition 9 Initialize Stack
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
C++ Programming: Program Design Including Data Structures, Fourth Edition 11 Push (continued)
C++ Programming: Program Design Including Data Structures, Fourth Edition 12 Push (continued)
C++ Programming: Program Design Including Data Structures, Fourth Edition 13 Push (continued)
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
C++ Programming: Program Design Including Data Structures, Fourth Edition 15 Return the Top Element
C++ Programming: Program Design Including Data Structures, Fourth Edition 16. Pop Node pointed to by stackTop is removed
C++ Programming: Program Design Including Data Structures, Fourth Edition 18 Pop (continued)
C++ Programming: Program Design Including Data Structures, Fourth Edition 19 Copy Stack
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
C++ Programming: Program Design Including Data Structures, Fourth Edition 21 Constructors and Destructors
C++ Programming: Program Design Including Data Structures, Fourth Edition 22 Overloading the Assignment Operator (=)
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
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
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 * +
C++ Programming: Program Design Including Data Structures, Fourth Edition 26 Application of Stacks: Postfix Expressions Calculator (continued)
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
C++ Programming: Program Design Including Data Structures, Fourth Edition 28 Application of Stacks: Postfix Expressions Calculator (continued) Example: * = −Read first symbol 6 is a number push it onto the stack −Read next symbol 3 is a number push it onto the stack
C++ Programming: Program Design Including Data Structures, Fourth Edition 29 Application of Stacks: Postfix Expressions Calculator (continued) Example: * = −Read next symbol + is an operator (two operands) pop stack twice, perform operation, put result back onto stack
C++ Programming: Program Design Including Data Structures, Fourth Edition 30 Example: * = −Read next symbol 2 is a number push it onto the stack Application of Stacks: Postfix Expressions Calculator (continued)
C++ Programming: Program Design Including Data Structures, Fourth Edition 31 Example: * = −Read next symbol * is an operator pop stack twice, perform operation, put result back onto stack Application of Stacks: Postfix Expressions Calculator (continued)
C++ Programming: Program Design Including Data Structures, Fourth Edition 32 Example: * = −Read next symbol = is an operator indicates end of expression Print the result (pop stack first) Application of Stacks: Postfix Expressions Calculator (continued)
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
C++ Programming: Program Design Including Data Structures, Fourth Edition 34 Application of Stacks: Postfix Expressions Calculator (continued) Examples: ; 6 - = ; is an illegal operator * = Does not have enough operands for = Error: stack will have two elements when we encounter equal ( = ) sign
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
C++ Programming: Program Design Including Data Structures, Fourth Edition 36 Main Algorithm Pseudocode: We will write four functions: − evaluateExpression, evaluateOpr, discardExp, and printResult
C++ Programming: Program Design Including Data Structures, Fourth Edition 37 Function evaluateExpression
C++ Programming: Program Design Including Data Structures, Fourth Edition 38 Function evaluateOpr
39
C++ Programming: Program Design Including Data Structures, Fourth Edition 40 Function discardExp This function is called whenever an error is discovered in the expression
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
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
45
C++ Programming: Program Design Including Data Structures, Fourth Edition 46 Let us now execute the following statements: Output: Removing Recursion (continued)