Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Similar documents
Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Stacks and their Applications

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

The x86 Instruction Set

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Scheduling and Digraphs

Formal Languages and Automata Theory, SS Project (due Week 14)

The Critical-Path Algorithm

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

Recursive Descent Parsers

Minimal Spanning Trees

Scope and Parameter Passing

LR Parsing - Conflicts

The Pairwise-Comparison Method

List Iterator Implementation

March 13/2003 Jayakanth Srinivasan,

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Sampling Distribution Examples Sections 15.4, 15.5

STACKS AND QUEUES. Problem Solving with Computers-II

Stack Abstract Data Type

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Stacks Calculator Application. Alexandra Stefan

The x86 Architecture

Binary Tree Applications

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

The Decreasing-Time Algorithm

The Class Construct Part 1

Magnification and Minification

The Traveling Salesman Problem Brute Force Method

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Friends and Unary Operators

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Lecture 4: Stack Applications CS2504/CS4092 Algorithms and Linear Data Structures. Parentheses and Mathematical Expressions

Scope and Parameter Passing

Stack and Its Implementation

PA3 Design Specification

SimpleCalc. which can be entered into a TI calculator, like the one on the right, like this:

Programming Languages

Solving Recursive Sequences by Iteration

The x87 Floating-Point Unit

Rotations and Translations

Recognition of Tokens

Function Definition Syntax Tree

Building the Abstract Syntax Trees

The Stack and Queue Types

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

The Plurality-with-Elimination Method

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

LR Parsing - The Items

An Introduction to Trees

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

Shading Triangles. Lecture 37. Robb T. Koether. Hampden-Sydney College. Mon, Nov 30, 2015

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

UNIT 3

The Graphics Pipeline

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Abstract Syntax Trees Synthetic and Inherited Attributes

Dynamic Allocation of Memory

Lecture 12 ADTs and Stacks

Lecture Chapter 6 Recursion as a Problem Solving Technique

The Graphics Pipeline

DTDs and XML Attributes

CS 206 Introduction to Computer Science II

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Dynamic Allocation of Memory

Infix to Postfix Conversion

Lab 7 1 Due Thu., 6 Apr. 2017

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Density Curves Sections

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES

Recursive Linked Lists

Linear Data Structure

Lecture 8: Simple Calculator Application

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

Street-Routing Problems

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Implementing Linked Lists

Programming Languages

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Monday, November 13, 2017

12 Abstract Data Types

Ambient and Diffuse Light

Fundamental Data Types

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Lecture Data Structure Stack

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Transcription:

Stack Applications Lecture 25 Sections 18.7-18.8 Robb T. Koether Hampden-Sydney College Mon, Mar 30, 2015 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 1 / 34

1 The Triangle Puzzle 2 Infix, Postfix, and Prefix Notation 3 Infix Expression Evaluation 4 Postfix Expressions 5 Function Calls 6 Assignment Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 2 / 34

Outline 1 The Triangle Puzzle 2 Infix, Postfix, and Prefix Notation 3 Infix Expression Evaluation 4 Postfix Expressions 5 Function Calls 6 Assignment Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 3 / 34

The Restaurant Triangle While waiting for your order in a restaurant, you pick up and play the Triangle Puzzle. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 4 / 34

The Triangle Puzzle There are fifteen holes. One hole is empty. The other 14 holes have pegs in them. You make moves by jumping, as in checkers. One peg jumps an adjacent peg, landing in an empty hole. Remove the jumped peg. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 5 / 34

The Restaurant Triangle Puzzle The goal is to remove all but one peg. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 6 / 34

The Restaurant Triangle Puzzle There are 36 possible moves 6 in each of 6 directions). Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 6 / 34

The Restaurant Triangle Puzzle Six moves up this diagonal. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 6 / 34

The Restaurant Triangle Puzzle Six moves down this diagonal. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 6 / 34

The Restaurant Triangle Puzzle Six moves up this diagonal. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 6 / 34

The Restaurant Triangle Puzzle Six moves down this diagonal. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 6 / 34

The Restaurant Triangle Puzzle Six moves right horizontally. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 6 / 34

The Restaurant Triangle Puzzle Six moves left horizontally. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 6 / 34

The Restaurant Triangle Puzzle List the 36 possible moves in some any) order. The program will systematically consider the moves until it finds a valid move. Then it will Make that move. Push that move onto the stack for future reference. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 7 / 34

The Restaurant Triangle Puzzle If no move if possible, then it will Pop the last move off the stack. Continue through the list of possible moves until it finds a legal move. Make that move and continue as before. When a single peg remains, the contents of the stack is a winning sequence of moves. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 8 / 34

Postfix Expression Evaluation Run the program TrianglePuzzle.cpp. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 9 / 34

