Stack Applications Lecture 29 Thu, Apr 5, /23/2019

Slides:



Advertisements
Similar presentations
INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Prefix, Postfix, Infix Notation
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
COSC 2006 Chapter 7 Stacks III
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
Arithmetic Expressions
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
Infix to postfix conversion Process the tokens from a vector infixVect of tokens (strings) of an infix expression one by one When the token is an operand.
Stacks & Queues Infix Calculator CSC 172 SPRING 2004 LECTURE 13.
Infix, Postfix, Prefix.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
1 Stacks Stack Applications Evaluating Postfix Expressions Introduction to Project 2 Reading: L&C Section 3.2,
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Stack Applications.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
CHP-3 STACKS.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
BCA II Data Structure Using C
Stacks Stack Abstract Data Type (ADT) Stack ADT Interface
Review Use of Stack Introduction Stack in our life Stack Operations
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
MEMORY REPRESENTATION OF STACKS
Stacks Chapter 7 introduces the stack data type.
CSC 172 DATA STRUCTURES.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
Stack application: postponing data usage
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
Stack Data Structure, Reverse Polish Notation, Homework 7
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Lecture No.07 Data Structures Dr. Sohail Aslam
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Stack Applications Lecture 29 Thu, Apr 1, /29/2019 Stacks.
Applications of Stacks and Queues
Data Structures and Algorithms 2/2561
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
LINEAR DATA STRUCTURES
DATA STRUCTURES IN PYTHON
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

Stack Applications Lecture 29 Thu, Apr 5, 2007 2/23/2019

Infix Expressions 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. 2/23/2019 Stack Applications

Postfix Expressions A postfix expression with one (binary) operator is written in the order: left-operand, right-operand, operator. For example, to add 3 and 4, we write 3 4 +. 2/23/2019 Stack Applications

Prefix Expressions A prefix expression with one binary operator is written in the order: operator, left-operand, right-operand. For example, to add 3 and 4, we write + 3 4. 2/23/2019 Stack Applications

Postfix Expression Evaluation In expression with several operators, each operand is a subexpression Example: 3 4 + 5 6 + * Left operand of * is 3 4 + Right operand of * is 5 6 + Parentheses are never needed! 2/23/2019 Stack Applications

Postfix Expression Evaluation Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. 2/23/2019 Stack Applications

Postfix Expression Evaluation Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. 1 2 + 2/23/2019 Stack Applications

Postfix Expression Evaluation Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. 1 2 + 3 * 2/23/2019 Stack Applications

Postfix Expression Evaluation Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. 1 2 + 3 * 4 2/23/2019 Stack Applications

Postfix Expression Evaluation Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. 1 2 + 3 * 4 5 6 + 2/23/2019 Stack Applications

Postfix Expression Evaluation Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. 1 2 + 3 * 4 5 6 +7 8 * 2/23/2019 Stack Applications

Postfix Expression Evaluation Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. 1 2 + 3 * 4 5 6 +7 8 * – 2/23/2019 Stack Applications

Postfix Expression Evaluation Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. 1 2 + 3 * 4 5 6 +7 8 * – / 2/23/2019 Stack Applications

Postfix Expression Evaluation Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. 1 2 + 3 * 4 5 6 +7 8 * – / – 2/23/2019 Stack Applications

Postfix Expression Evaluation Rewrite the infix expression ((1 + 2)*3 – 4/(5 + 6 – 7*8))*9 as a postfix expression. 1 2 + 3 * 4 5 6 +7 8 * – / – 9 * 2/23/2019 Stack Applications

Postfix Expression Evaluation Begin with an empty stack. Each number or operator is a token. Process the tokens in the postfix expression from left to right. 2/23/2019 Stack Applications

Postfix Expression Evaluation If the token is a number, Push it onto the stack. If the token is a binary operator, Pop two numbers off the stack. Combine them under the operator. Push the result onto the stack. The single remaining value on the stack is the value of the expression. 2/23/2019 Stack Applications

Postfix Expression Evaluation Evaluate 3 4 + 5 6 + * Read 3: Push 3 Read 4: Push 4 Read +: Pop 4, pop 3, compute 3 + 4 = 7, push 7 Read 5: Push 5 Read 6: Push 6 Read +: Pop 6, pop 5, compute 5 + 6 = 11, push 11 Read *: Pop 11, pop 7, compute 7 * 11 = 77, push 77 2/23/2019 Stack Applications

PostfixEvaluator.cpp PostfixEvaluator.cpp 2/23/2019 Stack Applications

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. 2/23/2019 Stack Applications

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. 2/23/2019 Stack Applications

The Restaurant Triangle Puzzle While waiting for your order in a restaurant, you pick up and play the Triangle Puzzle. 2/23/2019 Stack Applications

The Restaurant 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. 2/23/2019 Stack Applications

The Restaurant Triangle Puzzle The goal is to remove all but one peg. 2/23/2019 Stack Applications

The Restaurant Triangle Puzzle There are 18 possible moves, each in either of 2 directions. 2/23/2019 Stack Applications

The Restaurant Triangle Puzzle Six moves in each direction along this diagonal. 2/23/2019 Stack Applications

The Restaurant Triangle Puzzle Six moves in each direction along this diagonal. 2/23/2019 Stack Applications

The Restaurant Triangle Puzzle Six moves in each direction along this diagonal. 2/23/2019 Stack Applications

The Restaurant Triangle Puzzle Six moves in each direction along this diagonal. 2/23/2019 Stack Applications

The Restaurant Triangle Puzzle Six moves in each direction horizontally. 2/23/2019 Stack Applications

The Restaurant Triangle Puzzle Six moves in each direction horizontally. 2/23/2019 Stack Applications

The Restaurant Triangle Puzzle List the 36 possible moves in a particular order. The program will systematically try the moves until it finds a valid move. Then it will Make that move. Push that move onto the stack. 2/23/2019 Stack Applications

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. 2/23/2019 Stack Applications

The Restaurant Triangle Puzzle TrianglePuzzle.cpp 2/23/2019 Stack Applications

Solving a Maze A computer program can solve a maze by using trial and error. Systematically try each open path. When a dead-end is reached, back up to the most recent alternative path. 2/23/2019 Stack Applications

Solving a Maze Push the starting square onto the stack. For the square on top of the stack, attempt to move to an adjacent square. Test the adjacent squares in the following order. Up Down Left Right 2/23/2019 Stack Applications

Solving a Maze When a move is possible, Push the new square onto the stack. When no move is possible (dead-end), Repeatedly pop squares off the stack until an untried alternate move is available. When the final square is reached, the stack contains the path (in reverse order) from start to finish. 2/23/2019 Stack Applications