270 likes | 591 Views
Problem of the Day. What do you get when you cross a mountain climber and a grape?. Problem of the Day. What do you get when you cross a mountain climber and a grape? Nothing, you cannot cross a scalar. CSC 212 – Data Structures. Lecture 22: Implementing Stack. Using Stack.
E N D
Problem of the Day • What do you get when you cross a mountain climber and a grape?
Problem of the Day • What do you get when you cross a mountain climber and a grape? • Nothing, you cannot cross a scalar.
CSC 212 – Data Structures Lecture 22:Implementing Stack
Using Stack • Last-In, First-Out principle used to access data • Also called LIFO ordering • Top of stack is where data added & removed
Using Stack • Last-In, First-Out principle used to access data • Also called LIFO ordering • Top of stack is where data added & removed • Pulling out tablecloth trick does not work
Stack Interface public interface Stack<E> extends Collection {public Etop()throws EmptyStackException;public Epop() 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
Array-based Implementation … 0 1 2 • Array reference in a field • Another field tracks top element’s index • Stack size also: top + 1 • Add to next lowest index • Remove highest index Algorithm isEmpty()returntop== -1 Algorithmpop()ifisEmpty()then throw new EmptyStackExceptionelsetoptop 1returnS[top + 1]
Array-based Implementation … 0 1 2 top • Array reference in a field • Another field tracks top element’s index • Stack size also: top + 1 • Add to next lowest index • Remove highest index Algorithm isEmpty()returntop== -1 Algorithmpop()ifisEmpty()then throw new EmptyStackExceptionelsetoptop 1returnS[top+ 1]
Array-based Implementation … X 0 1 2 top • Array reference in a field • Another field tracks top element’s index • Stack size also: top + 1 • Add to next lowest index • Remove highest index Algorithm isEmpty()returntop== -1 Algorithmpop()ifisEmpty()then throw new EmptyStackExceptionelsetoptop 1returnS[top + 1]
Array-based Implementation … X 0 1 2 top • Array reference in a field • Another field tracks top element’s index • Stack size also: top + 1 • Add to next lowest index • Remove highest index Algorithm isEmpty()returntop== -1 Algorithmpop()ifisEmpty()then throw new EmptyStackExceptionelsetoptop 1returnS[top + 1]
… X 0 1 2 top • Could fill array with data • More push()s impossible • Throw exception on error • Specific to array-based • Unchecked exception must be used Algorithmpush(elem) iftop==S.length 1then throw new FullStackExceptionelsetoptop+ 1S[top] elemfi
… X 0 1 2 top • Could fill array with data • More push()s impossible • Throw exception on error • Specific to array-based • Unchecked exception must be used Algorithmpush(elem) iftop==S.length 1then throw new FullStackExceptionelsetoptop+ 1S[top] elemfi
… 0 1 2 top • Could fill array with data • More push()s impossible • Throw exception on error • Specific to array-based • Unchecked exception must be used Algorithmpush(elem) iftop==S.length 1then throw new FullStackExceptionelsetoptop+ 1S[top] elemfi
Oops… My Bad … 0 1 2 top • Could fill array with data • More push()s impossible • Throw exception on error • Specific to array-based • Unchecked exception must be used Algorithmpush(elem) iftop==S.length 1then throw new FullStackExceptionelsetoptop+ 1S[top] elemfi
Why It Rocks Why It Sucks Array-based Stack • Easy to write & read • Simple to find bugs • Quick running times • Methods take O(1) time • Array must be huge • Max. possible elements • Problems occur when too many exist at once • When full,throws specific exception
Better Approach (Maybe?) • Implement Stack using linked list • Grows & shrinks as elements added & removed • Add element in push() by allocating a Node • pop()removes Nodes from linked list in • Concerns about size limits are forgotten
Once You pop()… • Check for empty Stack • If it is, throw exception • Pop the top Node • Node’s element saved • top moved to next Node Algorithmpop()ifisEmpty()thenthrow new EmptyStackExceptionelseretValtop.getElement()toptop.getNext() size size- 1fireturnretVal
Once You pop()… Algorithmpop()ifisEmpty()thenthrow new EmptyStackExceptionelseretValtop.getElement()toptop.getNext() size size- 1fireturnretVal retVal • Check for empty Stack • If it is, throw exception • Pop the top Node • Node’s element saved • top moved to next Node
Once You pop()… retVal • Check for empty Stack • If it is, throw exception • Pop the top Node • Node’s element saved • top moved to next Node Algorithmpop()ifisEmpty()thenthrow new EmptyStackExceptionelseretValtop.getElement()toptop.getNext() size size- 1fireturnretVal
Once You pop()… retVal • Check for empty Stack • If it is, throw exception • Pop the top Node • Node’s element saved • top moved to next Node Algorithmpop()ifisEmpty()thenthrow new EmptyStackExceptionelseretValtop.getElement()toptop.getNext() size size- 1fireturnretVal
Linked list-based Stack • pushing very easy, too • Adds new top node • Easy to check if empty • Simplified w/o sentinel Algorithmpush(e)newNnewNode(e,top)topnewNsize size + 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 sentinel Algorithmpush(e)newNnewNode(e,top)topnewNsize size + 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 sentinel Algorithmpush(e)newNnewNode(e,top)topnewNsize size + 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 sentinel Algorithmpush(e)newNnewNode(e,top)topnewNsize size + 1 AlgorithmisEmpty()returntop == null
Linked list-based Stack • pushing very easy, too • Adds new top node • Easy to check if empty • Simplified w/o sentinel Algorithmpush(e)newNnewNode(e,top)topnewNsize size + 1 AlgorithmisEmpty()returntop == null
Your Turn • Get into your groups and complete activity
For Next Lecture • Read GT section 5.2 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