Upload
lizbeth-curtis
View
234
Download
4
Embed Size (px)
Citation preview
LECTURE 24:STACK ADTS
CSC 212 – Data Structures
Rest of the Year
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 Interfaces
View of an ADT
You
Other Coder
IOU
ADT
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
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?
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
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
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
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
Requires defining a new exceptionpublic 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
public interface Stack<E> 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
Using Stack
Using Stack
Computing Spans
Given X, S[i] is # of consecutive smaller entries preceding X[i]
Used by: Financial analysts Analytical
chemists
6 3 4 5 2
1 1 2 3 1
X
S
01234567
0 1 2 3 4
= O(n) * (O(1) + O(n) * O(1)) = O(n2) time
Non-Stack Based AlgorithmAlgorithm spans1(int[] X)
S new int[X.length] nfor i 0 to X.length 1 do n
s 1 1while s i X[i s] X[i] 12…(n
1)=O(n) s s 1 1
endwhileS[i] s 1
endforreturn S 1
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
01234567
0 1 2 3 4 5 6 7
Algorithm spans2(int[] X) #S new int[X.length] nA new Stack implementation1for i 0 to X.length 1 do n
while (!A.isEmpty() && X[A.top()] X[i]) do ?
A.pop() 1 endwhile
if A.isEmpty() then 1S[i] i 1 1
elseS[i] i A.top() 1
fiA.push(i) 1
endforreturn 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!
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…