Download presentation
Presentation is loading. Please wait.
Published byAleesha Merritt Modified over 9 years ago
1
Problem of the Day What do you get when you cross a mountain climber and a grape?
2
Problem of the Day What do you get when you cross a mountain climber and a grape? Nothing, you cannot cross a scalar.
3
CSC 212 – Data Structures
4
Using Stack
6
public interface Stack 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
7
Array-based Implementation 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() return top == -1 Algorithm pop() if isEmpty() then throw new EmptyStackException else top top 1 return S[top + 1] 012 … ArrayStack S top -1
8
Array-based Implementation 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() return top == -1 Algorithm pop() if isEmpty() then throw new EmptyStackException else top top 1 return S[top + 1] 012 top … ArrayStack S top -1
9
top Array-based Implementation 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() return top == -1 Algorithm pop() if isEmpty() then throw new EmptyStackException else top top 1 return S[top + 1] ArrayStack S top X 012 … X
10
Array-based Implementation 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() return top == -1 Algorithm pop() if isEmpty() then throw new EmptyStackException else top top 1 return S[top + 1] ArrayStack S top (X -1) 012 … X top
11
Could fill array with data More push() s impossible Throw exception on error Specific to array-based Unchecked exception must be used Algorithm push(elem) if top == S.length 1 then throw new FullStackException else top top + 1 S[top] elem fi ArrayStack S top (X -1) 012 … X top
12
Could fill array with data More push() s impossible Throw exception on error Specific to array-based Unchecked exception must be used Algorithm push(elem) if top == S.length 1 then throw new FullStackException else top top + 1 S[top] elem fi ArrayStack S top X 012 … X top
13
Could fill array with data More push() s impossible Throw exception on error Specific to array-based Unchecked exception must be used Algorithm push(elem) if top == S.length 1 then throw new FullStackException else top top + 1 S[top] elem fi ArrayStack S top S.length-1 012 … top
14
Oops… My Bad Could fill array with data More push() s impossible Throw exception on error Specific to array-based Unchecked exception must be used Algorithm push(elem) if top == S.length 1 then throw new FullStackException else top top + 1 S[top] elem fi ArrayStack S top S.length-1 012 … top
15
Array-based Stack Why It RocksWhy It Sucks Easy to write & read Simple to find bugs Quick running times Methods take O(1) time huge Array must be huge Max. possible elements Problems occur when too many exist at once When full, throws specific exception
16
Better Approach (Maybe?) Implement Stack using linked list Grows & shrinks as elements added & removed Add element in push() by allocating a Node pop() removes Node s from linked list in Concerns about size limits are forgotten
17
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 Algorithm pop() if isEmpty() then throw new EmptyStackException else retVal top.getElement() top top.getNext() size size - 1 fi return retVal NodeStack size 6 top
18
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 retVal Algorithm pop() if isEmpty() then throw new EmptyStackException else retVal top.getElement() top top.getNext() size size - 1 fi return retVal NodeStack size 6 top
19
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 retVal Algorithm pop() if isEmpty() then throw new EmptyStackException else retVal top.getElement() top top.getNext() size size - 1 fi return retVal NodeStack size 6 top
20
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 retVal Algorithm pop() if isEmpty() then throw new EmptyStackException else retVal top.getElement() top top.getNext() size size - 1 fi return retVal NodeStack size 5 top
21
Linked list-based Stack push ing very easy, too Adds new top node Easy to check if empty Simplified w/o sentinel Algorithm push(e) newN new Node(e, top) top newN size size + 1 Algorithm isEmpty() return top == null NodeStack size 5 top
22
Linked list-based Stack push ing very easy, too Adds new top node Easy to check if empty Simplified w/o sentinel Algorithm push(e) newN new Node(e, top) top newN size size + 1 Algorithm isEmpty() return top == null e NodeStack size 5 top
23
Linked list-based Stack push ing very easy, too Adds new top node Easy to check if empty Simplified w/o sentinel Algorithm push(e) newN new Node(e, top) top newN size size + 1 Algorithm isEmpty() return top == null newN NodeStack size 5 top e
24
Linked list-based Stack push ing very easy, too Adds new top node Easy to check if empty Simplified w/o sentinel Algorithm push(e) newN new Node(e, top) top newN size size + 1 Algorithm isEmpty() return top == null newN NodeStack size 5 top e
25
Linked list-based Stack push ing very easy, too Adds new top node Easy to check if empty Simplified w/o sentinel Algorithm push(e) newN new Node(e, top) top newN size size + 1 Algorithm isEmpty() return top == null NodeStack size 6 top
26
Your Turn Get into your groups and complete activity
27
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.