Stacks Abstract Data Types (ADTs) Stacks

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.
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.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
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?
1 Lecture 25 Abstract Data Types –II Overview  A stack Interface in Java  StackEmptyException  Stack ADT(A Simple Array-Based Implementation  Queue.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Stacks.
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.
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.
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)
Stacks1 CS2468 Data Structures and Data Management Lecturer: Lusheng Wang Office: B6422 Phone:
Stacks, Queues, and Deques
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Topic 3 The Stack ADT.
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.
Abstract Data Type (ADT) & Stacks
Chapter 3 Introduction to Collections – Stacks Modified
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
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.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
LECTURE 24: STACK ADTS CSC 212 – Data Structures.
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)
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.
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.
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 (and Queues).
Stacks II David Lillis School of Computer Science and Informatics
Elementary Data Structures
Review Array Array Elements Accessing array elements
Stacks Stacks.
CSCI 3333 Data Structures Stacks.
Exceptions, Interfaces & Generics
Stacks.
Stacks and Queues.
Queues Queues Queues.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
CMSC 341 Lecture 5 Stacks, Queues
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Stacks.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks, Queues, and Deques
CS210- Lecture 3 Jun 6, 2005 Announcements
A type is a collection of values
Stacks and Linked Lists
CHAPTER 3: Collections—Stacks
Presentation transcript:

Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions

Abstract Data Types (ADTs) An Abstract Data Type is an abstraction of a data structure. (No coding is involved.) The ADT specifies: what can be stored in the ADT what operations can be done on/by the ADT For example, if we are going to model a bag of marbles as an ADT, we could specify that: this ADT stores marbles this ADT supports putting in a marble and getting out a marble.

Abstract Data Types (ADTs) There are lots of formalized and standard ADTs. (A bag of marbles is not one of them.) In this course we are going to learn a lot of different standard ADTs. (stacks, queues, trees...)

Stacks A stack is a container of objects that are inserted and removed according to the last-in-first-out (LIFO) principle. Objects can be inserted at any time, but only the last (the most-recently inserted) object can be removed. Inserting an item is known as “pushing” onto the stack. “Popping” off the stack is synonymous with removing an item. A PEZ® dispenser as an analogy:

The Stack Abstract Data Type A stack is an abstract data type (ADT) that supports two main methods: push(o): Inserts object o onto top of stack pop(): Removes the top object of stack and returns it; if the stack is empty, an error occurs The following support methods should also be defined: size(): Returns the number of objects in stack isEmpty(): Return a boolean indicating if stack is empty. top(): Return the top object of the stack, without removing it; if the stack is empty, an error occurs.

Application: Time Series The span si of a stock’s price on a certain day i is the maximum number of consecutive days (up to the current day) the price of the stock has been less than or equal to its price on day i

An Inefficient Algorithm There is a straightforward way to compute the span of a stock on each of n days: Algorithm computeSpans1(P): Input: an n-element array P of numbers such that P[i] is the price of the stock on day i Output: an n-element array S of numbers such that S[i] is the span of the stock on day i for i 0 to n - 1 do k  0 done  false repeat if P[i - k]  P[i] then k  k + 1 else done  true until (k = i) or done S[i]  k return S The running time of this algorithm is (ugh!) O(n2). Why?

A Stack Can Help We see that si on day i can be easily computed if we know the closest day preceding i, such that the price is greater than on that day than the price on day i. If such a day exists, let’s call it h(i), otherwise, we conventionally define h(i) = -1 The span is now computed as si = i- h(i) We use a stack to keep track of h(i)

An Efficient Algorithm The code for our new algorithm: Algorithm computeSpans2(P): Input: an n-element array P of numbers representing stock prices Output: an n-element array S of numbers such that S[i] is the span of the stock on day i Let D be an empty stack for i 0 to n - 1 do k  0 done  false while not(D.isEmpty() or done) do if P[i] ≥ P[D.top()] then D.pop() else done  true if D.isEmpty() then h  -1 else h  D.top() S[i]  i - h D.push(i) return S

Java Stuff Given the stack ADT, we need to code the ADT in order to use it in the programs. You need to understand two program constructs: interfaces and exceptions. An interface is a way to declare what a class is to do. It does not mention how to do it. For an interface, you just write down the method names and the parameters. When specifying parameters, what really matters is their types. Later, when you write a class for that interface, you actually code the content of the methods. Separating interface and implementation is a useful programming technique. Interface example:

