Ch 3: Stacks and Queues Flashcards
Stacks and Queues:
Important Data Structures
- Stack
- Queue
- Array
- Multi-Stack
- Priority Queue
Stacks and Queues:
Important Concepts/Operations
- FIFO Ordering
- LIFO Ordering
- Implementing a Stack with an Array
- Implementing a Queue with Stacks
- Sorting Stacks
Stack:
Basic Description
- Data is stored as a “stack”, similar to a physical stack of dinner plates
- Uses Last-In, First Out (LIFO) ordering
- The most recently added item is the first removed
- Allows constant-time adds and removes, but linear-time access to nth item
Stack:
Defining Operations
- pop()
- push(item)
- peek()
- isEmpty()
Stack Operations:
pop()
Removes and returns the top item from the stack
Stack Operations:
push( item)
Adds an item to the top of the stack
Stack Operations:
peek()
Returns, but does not remove, the top item of the stack
Stack Operations:
isEmpty()
Returns true iff the stack is empty
Stacks:
Time Complexity
- O(i) time to access the ith item
- Need to pop everything above it
- O(1) time for adding and removing items
Uses for Stacks
- Storing temporary data for recursive algorithms, allows going “backward” by popping items off on the way back ‘up”
- Implementing recursive algorithms iteratively
Stack Implementation:
Basic Structure
public class MyStack<T> { private static class StackNode<T> { private T data; private StackNode<T> next; public StackNode(T data){}; } private StackNode<T> top; public T pop(){} public void push(T item){} public T peek(){} public boolean isEmpty(){} }
Stack Implementation:
pop() method
public T pop() { if (top == null) throw new EmptyStackException(); T item = top.data; top = top.next; return item; }
Stack Implementation:
push() method
public void push(T item) { StackNode<T> node = new StackNode<T>(item); node.next = top; top = node; }
Stack Implementation:
peek() method
public T peek() { if (top == null) throw new EmptyStackException(); return top.data; }
Stack Implementation:
isEmpty() method
public boolean isEmpty() { return top == null; }
Queue:
Basic Description
- Similar to a line or queue at a store
- Items are removed in the same order that they are added
- First In, First Out (FIFO) ordering
Queue:
Defining Operations
- add(item)
- remove()
- peek()
- isEmpty()
Queue Operations:
add(item) method
Adds an item to the end of the list
Queue Operations:
remove()
Removes and returns the first item in the list
Queue Operations:
peek()
Returns, but does not remove, the first item in the queue
Queue Operations:
isEmpty()
Returns true if the queue is empty, false if it contains items
Queue:
Basic Structure
public class MyQueue<T> { private static class QueueNode<T> { private T data; private QueueNode<T> next; public QueueNode(T data){ this.data = data;} } private QueueNode<T> first; private QueueNode<T> last; public void add(T item){} public T remove(){} public T peek(){} public boolean isEmpty(){} }
Queue:
Common Uses
- Breadth-First Search: Stores a list of nodes that need to be processed in order
- Implementing a cache
- Processing requests, data, events, etc in the order received
Queue Implementation:
add(T item)
public void add(T item) { QueueNode<T> node = new QueueNode<T>(item); if (last != null) last.next = node; last = node; if (first == null) first = last; }
Queue Implementation:
remove()
public T remove() { if (first == null) throw new NoSuchElementException(); T data = first.data; first = first.next; if (first == null) last = null; return data; }
Queue Implementation:
peek()
public T peek() { if (first == null) throw new NoSuchElementException(); return first.data; }
Queue Implementation:
isEmpty()
public boolean isEmpty() { return first == null; }