30 May 20161 Stacks (5.1) CSE 2011 Winter 2011. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.

Slides:



Advertisements
Similar presentations
Stacks.
Advertisements

Chapter 5.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Stacks. Queues. Double-Ended Queues. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Applications of Stacks Direct applications Delimiter matching Undo sequence in a text.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks, Queues, and Deques
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
1 Lecture 25 Abstract Data Types –II Overview  A stack Interface in Java  StackEmptyException  Stack ADT(A Simple Array-Based Implementation  Queue.
Stacks.
Stacks and QueuesCSC311: Data Structures1 Chapter 5 Stacks and Queues Objectives –Stacks and implementations –Queues and implementations –Double-ended.
CSC401 – Analysis of Algorithms Lecture Notes 3 Basic Data Structures Objectives: Introduce basic data structures, including –Stacks –Queues –Vectors –Lists.
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.
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.
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)
Implementing and Using Stacks
Stacks1 CS2468 Data Structures and Data Management Lecturer: Lusheng Wang Office: B6422 Phone:
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stacks © 2010 Goodrich, Tamassia1Stacks. 2 Abstract Data Types (ADTs)  An abstract data type (ADT) is an abstraction of a data structure  An ADT specifies:
Abstract Data Type (ADT) & Stacks
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
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)
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
Stacks & Queues EECS: Stacks & Queues Stacks April 23, 2017
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
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 1. Outline 2  Stacks  Queues.
Dynamic Arrays and Stacks CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
1 COMP9024: Data Structures and Algorithms Week Four: Stacks and Queues Hui Wu Session 1, 2016
Click to edit Master text styles Stacks Data Structure.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stack: Last In First Out (LIFO).–Used in procedure calls, to compute arithmetic expressions.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Stacks 1/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Stacks (and Queues).
Elementary Data Structures
Stacks Stacks.
CSCI 3333 Data Structures Stacks.
Stacks.
Stacks 9/12/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Stacks.
Stacks.
Chapter 5 Stacks and Queues 11/28/2018 Stacks.
Stacks 12/7/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Copyright © Aiman Hanna All rights reserved
Recall What is a Data Structure Very Fundamental Data Structures
Stacks Abstract Data Types (ADTs) Stacks
Stacks and Queues DSA 2013 Stacks n Queues.
Stacks.
Stacks.
Lecture 8: Stacks, Queues
Stacks and Linked Lists
Presentation transcript:

30 May Stacks (5.1) CSE 2011 Winter 2011

Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies:  Data stored  Operations on the data  Error conditions associated with operations Example: ADT modeling a simple stock trading system  The data stored are buy/sell orders  The operations supported are order buy(stock, shares, price) order sell(stock, shares, price) void cancel(order)  Error conditions: Buy/sell a nonexistent stock Cancel a nonexistent order

3 Stacks: LIFO Insertions and deletions follow the Last-In First-Out rule Example applications: – Undo operation in a text editor – History of visited web pages – Sequence of method calls in Java

Stacks4 Method Stack in the JVM The Java Virtual Machine (JVM) keeps track of the chain of active methods with a stack When a method is called, the JVM pushes on the stack a frame containing  Local variables and return value  Program counter, keeping track of the statement being executed When a method ends, its frame is popped from the stack and control is passed to the method on top of the stack Allows for recursion main() { int i = 5; foo(i); } foo(int j) { int k; k = j+1; bar(k); } bar(int m) { … } bar PC = 1 m = 6 foo PC = 3 j = 5 k = 6 main PC = 2 i = 5

5 Stack ADT Data stored: arbitrary objects Operations: – push(object): inserts an element – object pop(): removes and returns the last inserted element Other useful operations: – object top(): returns the last inserted element without removing it

6 Error Conditions push(object) object pop() object top() Exceptions are thrown when an operation cannot be executed. Execution of pop() or top() on an empty stack → throws EmptyStackException. Another useful operation: – boolean isEmpty(): returns true if the stack is empty; false otherwise.

7 Stack Operations push(object) object pop() object top() boolean isEmpty() Still another useful operation: int size(): returns the number of elements in the stack Any others? Depending on implementation

Stacks8 Stack Interface in Java Java interface corresponding to our Stack ADT Requires the definition of class EmptyStackException Different from the built-in Java class java.util.Stack public interface Stack { public int size(); public boolean isEmpty(); public Object top() throws EmptyStackException; public void push(Object o); public Object pop() throws EmptyStackException; }

9 Array-based Implementation An array S of maximum size N A variable t that keeps track of the top element in array S  How to initialize t? Top element: S[t] push( ), pop( ): how to update t? Stack is empty, isEmpty( ): ? Number of elements in the stack, size(): ?

10 Pseudo-code Algorithm size(): return (t + 1); Algorithm isEmpty(): return (t < 0); Algorithm top(): if (isEmpty()) throw StackEmptyException; return S[t]; Algorithm pop(): if (isEmpty()) throw StackEmptyException; temp = S[t]; t = t – 1; return temp; Optimization: set S[t] to null before decrementing t. Homework: implement pop() without any temp variable.

11 Method push() Algorithm push(object): t = t + 1; S[t] = object; The array may become full push() method will then throw a FullStackException Limitation of array-based implementation One solution: extend the stack Algorithm push(object): if (size() == N) throw FullStackException; t = t + 1; S[t] = object;

Stacks12 Array-based Stack in Java public class ArrayStack implements Stack { // holds the stack elements private Object S[ ]; // index to top element private int top = -1; // constructor public ArrayStack(int capacity) { S = new Object[capacity]); } public Object pop() throws EmptyStackException { if isEmpty() throw new EmptyStackException (“Empty stack: cannot pop.”); Object temp = S[top]; // facilitates garbage collection S[top] = null; top = top – 1; return temp; }

13 Performance of Array Implementation Each operation runs in O(1) time (no loops, no recursion) Array-based implementation is simple, efficient, but … The maximum size N of the stack is fixed How to determine N? Not easy! Alternatives?  Extendable arrays  Linked lists (singly or doubly linked???)

Linked List Implementation Singly or doubly linked list? Where should the “top” be, head or tail? 14 ABCD 

15 push() Method

pop() Method 16

17 Analysis of Linked List Implementation Space usage: O(n) n = number of elements in the stack Each operation runs in O(1) time No limit on the stack size, subject to available memory (run-time error OutOfMemoryError) Java code: textbook, p. 212

18 Homework List-based and array-based operations all run in O(1) time. List-based implementation imposes no limit on the stack size, while array-based implementation does. Is list-based implementation better? Can we perform push() and pop() at the tail of the linked list? Analyze the running time in this case. Study the linked list implementation of stacks in Java on p. 212.

More Applications of Stacks Reversing an array using a stack (5.1.4) Matching parentheses, brackets, and quotes in Java files (5.1.5) 19

Next time... Queues (5.2) 20