A Stack Interface in Java While, the stack data structure is a “built-in” class of Java’s java.util package, we define our own stack interface: public interface Stack { // accessor methods public int size(); public boolean isEmpty(); public Object top() throws StackEmptyException; // update methods public void push (Object element); public Object pop() throws StackEmptyException; }

Exceptions Exceptions are yet another programming construct, useful for handling errors. When you find an error (or an exceptional case), you just throw an exception. Example public void eatPizza() throws StomachAcheException {... if (ateTooMuch) throw new StomachAcheException(“Ouch”); ...} As soon as the exception is thrown, the flow of control exits from the current method. So when StomachAcheException is thrown, we exit from method eatPizza() and go to where that method was called from.

More Exceptions Say the following code fragment called the method eatPizza() in the first place. private void simulateMeeting() {... try { aStupidTA.eatPizza(); } catch(StomachAcheException e) System.out.println(“somebody has a stomach ache”); ...}

Even More Exceptions We will get back to aStupidTA.eatPizza(); because, remember, eatPizza() threw an exception. The try block and the catch block means that we are listening for exceptions that are specified in the catch parameter. Because catch is listening for StomachAcheException, the flow of control will now go to the catch block. And System.out.println will get executed. Note that a catch block can contain anything. It does not have to do only System.out.println. You can handle the caught error in any way you like; you can even throw them again. Note that if somewhere in your method, you throw an exception, you need to add a throws clause next to your method name. What is the point of using exceptions? You can delegate upwards the responsibility of handling an error. Delegating upwards means letting the code who called the current code deal with the problem.

Even More Exceptions If you never catch an exception, it will propagate upwards and upwards along the chain of method calls until the user sees it

Final Exceptions OK, so we threw and caught exceptions. But what exactly are they in Java? Classes. Check out the StomachAcheException. public class StomachAcheException extends RuntimeException { public StomachAcheException(String err) { super(err); }

An Array-Based Stack Create a stack using an array by specifying a maximum size N for our stack, e.g., N = 1,024. The stack consists of an N-element array S and an integer variable t, the index of the top element in array S. Algorithm size(): return t +1 Algorithm isEmpty(): return (t < 0) Algorithm top(): if isEmpty() then throw a StackEmptyException return S[t] ... NOTE: Array indices start at 0, so we initialize t to -1 Pseudo-code is to the right.

Pseudo-Code (contd.) Algorithm push(o): if size() = N then throw a StackFullException t  t + 1 S[t]  o Algorithm pop(): if isEmpty() then throw a StackEmptyException e  S[t] S[t]¨null t  t-1 return e

An Array-Based Stack (contd.) Both the push and pop methods runs in O(1) time The array implementation is simple and efficient. There is a predefined upper bound, N, on the size of the stack, which may be too small for a given application, or cause a waste of memory. StackEmptyException is required by the interface. StackFullException is particular to this implementation. Algorithm pop(): if isEmpty() then throw a StackEmptyException e  S[t] S[t]¨null t  t-1 return e Algorithm push(o): if size() = N then throw a StackFullException t  t + 1 S[t]  o

Array-Based Stack in Java public class ArrayStack implements Stack { // Implementation of the Stack interface using an array. public static final int CAPACITY = 1024; // default capacity of the stack private int capacity; // maximum capacity of the stack. private Object S[ ]; // S holds the elements of the stack private int top = -1; // the top element of the stack. public ArrayStack( ) { // Initialize the stack this(CAPACITY);// with default capacity} public ArrayStack(int cap) { // Initialize the stack with given capacity capacity = cap; S = new Object[capacity];}

Array-Based Stack in Java (contd.) public int size( ) { //Return the current stack size return (top + 1);} public boolean isEmpty( ) { // Return true iff the stack is empty return (top < 0);} public void push(Object obj) throws StackFullException{ // Push a new element on the stack if (size() == capacity) {throw new StackFullException(“Stack overflow.”); }S[++top] = obj;}

Array-Based Stack in Java (contd.) public Object top( )// Return the top stack element throws StackEmptyException { if (isEmpty( )) { throw new StackEmptyException(“Stack is empty.”); }return S[top];} public Object pop() // Pop off the stack element Object elem; if (isEmpty( )) {throw new StackEmptyException(“Stack is Empty.”); elem = S[top]; S[top--] = null; // Dereference S[top] and decrement top return elem; }}