C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 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: From Problem Analysis to Program Design, Fourth Edition7 Default Constructor Initializes the stack to an empty state when a stack object is declared −Sets stackTop to NULL
C++ Programming: From Problem Analysis to Program Design, Fourth Edition8 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: From Problem Analysis to Program Design, Fourth Edition9 Initialize Stack
C++ Programming: From Problem Analysis to Program Design, Fourth Edition10 Push The newElement is added at the beginning of the linked list pointed to by stackTop
C++ Programming: From Problem Analysis to Program Design, Fourth Edition11 Push (continued)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition12 Push (continued)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition13 Push (continued)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition14 Push (continued) We do not need to check whether the stack is full before we push an element onto the stack
C++ Programming: From Problem Analysis to Program Design, Fourth Edition15 Return the Top Element
C++ Programming: From Problem Analysis to Program Design, Fourth Edition16. Pop Node pointed to by stackTop is removed
C++ Programming: From Problem Analysis to Program Design, Fourth Edition18 Pop (continued)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition19 Copy Stack
C++ Programming: From Problem Analysis to Program Design, Fourth Edition20 Copy Stack (continued) Notice that this function is similar to the definition of copyList for linked lists
C++ Programming: From Problem Analysis to Program Design, Fourth Edition21 Constructors and Destructors
C++ Programming: From Problem Analysis to Program Design, Fourth Edition22 Overloading the Assignment Operator (=)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition23 Stack as Derived from the class unorderedLinkedList Our implementation of push is similar to insertFirst (discussed for general lists) −Other functions are similar too: initializeStack and initializeList isEmptyList and isEmptyStack linkedStackType can be derived from linkedListType − class linkedListType is abstract Must implement pop as described earlier
C++ Programming: From Problem Analysis to Program Design, Fourth Edition24 Stack as Derived from the class unorderedLinkedList (cont.) unorderedLinkedListType is derived from linkedListType −Provides the definitions of the abstract functions of the class linkedListType We can derive the linkedStackType from unorderedLinkedListType
C++ Programming: From Problem Analysis to Program Design, Fourth Edition25 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: From Problem Analysis to Program Design, Fourth Edition26 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: From Problem Analysis to Program Design, Fourth Edition27 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: From Problem Analysis to Program Design, Fourth Edition28 Application of Stacks: Postfix Expressions Calculator (continued)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition29 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: From Problem Analysis to Program Design, Fourth Edition30 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: From Problem Analysis to Program Design, Fourth Edition31 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: From Problem Analysis to Program Design, Fourth Edition32 Example: * = −Read next symbol 2 is a number push it onto the stack Application of Stacks: Postfix Expressions Calculator (continued)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition33 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: From Problem Analysis to Program Design, Fourth Edition34 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: From Problem Analysis to Program Design, Fourth Edition35 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: From Problem Analysis to Program Design, Fourth Edition36 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: From Problem Analysis to Program Design, Fourth Edition37 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: From Problem Analysis to Program Design, Fourth Edition38 Main Algorithm Pseudocode: We will write four functions: − evaluateExpression, evaluateOpr, discardExp, and printResult
C++ Programming: From Problem Analysis to Program Design, Fourth Edition39 Function evaluateExpression
C++ Programming: From Problem Analysis to Program Design, Fourth Edition40 Function evaluateOpr
C++ Programming: From Problem Analysis to Program Design, Fourth Edition41
C++ Programming: From Problem Analysis to Program Design, Fourth Edition42 Function discardExp This function is called whenever an error is discovered in the expression
C++ Programming: From Problem Analysis to Program Design, Fourth Edition43 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
C++ Programming: From Problem Analysis to Program Design, Fourth Edition44
C++ Programming: From Problem Analysis to Program Design, Fourth Edition45 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)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition46
C++ Programming: From Problem Analysis to Program Design, Fourth Edition47
C++ Programming: From Problem Analysis to Program Design, Fourth Edition48 Let us now execute the following statements: Output: Removing Recursion (continued) Linked Implementation of Stacks
C++ Programming: From Problem Analysis to Program Design, Fourth Edition49 Queues (This topic continues in file 02096_PPT_ch19-3.ppt )