Stacks and their Applications Lecture 23 Sections 18.1-18.2 Robb T. Koether Hampden-Sydney College Fri, Mar 16, 2018 Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 1 / 38
1 Stacks 2 The Stack Interface 3 Stack Applications Function Calls Infix, Postfix, and Prefix Notation Infix Expression Evaluation Postfix Expressions 4 Assignment Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 2 / 38
Outline 1 Stacks 2 The Stack Interface 3 Stack Applications Function Calls Infix, Postfix, and Prefix Notation Infix Expression Evaluation Postfix Expressions 4 Assignment Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 3 / 38
Stacks Definition Stack) A stack is a list that operates under the principle last in, first out LIFO). New elements are pushed onto the stack. Old elements are popped off the stack. To enforce the LIFO principle, we use a list and push and pop at the same end. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 4 / 38
Outline 1 Stacks 2 The Stack Interface 3 Stack Applications Function Calls Infix, Postfix, and Prefix Notation Infix Expression Evaluation Postfix Expressions 4 Assignment Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 5 / 38
Stack Constructors Stack Constructors Stack); Stackconst Stack& s); Stack) constructs an empty stack. StackStack&) constructs a copy of the specified stack. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 6 / 38
Stack Inspectors Stack Inspectors T top) const; int size) const; bool isempty) const; top) gets a copy of the element at the top of the stack but does not remove it). size) gets the number of elements in the stack. isempty) determines whether the stack is empty. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 7 / 38
Stack Mutators Stack Mutators void pushconst T& value); T pop); void makeempty); push) pushes the specified value onto the top of the stack. pop) pops and returns the element off the top of the stack. makeempty) makes the stack empty. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 8 / 38
Other Stack Member Functions Other Stack Member Functions bool isvalid) const; isvalid) determines whether the stack has a valid structure. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 9 / 38
Other Stack Functions Other Stack Functions istream& operator>>istream& in, Stack& s); ostream& operator<<ostream& out, const Stack& s); operator>>) reads a Stack object from the input stream. operator<<) writes a Stack object to the output stream. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 10 / 38
Implementation of Stacks Which push and pop functions should we use? pushfront) and popfront), or pushback) and popback). Choose a List class for which pushing and popping at one end will be efficient. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 11 / 38
The Input Facilitator One must be careful when reading a stack. {10, 20, 30, 40, 50} As the values are read from left to right, they should be pushed onto the stack at one end or the other). Which end, left or right, is the top of the stack? It matters.) When we display the stack, it should look the same regardless of the kind of List we used. Do we need to write new input) and/or output) functions? Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 12 / 38
Outline 1 Stacks 2 The Stack Interface 3 Stack Applications Function Calls Infix, Postfix, and Prefix Notation Infix Expression Evaluation Postfix Expressions 4 Assignment Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 13 / 38
Outline 1 Stacks 2 The Stack Interface 3 Stack Applications Function Calls Infix, Postfix, and Prefix Notation Infix Expression Evaluation Postfix Expressions 4 Assignment Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 14 / 38
Handling Function Calls When a function is called, the program Pushes the values of the parameters. Pushes the address of the next instruction to which the function should return later). Allocates space on the stack for the local variables. Branches to the first line in the function. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 15 / 38
Handling Function Calls The Stack Other Stuff Begin with the current stack Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 16 / 38
Handling Function Calls The Stack Other Stuff Function Parameters Push the function parameters Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 16 / 38
Handling Function Calls The Stack Other Stuff Function Parameters Return Address Push the return address Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 16 / 38
Handling Function Calls The Stack Other Stuff Function Parameters Return Address Local Variables Push the local variables Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 16 / 38
Handling Function Calls When a function returns, the program Pops the values of the local variables. Pops the return address and stores it in the IP register. Pops the parameters. The stack has now been returned to its previous state. Execution continues with the instruction in the IP register. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 17 / 38
Handling Function Calls The Stack Other Stuff Function Parameters Return Address Pop the local variables Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 18 / 38
Handling Function Calls The Stack Other Stuff Function Parameters Pop the return address Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 18 / 38
Handling Function Calls The Stack Other Stuff Pop the function parameters Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 18 / 38
Outline 1 Stacks 2 The Stack Interface 3 Stack Applications Function Calls Infix, Postfix, and Prefix Notation Infix Expression Evaluation Postfix Expressions 4 Assignment Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 19 / 38
Infix Notation An infix expression is an arithmetic expression in which the binary operators are written in between the operands. For example, to add 3 and 4, we write 3 + 4. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 20 / 38
Postfix Expressions In a postfix expression, the operator is written after the operands. For example, to add 3 and 4, we write 3 4 +. The infix expression 2 3 + 4 5 would be written as 2 3 4 5 + in postfix notation. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 21 / 38
Prefix Expressions In a prefix expression, the operator is written before the operands. For example, to add 3 and 4, we write + 3 4. The infix expression 2 3 + 4 5 would be written as + 2 3 4 5 in prefix notation. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 22 / 38
Outline 1 Stacks 2 The Stack Interface 3 Stack Applications Function Calls Infix, Postfix, and Prefix Notation Infix Expression Evaluation Postfix Expressions 4 Assignment Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 23 / 38
Fully Parenthesized Infix Expressions With infix expressions, the operations are not necessarily performed from left to right. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 24 / 38
Fully Parenthesized Infix Expressions With infix expressions, the operations are not necessarily performed from left to right. Infix expressions may require parentheses to specify the order of operation. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 24 / 38
Fully Parenthesized Infix Expressions With infix expressions, the operations are not necessarily performed from left to right. Infix expressions may require parentheses to specify the order of operation. Precedence and associativity rules allow us to omit some of the parentheses. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 24 / 38
Fully Parenthesized Infix Expressions With infix expressions, the operations are not necessarily performed from left to right. Infix expressions may require parentheses to specify the order of operation. Precedence and associativity rules allow us to omit some of the parentheses. A fully parenthesized expression requires no precedence or associativity rules. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 24 / 38
Fully Parenthesized Infix Expressions With infix expressions, the operations are not necessarily performed from left to right. Infix expressions may require parentheses to specify the order of operation. Precedence and associativity rules allow us to omit some of the parentheses. A fully parenthesized expression requires no precedence or associativity rules. In a fully parenthesized expression, there is a pair of parentheses for every operator. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 24 / 38
Examples The expression 1 + 2 3 would be fully parenthesized as 1 + 2 3)). The expression 2 3 + 4/5 6 would be fully parenthesized as 2 3) + 4/5)) 6). Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 25 / 38
Infix Expression Evaluation We may use a pair of stacks to evaluate a fully parenthesized infix expression. The expression contains four types of token: Left parenthesis Right parenthesis ) Number, e.g., 123 Operator +,,, / Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 26 / 38
Infix Expression Evaluation To evaluate the expression we need a stack of numbers and a stack of operators. Read the tokens from left to right and process them as follows: Token Left parenthesis Number Operator Right Parenthesis Action No action Push the number onto the number stack Push the operator onto the operator stack 1. Pop two numbers off the number stack 2. Pop one operator off the operator stack 3. Perform the operation on the numbers 4. Push the result onto the number stack Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 27 / 38
Example Use the algorithm to evaluate the expression 2 5) + 6/3)) 8) Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 28 / 38
Example Robb T. Koether Hampden-Sydney College) Stacks and caption their Applications Fri, Mar 16, 2018 29 / 38 Token Number Stack Operator Stack Begin with an empty stack
Robb T. Koether Hampden-Sydney College) Stacks and caption their Applications Fri, Mar 16, 2018 29 / 38 Example Token Number Stack Operator Stack 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stacks and caption their Applications Fri, Mar 16, 2018 29 / 38 Example Token Number Stack Operator Stack 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stacks and caption their Applications Fri, Mar 16, 2018 29 / 38 Example Token Number Stack Operator Stack 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stacks and caption their Applications Fri, Mar 16, 2018 29 / 38 Example Token Number Stack Operator Stack 2 2 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stacks and caption their Applications Fri, Mar 16, 2018 29 / 38 Example Token Number Stack Operator Stack 2 2 2 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stacks and caption their Applications Fri, Mar 16, 2018 29 / 38 Example Token Number Stack Operator Stack 2 2 2 5 2 5 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stacks and caption their Applications Fri, Mar 16, 2018 29 / 38 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stacks and caption their Applications Fri, Mar 16, 2018 29 / 38 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stacks and caption their Applications Fri, Mar 16, 2018 29 / 38 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 10 + 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stacks and caption their Applications Fri, Mar 16, 2018 29 / 38 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 10 + 6 10 6 + 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stacks and caption their Applications Fri, Mar 16, 2018 29 / 38 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 10 + 6 10 6 + / 10 6 + / 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stacks and caption their Applications Fri, Mar 16, 2018 29 / 38 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 10 + 6 10 6 + / 10 6 + / 3 10 6 3 + / 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stacks and caption their Applications Fri, Mar 16, 2018 29 / 38 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 10 + 6 10 6 + / 10 6 + / 3 10 6 3 + / ) 10 2 + 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stacks and caption their Applications Fri, Mar 16, 2018 29 / 38 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 10 + 6 10 6 + / 10 6 + / 3 10 6 3 + / ) 10 2 + ) 12 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stacks and caption their Applications Fri, Mar 16, 2018 29 / 38 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 10 + 6 10 6 + / 10 6 + / 3 10 6 3 + / ) 10 2 + ) 12 12 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stacks and caption their Applications Fri, Mar 16, 2018 29 / 38 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 10 + 6 10 6 + / 10 6 + / 3 10 6 3 + / ) 10 2 + ) 12 12 8 12 8 2 5) + 6/3)) 8)
Robb T. Koether Hampden-Sydney College) Stacks and caption their Applications Fri, Mar 16, 2018 29 / 38 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 + 10 + 10 + 6 10 6 + / 10 6 + / 3 10 6 3 + / ) 10 2 + ) 12 12 8 12 8 ) 4 2 5) + 6/3)) 8)
Infix Expression Evaluation Run the program InfixEvalFullParen.cpp. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 30 / 38
Outline 1 Stacks 2 The Stack Interface 3 Stack Applications Function Calls Infix, Postfix, and Prefix Notation Infix Expression Evaluation Postfix Expressions 4 Assignment Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 31 / 38
Postfix Expression Evaluation Example Postfix Expressions) Expression: 3 4 + 5 6 +. Left operand of is 3 4 +. Right operand of is 5 6 +. In postfix expressions, parentheses are never needed! Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 32 / 38
Postfix Expression Evaluation To evaluate a postfix expression we need a stack of numbers. Read the tokens from left to right and process them as follows: Token Number Operator Action Push the number onto the number stack 1. Pop two numbers off the number stack 2. Pop one operator off the operator stack 3. Perform the operation on the numbers 4. Push the result onto the number stack Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 33 / 38
Postfix Expression Evaluation Example Postfix Expressions) The fully parenthesized infix expression 2 5) + 6/3)) 8) can be written as 2 5 + 6/3 8 As a postfix expression, it is 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 34 / 38
Example Token 2 2 Number Stack 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 35 / 38
Example Token 2 2 5 2 5 Number Stack 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 35 / 38
Example Token 2 2 5 2 5 10 Number Stack 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 35 / 38
Example Token 2 2 5 2 5 10 6 10 6 Number Stack 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 35 / 38
Example Token Number Stack 2 2 5 2 5 10 6 10 6 3 10 6 3 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 35 / 38
Example Token Number Stack 2 2 5 2 5 10 6 10 6 3 10 6 3 / 10 2 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 35 / 38
Example Token Number Stack 2 2 5 2 5 10 6 10 6 3 10 6 3 / 10 2 + 12 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 35 / 38
Example Token Number Stack 2 2 5 2 5 10 6 10 6 3 10 6 3 / 10 2 + 12 8 12 8 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 35 / 38
Example Token Number Stack 2 2 5 2 5 10 6 10 6 3 10 6 3 / 10 2 + 12 8 12 8 4 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 35 / 38
Postfix Expression Evaluation Run the program PostfixEvaluator.cpp. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 36 / 38
Outline 1 Stacks 2 The Stack Interface 3 Stack Applications Function Calls Infix, Postfix, and Prefix Notation Infix Expression Evaluation Postfix Expressions 4 Assignment Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 37 / 38
Assignment Assignment Read Sections 18.1-18.2, 18.7-18.8. Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018 38 / 38