Presentation is loading. Please wait.

Presentation is loading. Please wait.

LECTURE 24: STACK ADTS CSC 212 – Data Structures.

Similar presentations


Presentation on theme: "LECTURE 24: STACK ADTS CSC 212 – Data Structures."— Presentation transcript:

1 LECTURE 24: STACK ADTS CSC 212 – Data Structures

2 Rest of the Year

3  Each ADT is defined by single Interface  Guarantees methods exist & what they should do  But classes are free to implement however they want  Programmer knows from the interface :  How to call methods  What calling the method will do  Value returned by the method  If and when Exceptions are thrown ADTs Mean Interface s

4 View of an ADT You Other Coder IOU ADT

5  ADTs must remain abstract  Any implementation okay, if it completes methods  Could implement an ADT with:  Array  Linked list  Trained monkeys  College students Implementing ADT

6  Linked lists have very specific implementation  Singly-, & doubly-linked versions exist…  … but implementation impossible using an array  No trained monkeys could do the same work  There is no functionality specified  No standard way to access or use data  In fact, there is no ADT serving as guarantee!  Arrays & linked lists are concrete implementations Is Linked Lists an ADT?

7  Superinterface for all our ADTs  Define methods common to all data structures  Access & usages patterns will differ, however public interface Collection { public int size(); public boolean isEmpty(); } Collection Classes

8  Awwww… our first collection class  Works like PEZ dispenser:  Add by pushing data onto top  Pop top item off to remove  Accessing other values impossible  Top item only is available  Cheap plastic/private fields get in way Stacks

9 Applications of Stacks  Stacks are used everywhere you look  Back & Forward buttons in web browser  Powerpoint application’s Undo & Redo commands  Methods’ stackframes used during execution  Java uses stacks to execute operations in program  Many other data structures also rely on stacks

10  Defines two vital methods…  push(obj)  add obj onto top of stack  pop()  remove & return item on top of stack  … an accessor method…  top()  return top item (but do not remove it)  … and Collection ’s methods…  size()  returns number of items in stack  isEmpty()  states if the stack contains items Stack ADT

11  Requires defining a new exception public class EmptyStackException extends RuntimeException { public EmptyStackException(String err) { super(err); } }  EmptyStackException is unchecked  Need not be listed in throws, but could be  Unchecked since there is little you can do to fix it  try-catch not required, but can crash program More Stack ADT

12 public interface Stack extends Collection { public E top() throws EmptyStackException; public E pop() throws EmptyStackException; public void push(E element); }  Any type of data stored within a Stack  Generics enable us to avoid rewriting this code  Minimum set of exceptions defined by interface  Classes could throw more unchecked exceptions Stack Interface

13 Using Stack

14

15 Computing Spans  Given X, S[i] is # of consecutive smaller entries preceding X[i]  Used by:  Financial analysts  Analytical chemists 63452 11231 X S

16  = O(n) * (O(1) + O(n) * O(1)) = O(n 2 ) time Non-Stack Based Algorithm Algorithm spans1(int[] X)‏ S  new int[X.length] n for i  0 to X.length  1 don s  11 while s  i  X[i  s]  X[i] 1  2  …  (n  1)=O(n)‏ s  s  11 endwhile S[i]  s 1 endfor return S 1

17 Computing Spans with a Stack  Improve using Stack  Keep local maxima  Let i =current index  Pop indices until find j, such that X[i]  X[j]  Set S[i]  i  j  Push i onto stack

18 Algorithm spans2(int[] X) # S  new int[X.length] n A  new Stack implementation 1 for i  0 to X.length  1 don while (!A.isEmpty() && X[A.top()]  X[i]) do? A.pop()1 endwhile if A.isEmpty() then 1 S[i]  i  11 else S[i]  i  A.top()1 fi A.push(i)1 endfor return S 1 Stack-Based Algorithm  Each array index  Pushed once  Popped at most once  While-loop runs once per stack entry  So, ≤ n times per run  O(n) time to run!

19  Start week #9 assignment  Due by 5PM next Tuesday  Start programming assignment #3  Messages are not always sent to everyone!  Re-examine section 5.1 in book before class  Discuss how to implement the Stack ADT  What ways can we implement it?  How do arrays and linked-lists fit into this? Before Next Lecture…


Download ppt "LECTURE 24: STACK ADTS CSC 212 – Data Structures."

Similar presentations


Ads by Google