19
LECTURE 24: STACK ADTS CSC 212 – Data Structures

LECTURE 24: STACK ADTS CSC 212 – Data Structures

Embed Size (px)

Citation preview

Page 1: LECTURE 24: STACK ADTS CSC 212 – Data Structures

LECTURE 24:STACK ADTS

CSC 212 – Data Structures

Page 2: LECTURE 24: STACK ADTS CSC 212 – Data Structures

Rest of the Year

Page 3: LECTURE 24: STACK ADTS CSC 212 – Data Structures

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

Page 4: LECTURE 24: STACK ADTS CSC 212 – Data Structures

View of an ADT

You

Other Coder

IOU

ADT

Page 5: LECTURE 24: STACK ADTS CSC 212 – Data Structures

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

Page 6: LECTURE 24: STACK ADTS CSC 212 – Data Structures

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?

Page 7: LECTURE 24: STACK ADTS CSC 212 – Data Structures

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

Page 8: LECTURE 24: STACK ADTS CSC 212 – Data Structures

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

Page 9: LECTURE 24: STACK ADTS CSC 212 – Data Structures

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

Page 10: LECTURE 24: STACK ADTS CSC 212 – Data Structures

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

Page 11: LECTURE 24: STACK ADTS CSC 212 – Data Structures

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

Page 12: LECTURE 24: STACK ADTS CSC 212 – Data Structures

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

Page 13: LECTURE 24: STACK ADTS CSC 212 – Data Structures

Using Stack

Page 14: LECTURE 24: STACK ADTS CSC 212 – Data Structures

Using Stack

Page 15: LECTURE 24: STACK ADTS CSC 212 – Data Structures

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

Page 16: LECTURE 24: STACK ADTS CSC 212 – Data Structures

= 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

Page 17: LECTURE 24: STACK ADTS CSC 212 – Data Structures

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

Page 18: LECTURE 24: STACK ADTS CSC 212 – Data Structures

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!

Page 19: LECTURE 24: STACK ADTS CSC 212 – Data Structures

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…