Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Sample PMT online… Browse 1120/sumII05/PMT/2004_1/ 1120/sumII05/PMT/2004_1/
Stacks Chapter 11.
Prefix, Postfix, Infix Notation
Stacks Chapter 21 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
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.
Chapter 3 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.
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
1 Stacks Stack Applications Evaluating Postfix Expressions Introduction to Project 2 Reading: L&C Section 3.2,
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
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.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
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.
Chapter 6 Stacks CS Data Structures Mehmet H Gunes Modified from authors’ slides.
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.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Data Structures and Abstractions with Java, 4e Frank Carrano
Stacks  Introduction  Applications  Implementations  Complex Applications.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 14 Stacks.
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.
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java,
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.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Stacks Stack Abstract Data Type (ADT) Stack ADT Interface
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Stacks Chapter 5.
Stacks Chapter 6.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
CSE 373: Data Structures and Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
COMPUTER 2430 Object Oriented Programming and Data Structures I
Introduction to Data Structures
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
Infix to Postfix Conversion
Stacks.
Stacks Chapter 5.
Introduction to Stacks
Stacks.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
Stack Implementations
© 2016 Pearson Education, Ltd. All rights reserved.
A List Implementation that Links Data
Queues, Deques, and Priority Queues
Presentation transcript:

Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano

Stacks FIGURE 5-1 Some familiar stacks Add item on top of stack Remove item that is topmost  Last In, First Out … LIFO © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Specifications of the ADT Stack © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Specifications of the ADT Stack © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Design Decision When stack is empty  What to do with pop and peek ? Possible actions  Assume that the ADT is not empty;  Return null.  Throw an exception (which type?). © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Interface LISTING 5-1 An interface for the ADT stack © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Interface LISTING 5-1 An interface for the ADT stack © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Example FIGURE 5-2 A stack of strings after (a) push adds Jim; (b) push adds Jess; (c) push adds Jill; (d) push adds Jane; (e) push adds Joe; (f ) pop retrieves and removes Joe; (g) pop retrieves and removes Jane © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Security Note Design guidelines  Use preconditions and postconditions to document assumptions.  Do not trust client to use public methods correctly.  Avoid ambiguous return values.  Prefer throwing exceptions instead of returning values to signal problem. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Processing Algebraic Expressions Infix: each binary operator appears between its operands a + b Prefix: each binary operator appears before its operands + a b Postfix: each binary operator appears after its operands a b + Balanced expressions: delimiters paired correctly © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Processing Algebraic Expressions FIGURE 5-3 The contents of a stack during the scan of an expression that contains the balanced delimiters { [ ( ) ] } © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Processing Algebraic Expressions FIGURE 5-4 The contents of a stack during the scan of an expression that contains the unbalanced delimiters { [ ( ] ) } © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Processing Algebraic Expressions FIGURE 5-5 The contents of a stack during the scan of an expression that contains the unbalanced delimiters [ ( ) ] } © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Processing Algebraic Expressions FIGURE 5-6 The contents of a stack during the scan of an expression that contains the unbalanced delimiters { [ ( ) ] © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Processing Algebraic Expressions Algorithm to process for balanced expression. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Processing Algebraic Expressions Algorithm to process for balanced expression. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Java Implementation LISTING 5-2 The class BalanceChecker © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Java Implementation LISTING 5-2 The class BalanceChecker © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Java Implementation LISTING 5-2 The class BalanceChecker © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Infix to Postfix FIGURE 5-7 Converting the infix expression a + b * c to postfix form © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Successive Operators with Same Precedence FIGURE 5-8 Converting an infix expression to postfix form: (a) a - b + c; © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Successive Operators with Same Precedence FIGURE 5-8 Converting an infix expression to postfix form: a ^ b ^ c © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Infix-to-postfix Conversion © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Infix-to-postfix Algorithm © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Infix-to-postfix Algorithm © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Infix-to-postfix Algorithm © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Infix to Postfix FIGURE 5-9 The steps in converting the infix expression a / b * (c + (d - e)) to postfix form © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Evaluating Postfix Expressions FIGURE 5-10 The stack during the evaluation of the postfix expression a b / when a is 2 and b is 4 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Evaluating Postfix Expressions FIGURE 5-11 The stack during the evaluation of the postfix expression a b + c / when a is 2, b is 4, and c is 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Evaluating Postfix Expressions Algorithm for evaluating postfix expressions. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Evaluating Postfix Expressions Algorithm for evaluating postfix expressions. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Evaluating Infix Expressions FIGURE 5-12 Two stacks during the evaluation of a + b * c when a is 2, b is 3, and c is 4: (a) after reaching the end of the expression; © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Evaluating Infix Expressions FIGURE 5-12 Two stacks during the evaluation of a + b * c when a is 2, b is 3, and c is 4: (b) while performing the multiplication; © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Evaluating Infix Expressions FIGURE 5-12 Two stacks during the evaluation of a + b * c when a is 2, b is 3, and c is 4: (c) while performing the addition © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Evaluating Infix Expressions Algorithm to evaluate infix expression. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Evaluating Infix Expressions Algorithm to evaluate infix expression. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Evaluating Infix Expressions Algorithm to evaluate infix expression. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Evaluating Infix Expressions Algorithm to evaluate infix expression. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The Program Stack FIGURE 5-13 The program stack at three points in time: (a) when main begins execution; (b) when methodA begins execution; (c) when methodB begins execution © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Java Class Library: The Class Stack  Found in java.util Methods  A constructor – creates an empty stack  public T push(T item);  public T pop();  public T peek();  public boolean empty(); © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

End Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.