CHAPTER 3: Collections—Stacks

Slides:



Advertisements
Similar presentations
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
Advertisements

Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Chapter 3 Collections. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 3-2 Chapter Objectives Define the concept and terminology related.
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,
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,
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.
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. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Chapter 3 Stacks.
Chapter 12 Introduction to Collections - Stacks
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.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
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.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 3 Introduction to Collections – Stacks Modified
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 3: Collections Java Software Structures: Designing and Using.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
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)
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 5: Queues Java Software Structures: Designing and Using Data.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
Collections Chapter 12 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
Collections Using Generics in Collections. 2 Chapter Objectives Define the concept and terminology related to collections Explore the basic structure.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
COS 312 DAY 18 Tony Gauvin. Ch 1 -2 Agenda Questions? Capstone Progress reports over due Assignment 5 Corrected – 1 A, 2 B’s, 1 C, 1 D 1 F and 1 MIA –
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
Click to edit Master text styles Stacks Data Structure.
Collections & Stacks Collections A collection is an object that holds and organizes other objects It provides operations for accessing and managing.
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
COS 312 DAY 19 Tony Gauvin. Ch 1 -2 Agenda Questions? Next Capstone progress report is April 25 Assignment 5 OVERDUE – Will be graded tomorrow Assignment.
The Queue ADT.
Stacks Stack Abstract Data Type (ADT) Stack ADT Interface
Stack: a Linked Implementation
Data Structures Using C++ 2E
CHAPTER 4: Linked Structures
Stacks Chapter 5.
Stacks Stacks.
Stacks.
Chapter 14: 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.
Chapter 3 Introduction to Collections - Stacks
Stack Data Structure, Reverse Polish Notation, Homework 7
COMPUTER 2430 Object Oriented Programming and Data Structures I
Java Software Structures: John Lewis & Joseph Chase
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Java Software Structures: John Lewis & Joseph Chase
Chapter 4 Linked Structures - Stacks
CSC 1052 Stacks 1/11/2019.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks public interface Stack { public boolean empty();
Stacks.
Stacks Chapter 5.
Stacks CS-240 Dick Steflik.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Presentation transcript:

CHAPTER 3: Collections—Stacks Java Software Structures: Designing and Using Data Structures Third Edition John Lewis & Joseph Chase Modified by Chuck Cusack, Hope College

A Stack Collection Let's look at an example of a collection A stack collection arranges elements in a last in, first out manner (LIFO) It is very similar to a stack of plates, books, etc. You may only place new elements on the top of the stack You may only remove elements from the top of the stack

The conceptual view of a stack collection

Collection Operations Every collection has a set of operations that define how we interact with it They usually include ways for the user to: add and remove elements determine if the collection is empty determine the collection's size

The operations on a stack collection

The StackADT interface /** * @author Lewis and Chase * * Defines the interface to a stack data structure. */ package jss2;   public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed * onto stack public void push (T element); /** Removes and returns the top element * from this stack. * @return T element removed from the * top of the stack public T pop(); /** Returns without removing the top element * of this stack. * @return T element on top of the stack */ public T peek(); /** Returns true if this stack contains no elements. * @return boolean whether or not this stack is empty public boolean isEmpty();   /** Returns the number of elements in this stack. * @return int number of elements in this stack public int size(); /** Returns a string representation of this stack. * @return String representation of this stack public String toString(); }

Using Stacks Stacks are particularly helpful when solving certain types of problems Consider the undo operation in an application keeps track of the most recent operations in reverse order

Postfix Expressions Let's examine a program that uses a stack to evaluate postfix expressions In a postfix expression, the operator comes after its two operands We generally use infix notation, with parentheses to force precedence: (3 + 4) * 2 In postfix notation, this would be written 3 4 + 2 *

Postfix Expressions To evaluate a postfix expression: scan from left to right, determining if the next token is an operator or operand if it is an operand, push it on the stack if it is an operator, pop the stack twice to get the two operands, perform the operation, and push the result onto the stack At the end, there will be one value on the stack, which is the value of the expression

Using a stack to evaluate a postfix expression

Postfix Expressions To simplify the example, let's assume the operands to the expressions are integer literals Our solution uses a LinkedStack, though any implementation of a stack would suffice Let’s look at the implementations of the Postfix and PostfixEvaluator classes in Eclipse.

A UML class diagram for the postfix expression program

Managing Capacity An array has a particular number of cells when it is created – its capacity So the array's capacity is also the stack’s capacity What do we do when the stack is full and a new element is added? We could throw an exception We could return some kind of status indicator We could automatically expand the capacity

Managing Capacity The first two options require the user of the collection to be on guard and deal with the situation as needed The third option is best, especially in light of our desire to separate the implementation from the interface The capacity is an implementation problem, and shouldn't be passed along to the user unless there is a good reason to do so

The ArrayStack Class Now let's examine an array-based implementation of a stack We'll make the following design decisions: maintain an array of generic references the bottom of the stack is at index 0 the elements of the stack are in order and contiguous an integer variable top stores the index of the next available slot in the array This approach allows the stack to grow and shrink at the higher indexes

An array implementation of a stack

ArrayStack: Fields /** * @author Lewis and Chase * * Represents an array implementation of a stack. */ package jss2; import jss2.exceptions.*; public class ArrayStack<T> implements StackADT<T> { * constant to represent the default capacity of the array private final int DEFAULT_CAPACITY = 100; * int that represents both the number of elements and the next * available position in the array private int top;   * array of generic elements to represent the stack private T[] stack;

ArrayStack: Constructors /** * Creates an empty stack using the default capacity. */ public ArrayStack() { top = 0; stack = (T[])(new Object[DEFAULT_CAPACITY]); }   * Creates an empty stack using the specified capacity. * @param initialCapacity represents the specified capacity public ArrayStack (int initialCapacity) { stack = (T[])(new Object[initialCapacity]);

ArrayStack – the push operation /** * Adds the specified element to the top of this stack, expanding * the capacity of the stack array if necessary. * @param element generic element to be pushed onto stack */ public void push (T element) { if (size() == stack.length) expandCapacity();   stack[top] = element; top++; }

The stack after pushing element E

ArrayStack – the pop operation /** * Removes the element at the top of this stack and returns a * reference to it. Throws an EmptyCollectionException if the stack * is empty. * @return T element removed from top of stack * @throws EmptyCollectionException if a pop is attempted on empty stack */ public T pop() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("Stack");   top--; T result = stack[top]; stack[top] = null; return result; }

The stack after popping the top element

ArrayStack – the peek operation /** * Returns a reference to the element at the top of this stack. * The element is not removed from the stack. Throws an * EmptyCollectionException if the stack is empty. * @return T element on top of stack * @throws EmptyCollectionException if a peek is attempted on empty stack */ public T peek() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("Stack");   return stack[top-1]; }

Other operations The size operation is imply a matter of returning the count The isEmpty operation returns true if the count is 0 and false otherwise The toString operation concatenates a string made up of the results of the toString operations for each element in the stack

Analysis of Stack Operations Because stack operations all work on one end of the collection, they are generally efficient The push and pop operations, for the array implementation are O(1) Likewise, the other operations are also O(1)