Lecture 13:
Queues
CS 136: Spring 2024
Katie Keith
📣 Announcements
🎯 Today’s Learning Objectives
📚Readings
Review Stack
A stack is a linear collection of elements with operations based on a last-in-first-out (LIFO) policy. A push operation adds an element to the stack and a pop operation removes an element.
Review: Example Stack Use: Back button
Review: ADT vs. Data Structures
Abstract Data Type (ADT) | Data Structures |
List | (1) List implemented with arrays (2) List implemented with singly-linked lists |
Stack (Last-In-First-Out) | (1) Array-based stack (2) Stacks with Singly-Linked List |
Queue (First-In-First-Out) | (1) Array-based queue (2) Queue with Singly-Linked Lists (3) Queue with Doubly-Linked Lists |
Last week
Today!
This past Monday
Today!
Lab 4!
I am going to use an array to implement a Stack ADT.
I want to be able to dynamically grow the size of my stack so I implement the following:
Example array
💡Think-pair-share
StackWithArrays.java
💻
“Ugly” Cast 😞
s = (Item[]) new Object[capacity];
😞
Java 8 doesn’t allow direct creation of generic arrays
✅
🎯 Today’s Learning Objectives
Queue
A queue is a linear collection of elements with operations based on a first-in-first-out (FIFO) policy.
We use operations enqueue (add to the back of the queue) and dequeue (remove from the front of the queue).
Queue in the “real world”
Queue interface
public interface Queue<Item>{
// Returns true if the queue is empty
public abstract boolean isEmpty();
// Returns the number of items in the queue
public abstract int size();
// Adds an item to the end of the queue
public abstract void enqueue(Item item);
// Returns the first in the queue (and removes that item from the queue)
public abstract Item dequeue();
// Returns the element that is first in the queue (but does NOT remove it)
public abstract Item peek();
}
Example Queue Use: Printing jobs
For a printer, jobs submitted to the printer are stored in a queue. Then, the first job sent to the printer is the first one printed (first-in-first-out).
Queue<Integer> q = new Queue<Integer>();
q.enqueue(0);
q.enqueue(1);
for (int i =0; i<=n; i++){
int a = q.dequeue();
int b = q.dequeue();
q.enqueue(b);
q.enqueue(a + b);
System.out.println(a);
}
💡Think-pair-share
✅
✅
🎯 Today’s Learning Objectives
Design sketch: Implementing a Queue with Arrays
Can wrap around!
Next available slot
QueueWithArrays.java
💻
Wrapping around: Enqueue
public void enqueue(Item item) {
if(n == q.length){
resize(GROWTH_FACTOR*q.length);
}
q[tail++] = item;
if (tail == q.length){
tail = 0;
}
n++
}
null
null
“b”
“c”
“a”
We enqueued this item
tail == 0
Next item will go here
head == 2
Wrapping around: Dequeue
public Item dequeue() {
Item item = q[head];
n --;
head ++;
if(head == q.length){
head = 0;
}
return item;
}
null
null
“b”
“c”
tail == 1
head == q.length -1
“a”
Before dequeue
After dequeue
null
null
“b”
“c”
null
tail == 1
head == 0
Suppose we declare and create a queue q and enqueue to it a number of Strings.
What does the following code fragment do to the queue q?
Stack<String> s = new Stack<String>();
while(!q.isEmpty()){
s.push(q.dequeue());
}
while(!s.isEmpty()){
q.enqueue(s.pop());
}
“a”
“b”
“c”
“d”
head of queue
tail of queue
💡Think-pair-share
✅
✅
✅
🎯 Today’s Learning Objectives
Design Sketch: Implementing a Stack with Arrays
Design sketch: Implementing a Queue with Arrays