





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Howe; Class: Algorithms and Data Structures; Subject: Computer Science; University: Colorado State University; Term: Unknown 1989;
Typology: Study notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!






order of removal based on order of insertion
“first come,first serve” first in first out - FIFO “take from the top of the pile” last in first out - LIFO
a pile of papers a stack of plates in a cafe
both from top – head of list in a typical implementation
top
Empty? Retrieve -- get first item
Push -- add with constraint on position
Pop -- remove with constraints Remove all
Stack Methods
adds a new item to the top of the stack Exception when insertion fails
deletes the top item from the stack and returns it Exception when deletion fails
returns the top item from the stack, but does not remove it Exception when retrieval fails Stack Application - Runtime Stack
First method that returns is the last one invoked which returns to the second to the last one…
parameters and return values local variables return address: pointer to next instruction to be executed in calling method Stack Application - Depth First Search
Stack application - Prefix expressions How is this implemented?
Stack Implementation - Linked List (cont.) public Object pop() throws StackException { if (!isEmpty()) { Node temp = top; top = top.getNext(); return temp.getItem(); } else { throw new StackException(…); } } Stack Implementation – Singly Linked List
use add(1) [ADT List] worst case complexity: O(1)
use remove(1) [ADT List] worst case complexity: O(1)
use get(1) [ADT List]
Stack API in Java public class Stack
add to tail, remove from head (typical)
Queue Methods
adds a new item to the tail of the queue Throws exception if not successful
deletes the first item from the head of the queue and returns it Throws exception if not successful
returns the first item from the head of the queue, but does not remove it Throws exception if not successful Queue Implementation - Using ArrayList
use add - adds to the end of the ArrayList
use get(0) to get return value use remove(0) to destructively modify the queue
use get(0)
Naïve Array-Based Implementation Drift can cause the queue to appear full How do we initialize front and back? (Hint: how does a queue with a single element look like) Queue Implementation - Using Arrays
number of elements in queue head index
add to next open position increment count
remove from head by incrementing head decrement count
use head index
Determining Full in Array-Based Queue back catches up to front when the queue becomes full
Other Approaches
Queue Implementation - Using DLL DLL is symmetric two choices: enqueue at head or tail Enqueue use add(0) method Dequeue use remove method at size Peek use get method How can we use SLL without losing on order of complexity? Reference-Based Queue Implementation A linked list with two external references
At which end do we dequeue?
Reference-Based Queue Implementation II A circular linked list with one external reference
Reference-Based Queue II: Enqueue Enqueueing an item into a non-empty queue Reference-Based Queue II: Enqueue (cont.) Enqueueing an item into an empty queue Reference-Based Implementation II: Dequeue