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.

Slides:



Advertisements
Similar presentations
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Advertisements

Stacks, Queues, and Linked Lists
Lecture Stacks. A stack is a Last-In-First-Out (LIFO) or a First-In-Last-Out (FILO) abstract data type E.g. a deck of cards in which cards may be added.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
CHAPTER 7 Queues.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Chapter 3: Abstract Data Types Lists, Stacks Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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.
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
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.
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.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Stacks, Queues, and Deques
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Chapter 12 Introduction to Collections - Stacks
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
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.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
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.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
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.
© 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.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
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.
© 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.
Data Structures Using Java1 Chapter 6 Stacks. Data Structures Using Java2 Chapter Objectives Learn about stacks Examine various stack operations Learn.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
Collections Chapter 12 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Collections & Stacks Collections A collection is an object that holds and organizes other objects It provides operations for accessing and managing.
Stacks Stack Abstract Data Type (ADT) Stack ADT Interface
Stack: a Linked Implementation
Data Structures Using C++ 2E
Stacks.
Introduction to Exceptions in Java
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
COMPUTER 2430 Object Oriented Programming and Data Structures I
Java Software Structures: John Lewis & Joseph Chase
Java Software Structures: John Lewis & Joseph Chase
CSC 1052 Stacks 1/11/2019.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks Data structure Elements added, removed from one end only
Chapter 5 Stack (part 1).
Stacks, Queues, and Deques
A type is a collection of values
CHAPTER 3: Collections—Stacks
Presentation transcript:

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 put on the stack is the first element to be removed A stack is LIFO – last in, first out

3 A conceptual view of a stack

4 Basic operations Push: store a data item at the top of the stack Pop: retrieve a data item from the top of the stack

5 Example Push Item 1 Push Item 2 Push Item 3 Pop Push Item 4 Pop Item 1 Item 2 Item 1 Item 3 Item 2 Item 1 Item 2 Item 1 Item 4 Item 2 Item 1

6 More operations on a stack

7 Example A procedure to replace the elements of a nonempty stack, assuming they are of type integers, with their sum Pre: A nonempty stack with elements of type integers. Post: s contains only one element - the sum of previously stored elements.

8 Algorithm 1. element1  S.pop() 2. while stack is not empty repeat 2.1. element2  S.pop() 2.2. S.push(element1+element2) 2.3. element1  S.pop() 3. push(S,element1)

9 Stack applications Undo operations in editors Evaluating an arithmetic expression using postfix notation Converting infix expression into postfix expression Depth-first search in a tree/graph

10 Stack implementation The interface class Array implementation Linked implementation

11 Generic Types Java enables us to define a class based upon a generic type This means that we can define a class so that it stores, operates on, and manages objects whose type is not specified until the class is instantiated The type supplied at the time of instantiation replaces the type T wherever it is used in the declaration of the class

12 Example of Generic Types class Box { // declaration and code that manage objects // of type T } Box box1 = new Box ();

13 The Stack interface class public interface StackADT { // Adds one element to the top of this stack public void push (T element); // Removes and returns the top element from this stack public T pop(); // Returns without removing the top element of this stack public T peek(); // Returns true if this stack contains no elements public boolean isEmpty(); // Returns the number of elements in this stack public int size(); // Returns a string representation of this stack public String toString(); }

14 Array implementation Design decisions: maintain an array of Object 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

15 Array implementation

16 Private variables private final int DEFAULT_CAPACITY = 100; private int top; // indicates the next open slot private T[ ] stack;

17 Constructor public ArrayStack() { top = 0; stack = (T [ ])(new Object[DEFAULT_CAPACITY]); } public ArrayStack (int initialCapacity) { top = 0; stack = (T [ ])(new Object[initialCapacity]); }

18 Push If the array is full, expand its capacity Store the element to be pushed at position ‘top’ Increment ‘top’ public void push (T element) { if (size() == stack.length) expandCapacity(); stack[top] = element; top++; }

19 Pop If empty throw exception Decrement ‘top’ Get the contents at position ‘top’ into ‘result’ Return result public T pop() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException(“stack”); top--; T result = stack[top]; return result; }

20 Peek If empty throw exception Return the contents of the top node public T peek() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException(“stack”); return stack[top-1]; }

21 isEmpty if size is zero return true else return false public boolean isEmpty() { if (top == 0) return true else return false; }

22 Size return the size public int size() { return top; }

23 Managing Capacity Declare a new array with twice the size of the current array Copy the elements of the current array into the new array Update the reference variable stack to point to the new array

24 Managing Capacity public void expandCapacity() { T[ ] larger = (T[ ])(new Object[stack.length *2]); for(int index=0; index < stack.length; index++) larger[index] = stack[index]; stack = larger; }

25 Handling Exceptions An exception is an object that defines an unusual or erroneous situation A program can be designed to process an exception in one of three ways: - Not handle the exception at all - Handle the exception where it occurs - Handle the exception at another point in the program If an exception is not handled at all by the program, the program will produce an exception message and terminate

26 Handling Exceptions public class EmptyCollectionException extends RuntimeException { // // Sets up this exception with an appropriate message. // public EmptyCollectionException (String collection) { super ("The " + collection + " is empty."); }

27 Handling Exceptions – Try and Catch To handle an exception when it is thrown, we use a try statement A try statement consists of a try block followed by one or more catch clauses try { // statements in the try block } catch (IOException exception) { // statements that handle the I/O problem }

28 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 both linked and array implementations, are O(1) Likewise, the other operations are O(1) The expandArray method has complexity O(n), where n is the number of elements in the stack