csc 212 stacks & queues. announcement many programs not compiled before submission several...

18
CSC 212 Stacks & Queues

Upload: nickolas-malone

Post on 02-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc

CSC 212

Stacks & Queues

Page 2: CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc

Announcement

Many programs not compiled before submissionSeveral could not be compiledSeveral others not tested with javadocWhy?

Many students struggling with JavaThis is a programming classOnly way to get better: PRACTICE

Page 3: CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc

Announcement

Daily quizzes accepted electronically onlySubmit via one or other DropboxCannot force you to compile & test themCan make it much less workPlease, please, please use this as an excuse

to practice your Java Next homework will be assigned Thursday

Page 4: CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc

Abstract Data Types (ADTs)

We will discuss ADTs for remainder of class

ADTs do NOT specify implementation ADTs do guarantee minimum functionality

for data typeOnly discuss publicly available functionsWhat part(s) of Java could be used to do this?

Page 5: CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc

Abstract Data Types (ADTs)

ADTs define more than functionalityWhat data is storedHow the data will be handledWhat errors can occur and be handled

How?

Page 6: CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc

Stock Trading ADT

Suppose we want to design a stock trading ADT

What functionality should be defined?

What errors could occur? How should they be handled?

Page 7: CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc

Stacks

Stack is like a PEZ® dispenser: Objects get “pushed” onto stack

Objects pushed on top of stackObjects can be pushed at any time

Objects get “popped” from stackPopping object removes it from stackOnly top object on stack can be popped

Page 8: CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc

Stacks

So, Stack ADT defines two vital methods:void push(Object obj)Object pop()

Also defines other useful functionality: int size() returns number of elements on stackboolean isEmpty() returns if stack is emptyObject top() returns top entry from Stack w/o

removing it

Page 9: CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc

Stacks

We can define our own stack interface like this:public interface Stack {

public int size(); // returns elements in the stack

public boolean isEmpty(); // return if the stack is empty

public Object top() // return the top elementthrows StackEmptyException; // if the stack is

emptypublic void push(Object obj); // push obj onto the stackpublic Object pop() // return and remove top stack element

throws StackEmptyException; // if the stack is empty

}

Why do push(), pop(), and top() return objects of type Object?

Page 10: CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc

Stacks

Java stores “call frames” using a stackRecords parameters

References for object parameters, actual values for primitive

Maintains storage space for temporary resultsAlso location being executed

Page 11: CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc

findMin

1. public static int findMin(int[] a, int n) {

2. if (n == (a.length - 1))

3. return a[n];

4. else {

5. int recurMin = findMin(a, n+1);

6. if (a[n] < recurMin)

7. return a[n];

8. else

9. return recurMin;

10. }

11. }

Page 12: CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc

Array-based Stack

Could implement stacks using arraysRequiring a maximum size N for our stack

(e.g., N = 1000) Stack then includes

N-element array, s,Array index of the top element, t

Page 13: CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc

Array-based Stack

Pseudo-code:

int size(): return t +1

boolean isEmpty(): return t < 0

Object top():

if isEmpty() then

throw StackEmptyExceptionelse

return s [t ]

Page 14: CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc

Array-based Stack

Arrays start at 0, so initialize t to -1 Should we throw any new exceptions in

addition to our stack interface?

Page 15: CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc

Array-based Stack

More pseudo-code:push(obj):

if size() = N then throw StackFullException

t t + 1

s[t] obj

Object pop():

if isEmpty() then throw StackEmptyException

e s[t ]

s[t ] null

t t -1

return e

Page 16: CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc

Array-based Stack

What is input size for each of these methods?

What is running time for each method?

Page 17: CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc

Array-based Stack

Arrays make simple and efficient implementation

Note: N is upper bound on stack size If N is too small, cannot hold all items

Makes stack class unusable If N is too large, will waste memory

Can lead to significant performance penalty

Page 18: CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc

More group work

Write code to reverse elements in an array using a stack

What is the complexity of this method?