Ch 3: Stacks and Queues Flashcards

1
Q

Stacks and Queues:
Important Data Structures

A
  • Stack
  • Queue
  • Array
  • Multi-Stack
  • Priority Queue
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Stacks and Queues:
Important Concepts/Operations

A
  • FIFO Ordering
  • LIFO Ordering
  • Implementing a Stack with an Array
  • Implementing a Queue with Stacks
  • Sorting Stacks
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Stack:
Basic Description

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Stack:
Defining Operations

A
  • pop()
  • push(item)
  • peek()
  • isEmpty()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Stack Operations:
pop()

A

Removes and returns the top item from the stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Stack Operations:
push( item)

A

Adds an item to the top of the stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Stack Operations:
peek()

A

Returns, but does not remove, the top item of the stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Stack Operations:
isEmpty()

A

Returns true iff the stack is empty

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Stacks:
Time Complexity

A
  • O(i) time to access the ith item
  • Need to pop everything above it
  • O(1) time for adding and removing items
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Uses for Stacks

A
  • Storing temporary data for recursive algorithms, allows going “backward” by popping items off on the way back ‘up”
  • Implementing recursive algorithms iteratively
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Stack Implementation:
Basic Structure

A
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(){}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Stack Implementation:
pop() method

A
public T pop() {

    if (top == null)
        throw new EmptyStackException();
    
    T item = top.data;
    top = top.next;
    return item;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Stack Implementation:
push() method

A
public void push(T item) {

    StackNode<T> node = new StackNode<T>(item);
    node.next = top;
    top = node;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Stack Implementation:
peek() method

A
public T peek() {

if (top == null)
        throw new EmptyStackException();
    
    return top.data;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Stack Implementation:
isEmpty() method

A
public boolean isEmpty() {
        return top == null;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Queue:
Basic Description

A
  • 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
17
Q

Queue:
Defining Operations

A
  • add(item)
  • remove()
  • peek()
  • isEmpty()
18
Q

Queue Operations:
add(item) method

A

Adds an item to the end of the list

19
Q

Queue Operations:
remove()

A

Removes and returns the first item in the list

20
Q

Queue Operations:
peek()

A

Returns, but does not remove, the first item in the queue

21
Q

Queue Operations:
isEmpty()

A

Returns true if the queue is empty, false if it contains items

22
Q

Queue:
Basic Structure

A
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(){}
}
23
Q

Queue:
Common Uses

A
  • 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
24
Q

Queue Implementation:
add(T item)

A
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;
}
25
Q

Queue Implementation:
remove()

A
public T remove() {
  
    if (first == null) 
        throw new NoSuchElementException();
    
    T data = first.data;
    first = first.next;
    if (first == null) last = null;
		
    return data;
}
26
Q

Queue Implementation:
peek()

A
public T peek() {

    if (first == null)
		throw new NoSuchElementException();
		
    return first.data;
}
27
Q

Queue Implementation:
isEmpty()

A
public boolean isEmpty() {
    return first == null;
}