180 likes | 296 Views
Question of the Day. How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?. Question of the Day.
E N D
Question of the Day • How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?
Question of the Day • How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?
CSC 212 – Data Structures Lecture 22:Linked List-BasedStack
Using Stack • Last-In, First-Out principle used to access data • Also called LIFO ordering • Top of stack is where data added & removed
Stack Interface public interface Stack<E> extends Collection {public Epeek()throws EmptyCollectionException;public Epop() throws EmptyCollectionException;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
Why It Rocks Why It Sucks Array-based Stack • Easy to write & read • Simple to find bugs • Quick running times • Methods (usually) take O(1) time • Array must be huge • Max. possible elements • Problems occur when too many exist at once • When full,takes O(n) time to reload
Better Approach (Maybe?) • Implement Stack using linked list • Grows & shrinks as elements added & removed • Add element in push() by allocating LinearNode • pop()removes LinearNodefrom linked list • Concerns about size limits are forgotten
Once You pop()… • Check for empty Stack • If it is, throw exception • Pop top LinearNode • Node’s element saved • topmoves to next node Algorithmpop()ifisEmpty()thenthrow new EmptyCollectionExceptionelseretValtop.getElement()toptop.getNext()count count- 1fireturnretVal
Once You pop()… retVal • Check for empty Stack • If it is, throw exception • Pop top LinearNode • Node’s element saved • topmoves to next node Algorithmpop()ifisEmpty()thenthrow new EmptyCollectionExceptionelseretValtop.getElement()toptop.getNext()count count- 1fireturnretVal
Once You pop()… Algorithmpop()ifisEmpty()thenthrow new EmptyCollectionExceptionelseretValtop.getElement()toptop.getNext()count count- 1fireturnretVal retVal • Check for empty Stack • If it is, throw exception • Pop top LinearNode • Node’s element saved • topmoves to next node
Once You pop()… Algorithmpop()ifisEmpty()thenthrow new EmptyCollectionExceptionelseretValtop.getElement()toptop.getNext()count count- 1fireturnretVal retVal • Check for empty Stack • If it is, throw exception • Pop top LinearNode • Node’s element saved • topmoves to next node
Linked list-based Stack • pushing very easy, too • Adds new top node • Easy to check if empty • Simplified w/o extra class Algorithmpush(e)newNnewLinearNode(e)newN.setNext(top)topnewNcount count + 1 AlgorithmisEmpty()returntop == null
Linked list-based Stack e • pushing very easy, too • Adds new top node • Easy to check if empty • Simplified w/o extra class Algorithmpush(e)newNnewLinearNode(e)newN.setNext(top)topnewNcount count + 1 AlgorithmisEmpty()returntop == null
Linked list-based Stack newN e • pushing very easy, too • Adds new top node • Easy to check if empty • Simplified w/o extra class Algorithmpush(e)newNnewLinearNode(e)newN.setNext(top)topnewNcount count + 1 AlgorithmisEmpty()returntop == null
Linked list-based Stack newN e • pushing very easy, too • Adds new top node • Easy to check if empty • Simplified w/o extra class Algorithmpush(e)newNnewLinearNode(e)newN.setNext(top)topnewNcount count + 1 AlgorithmisEmpty()returntop == null
Linked list-based Stack • pushing very easy, too • Adds new top node • Easy to check if empty • Simplified w/o extra class Algorithmpush(e)newNnewLinearNode(e)newN.setNext(top)topnewNcount count + 1 AlgorithmisEmpty()returntop == null
Your Turn • Get into your groups and complete activity
For Next Lecture • Read sections 5.1 – 5.7 before Monday's class • Discusses design of the Queue ADT • Array-based implementation of Queue presented • Queue implementation of linked-list also shown • Week #8 weekly assignment due Tuesday • Programming Assignment #1 due next week • Not too late now; but should really have started!