1 Stacks Stack Applications Evaluating Postfix Expressions Introduction to Project 2 Reading: L&C Section 3.2, 3.4-3.8.

Slides:



Advertisements
Similar presentations
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Advertisements

Stacks Chapter 11.
Prefix, Postfix, Infix Notation
COSC 2006 Chapter 7 Stacks III
COMPSCI 105 S Principles of Computer Science 13 Stacks.
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.
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
Topic 15 Implementing and Using Stacks
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
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.
Introduction to Stacks What are Stacks? Stack implementation using arrays. Stack application.
Infix, Postfix, Prefix.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
Postfix notation. About postfix notation Postfix, or Reverse Polish Notation (RPN) is an alternative to the way we usually write arithmetic expressions.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Topic 15 Implementing and Using Stacks
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
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.
Topic 3 The Stack ADT.
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Stack Applications.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
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.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
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 Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Stacks Stack Abstract Data Type (ADT) Stack ADT Interface
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Stacks Chapter 5.
Stacks Chapter 6.
Recap: Solution of Last Lecture Activity
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
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
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
Stack Applications Lecture 29 Thu, Apr 5, /23/2019
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stacks.
Topic 15 Implementing and Using Stacks
(Part 2) Infix, Prefix & Postfix
Introduction to Stacks
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Introduction to Stacks
Data Structures and Algorithms 2/2561
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
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.
© 2016 Pearson Education, Ltd. All rights reserved.
CHAPTER 3: Collections—Stacks
Presentation transcript:

1 Stacks Stack Applications Evaluating Postfix Expressions Introduction to Project 2 Reading: L&C Section 3.2,

2 A Conceptual View of a Stack Top of Stack Adding an ElementRemoving an Element

3 Applications for a Stack A stack can be used as an underlying mechanism for many common applications –Evaluate postfix and prefix expressions –Reverse the order of a list of elements –Support an “undo” operation in an application –Backtrack in solving a maze

4 Evaluating Infix Expressions Traditional arithmetic expressions are written in infix notation (aka algebraic notation) (operand) (operator) (operand) (operator) (operand) * 2 When evaluating an infix expression, we need to use the precedence of operators –The above expression evaluates to 4 + (5 * 2) = 14 –NOT in left to right order as written (4 + 5) * 2 = 18 We use parentheses to override precedence

5 Evaluating Postfix Expressions Postfix notation is an alternative method to represent the same expression (operand) (operand) (operand) (operator) (operator) * + When evaluating a postfix expression, we do not need to know the precedence of operators Note: We do need to know the precedence of operators to convert an infix expression to its corresponding postfix expression

6 Evaluating Postfix Expressions We can process from left to right as long as we use the proper evaluation algorithm Postfix evaluation algorithm calls for us to: –Push each operand onto the stack –Execute each operator on the top element(s) of the stack (An operator may be unary or binary and execution may pop one or two values off the stack) –Push result of each operation onto the stack

7 Evaluating Postfix Expressions Expression = * / * * / *

8 Evaluating Postfix Expressions Core of evaluation algorithm using a stack while (tokenizer.hasMoreTokens()) { token = tokenizer.nextToken(); // returns String if (isOperator(token) { int op2 = (stack.pop()).intValue(); // Integer int op1 = (stack.pop()).intValue(); // to int int res = evalSingleOp(token.charAt(0), op1, op2); stack.push(new Integer(res)); } else // String to int to Integer conversion here stack.push (new Integer(Integer.parseint(token))); } // Note: Textbook’s code does not take advantage of // Java 5.0 auto-boxing and auto-unboxing

9 Evaluating Postfix Expressions Instead of this: int op2 = (stack.pop()).intValue(); // Integer to int int op1 = (stack.pop()).intValue(); // Integer to int int res = evalSingleOp(token.charAt(0), op1, op2); Why not this: int res = evalSingleOp(token.charAt(0), (stack.pop()).intValue(), (stack.pop()).intValue()); In which order are the parameters evaluated? Affects order of the operands to evaluation

10 Evaluating Postfix Expressions The parameters to the evalSingleOp method are evaluated in left to right order The pops of the operands from the stack occur in the opposite order from the order assumed in the interface to the method Results:OriginalAlternative 6 3 / = 26 3 / = / = 03 6 / = 2

11 Evaluating Postfix Expressions Our consideration of the alternative code above demonstrates a very good point Be sure that your code keeps track of the state of the data stored on the stack Your code must be written consistent with the order data will be retrieved from the stack to use the retrieved data correctly

12 The java.util.Stack Class The java.util.Stack class is part of the Java collections API. It is a subclass of the java.util.Vector class which is a subclass of java.util.AbstractList java.util.Stack inherits some methods from its superclasses that are inappropriate for a stack collection – a bad design decision! A good programmer will avoid using those methods on a Stack object

13 The java.util.Stack Class java.util.Stack has a search method that returns the distance from the top of the stack to the first occurrence of that object on the stack, e.g. search returns 1 for the top element of the stack If the object is not found, search returns -1 The search method is O(n) Does this method violate the principles of a stack as a data structure? (Discussion)

Introduction to Project 2 Mazes are a traditional form of puzzle The maze in this project is a bit unique

15

Introduction to Project 2 You are provided partial code for the four classes shown in the UML class diagram The code you need to write: –Add code to Maze constructor to build a queue of Bird objects and save it as the bird queue for each Bird in the maze –Complete the Bird class getNextBird method –Complete Solve1 main method - iterative –Complete Solve2 main method - recursive