Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.

Slides:



Advertisements
Similar presentations
Stacks.
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
CHAPTER 4 Queues. Queue  The queue, like the stack, is a widely used data structure  A queue differs from a stack in one important way  A stack is.
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
CS Data Structures II Review COSC 2006 April 14, 2017
Abstract Data Types (ADT) Collection –An object that can hold a list of other objects Homogeneous Collection –Contains elements all of the same type –Example:
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.
ITEC200 Week05 Stacks. 2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface.
CHAPTER 3 Stacks. Chapter Objectives  To learn about the stack data type and how to use its four methods:  push  pop  peek  empty  To understand.
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.
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.
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.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
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,
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
CST Razdan et al Razdan with contribution from others 1 Chapter 6 Stacks Anshuman Razdan Div of Computing.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007.
Definition Stack is an ordered collection of data items in which access is possible only at one end (called the top of the stack). Stacks are known.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Chapter 3 Stacks.
CHAPTER 3 Stacks MIDTERM OCTOBER 17 IN LAB. Chapter Objectives  To learn about the stack data type and how to use its four methods:  push  pop  peek.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 11: Stacks and Mazes Announcements 1.Midterm.
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.
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.Stack.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
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.
Data Structures and Algorithms
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Information and Computer Sciences University of Hawaii, Manoa
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.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture6.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Click to edit Master text styles Stacks Data Structure.
Elementary Data Structures
Stacks Chapter 5.
MIDTERM OCTOBER 17 IN LAB Chapter 3 Stacks.
Stacks.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Stacks.
Chapter 5 Stacks.
Stack Data Structure, Reverse Polish Notation, Homework 7
5.4 Additional Stack Applications
MIDTERM OCTOBER 11 IN LAB Chapter 3 Stacks.
CSC 1052 Stacks 1/11/2019.
Stacks and Queues 1.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stacks.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
5.3 Implementing a Stack Chapter 5 – The Stack.
CHAPTER 3: Collections—Stacks
Presentation transcript:

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 calculator CS340 1

Implementing a Stack 2 CS340

Implementing a Stack as an Extension of Vector  Part of the package java.util : public class Stack extends Vector  Vector: a growable array of objects CS340 3

Implementing a Stack as an Extension of Vector (cont.)  We can use Vector 's add method to implement push : public E push(obj E) { add(obj); return obj; }  pop can be coded as public E throws EmptyStackException { try { return remove (size() – 1); } catch (ArrayIndexOutOfBoundsException ex) { throw new EmptyStackException(); } CS340 4

Implementing a Stack as an Extension of Vector (cont.)  All of Vector operations can be applied to a Stack  Such as searches and access by index  This violates the principle of information hiding CS340 5

A little about information hiding  Principle:  Hide internal details of a component from other components  Why?  Prevent damage from wrong external code  Make components easier to understand/use  Simplify modification and repair  Facilitate re-use CS340 6

Implementing a Stack with a List Component  ListStack : has a List component  We can use ArrayList, Vector, or the LinkedList classes to implement the List interface.  push method: public E push(E obj) { theData.add(obj); return obj; }  Adapter class: stack in this case is adapter class of List  Method delegation: from stack to list CS340 7

Implementing a Stack with a List Component (cont.) public class ListStack implements Stack { private List theData; public ListStack( ) { theData = new ArrayList (); public E push( E obj ) { theData.add(obj); return obj; } CS340 8

9 Implementing a Stack with a List Component public E pop( ) { if( empty( ) ) throw new EmptyStackException( "ListStack pop" ); return theData.remove(theData.size()-1); public E peek( ) { if( empty( ) ) throw new EmptyStackException( "ListStack top" ); return theData.get(theData.size()-1); }

Implementing a Stack with a List Component public boolean empty( ) { return(theData.size() == 0); } CS340 10

Implementing a Stack Using an Array  If we implement a stack as an array, we would need... public class ArrayStack implements StackInt { private E[] theData; int topOfStack = -1; private static final int INITIAL_CAPACITY = public ArrayStack() { theData = (E[])new Object[INITIAL_CAPACITY]; } Allocate storage for an array with a default capacity Keep track of the top of the stack We do not need a size variable or method CS340 11

Implementing a Stack Using an Array (cont.) ArrayStack theData = topOfStack = -1 Object[] [0] = null [1] = null [2] = null [3] = null [4] = null [5] = null [6] = null [7] = null [8] = null [9] = null public E push(E obj) { if (topOfStack == theData.length - 1){ reallocate(); } topOfStack++; theData[topOfStack] = obj; return obj; } 0 Character value = 'J' 1 Character value = 'a' Character value = 'v' 2 Character value = 'a' 3 12

Implementing a Stack Using an Array public E pop() { if (empty()) { throw new EmptyStackException(); } return theData[topOfStack--]; } CS  This implementation is O(1)

Implementing a Stack as a Linked Data Structure  We can also implement a stack using a linked list of nodes It is easiest to insert and delete from the head of a list push inserts a node at the head and pop deletes the node at the head when the list is empty, pop returns null 14

Implementing a Stack as a Linked Data Structure (cont.) CS public class LinkedStack implements Stack { // Data fields Private Node topOfStackRef = null; // Methods: push, pop, peek, empty }

Comparison of Stack Implementations  Extending a Vector : poor choice for stack implementation  The easiest implementation uses a List component ( ArrayList is the simplest) for storing data  Array requires reallocation of space when the array becomes full, and  Linked data structure requires allocating storage for links  All insertions and deletions occur at one end: constant time, O(1) CS340 16

Additional Stack Applications 17 CS340

Additional Stack Applications  Postfix and infix notation  Expressions normally are written in infix form, but  it easier to evaluate an expression in postfix form since there is no need to group sub-expressions in parentheses or worry about operator precedence 18

Evaluating Postfix Expressions  Write a class that evaluates a postfix expression  Use the space character as a delimiter between tokens CS340 19

Evaluating Postfix Expressions (cont.) 1. create an empty stack of integers 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the character on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 7-20* 4 44 CS340 20

Evaluating Postfix Expressions (cont.) 1. create an empty stack of integers 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the character on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 7-20* CS340 21

Evaluating Postfix Expressions (cont.) 1. create an empty stack of integers 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the character on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 7-20* * 7 CS340 22

Evaluating Postfix Expressions (cont.) 1. create an empty stack of integers 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the character on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 7-20* CS340 23

Evaluating Postfix Expressions (cont.) 1. create an empty stack of integers 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the character on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 7-20* CS340 24

Evaluating Postfix Expressions (cont.) 1. create an empty stack of integers 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the character on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 7-20* CS340 25

Evaluating Postfix Expressions (cont.) 1. create an empty stack of integers 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the character on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 7-20* CS340 26

Evaluating Postfix Expressions (cont.) 1. create an empty stack of integers 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the number on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 7-20*447 8 CS340 27

Evaluating Postfix Expressions (cont.) CS  Listing 3.6 ( PostfixEvaluator.java, pages )

Evaluating Postfix Expressions (cont.)  Testing: write a driver which  creates a PostfixEvaluator object  reads one or more expressions and report the result  catches PostfixEvaluator.SyntaxErrorException  exercises each path by using each operator  exercises each path through the method by trying different orderings and multiple occurrences of operators  tests for syntax errors: an operator without any operands a single operand an extra operand an extra operator a variable name the empty string CS340 29