Outline 1 The Triangle Puzzle 2 Infix, Postfix, and Prefix Notation 3 Infix Expression Evaluation 4 Postfix Expressions 5 Function Calls 6 Assignment Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 10 / 34

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) Stack Applications Mon, Mar 30, 2015 11 / 34

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) Stack Applications Mon, Mar 30, 2015 12 / 34

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) Stack Applications Mon, Mar 30, 2015 13 / 34

Outline 1 The Triangle Puzzle 2 Infix, Postfix, and Prefix Notation 3 Infix Expression Evaluation 4 Postfix Expressions 5 Function Calls 6 Assignment Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 14 / 34

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 assumes 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) Stack Applications Mon, Mar 30, 2015 15 / 34

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) Stack Applications Mon, Mar 30, 2015 16 / 34

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) Stack Applications Mon, Mar 30, 2015 17 / 34

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) Stack Applications Mon, Mar 30, 2015 18 / 34

Example Use the algorithm to evaluate the expression 2 5) + 6/3)) 8) Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 19 / 34

Example Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Token Number Stack Operator Stack Begin with an empty stack

Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 5) + 6/3)) 8)

Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 5) + 6/3)) 8)

Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 5) + 6/3)) 8)

Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 2 2 5) + 6/3)) 8)

Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 2 2 2 5) + 6/3)) 8)

Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 2 2 5 2 5 2 5) + 6/3)) 8)

Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 Example Token Number Stack Operator Stack 2 2 2 5 2 5 ) 10 2 5) + 6/3)) 8)

Robb T. Koether Hampden-Sydney College) Stack caption Applications Mon, Mar 30, 2015 20 / 34 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) Stack caption Applications Mon, Mar 30, 2015 20 / 34 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) Stack caption Applications Mon, Mar 30, 2015 20 / 34 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) Stack caption Applications Mon, Mar 30, 2015 20 / 34 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) Stack caption Applications Mon, Mar 30, 2015 20 / 34 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) Stack caption Applications Mon, Mar 30, 2015 20 / 34 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) Stack caption Applications Mon, Mar 30, 2015 20 / 34 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) Stack caption Applications Mon, Mar 30, 2015 20 / 34 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) Stack caption Applications Mon, Mar 30, 2015 20 / 34 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) Stack caption Applications Mon, Mar 30, 2015 20 / 34 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) Stack Applications Mon, Mar 30, 2015 21 / 34

Outline 1 The Triangle Puzzle 2 Infix, Postfix, and Prefix Notation 3 Infix Expression Evaluation 4 Postfix Expressions 5 Function Calls 6 Assignment Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 22 / 34

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) Stack Applications Mon, Mar 30, 2015 23 / 34

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) Stack Applications Mon, Mar 30, 2015 24 / 34

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) Stack Applications Mon, Mar 30, 2015 25 / 34

Example Token 2 2 Number Stack 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 26 / 34

Example Token 2 2 5 2 5 Number Stack 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 26 / 34

Example Token 2 2 5 2 5 10 Number Stack 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 26 / 34

Example Token 2 2 5 2 5 10 6 10 6 Number Stack 2 5 6 3 / + 8 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 26 / 34

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) Stack Applications Mon, Mar 30, 2015 26 / 34

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) Stack Applications Mon, Mar 30, 2015 26 / 34

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) Stack Applications Mon, Mar 30, 2015 26 / 34

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) Stack Applications Mon, Mar 30, 2015 26 / 34

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) Stack Applications Mon, Mar 30, 2015 26 / 34

Postfix Expression Evaluation Run the program PostfixEvaluator.cpp. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 27 / 34

Outline 1 The Triangle Puzzle 2 Infix, Postfix, and Prefix Notation 3 Infix Expression Evaluation 4 Postfix Expressions 5 Function Calls 6 Assignment Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 28 / 34

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) Stack Applications Mon, Mar 30, 2015 29 / 34

Handling Function Calls The Stack Other Stuff Begin with the current stack Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 30 / 34

Handling Function Calls The Stack Other Stuff Function Parameters Push the function parameters Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 30 / 34

Handling Function Calls The Stack Other Stuff Function Parameters Return Address Push the return address Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 30 / 34

Handling Function Calls The Stack Other Stuff Function Parameters Return Address Local Variables Push the local variables Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 30 / 34

Handling Function Calls When a function returns, the program Pops and discards) the values of the local variables. Pops the return address and stores it in the IP register). Branches to the return address. Pops the parameters. The stack has now been returned to its previous state. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 31 / 34

Handling Function Calls The Stack Other Stuff Function Parameters Return Address Pop the local variables Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 32 / 34

Handling Function Calls The Stack Other Stuff Function Parameters Pop the return address Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 32 / 34

Handling Function Calls The Stack Other Stuff Pop the function parameters Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 32 / 34

Outline 1 The Triangle Puzzle 2 Infix, Postfix, and Prefix Notation 3 Infix Expression Evaluation 4 Postfix Expressions 5 Function Calls 6 Assignment Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 33 / 34

Assignment Assignment Read Sections 18.7-18.8. Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 34 / 34