Lecture 14 - Queue Implementation
Lecture 14 - Queue Implementation
Data Structures
3
Today …
4
Lecture Objectives
5
1. Queue Review
● A linear data structure, uses FIFO principle!
• Uses FIFO: First In First Out!
• Has front/head & tail/rear
● Inserting an element Dequeue
(remove)
front tail
• AKA enqueue
c y b m
• Elements join at the tail
Enqueue
● Removing an element (add)
• AKA dequeue
• Elements exit from front
● Many queue application areas, e.g.
• printer queue, keystroke queue, CPU processes, etc. 6
Illustration of Queue methods
Assuming an empty queue:
front tail
a) enqueue (19) 19
b) enqueue (28) 19 28
c) enqueue (35) 19 28 35
d) dequeue() 19 28 35
7
Exercise 1
8
Types of queues
a) Normal queue (FIFO) – standard queue
0 1 2 3 4 5 15
How does enqueue() affect front and tail?
front tail
front tail
1st dequeue(): 26 69 48
0 1 2 3 4 5
front front tail
2nd dequeue(): 26 69 48
0 1 2 3 4 5
◦ Note:
◦ dequeue() removes item from the queue, NOT the array
◦ Happens in front
◦ front will keep on increasing until it reaches tail
◦ Simultaneously, size will keep on reducing until 0 (Empty!)
◦ At this point: set both front & tail to 0 at next enqueue() 17
Initial look at our Queue implementation
● Let us call our class Queue1
● It will store String values for simplicity
public class Queue1{
// instance variable
private String[] queue; // to hold elements
private int front; // indexes front element
private int tail; // indexes tail element
// Rest of methods - to be added as we build the class
}
18
Let us now writing Queue methods
i. Constructor
● Creates an array of a specified size
● Set queue spaces to size of the array
● Initialises both front & tail to -1 // for empty queue
public Queue1(int n){
queue = new String[n];
front = -1;
tail = -1;
}
19
ii. size()
• Returns number of elements in a Queue
• Take-home: Loop from the head to tail node, count
nodes individually!
20
iii. isEmpty()
• Checks if Queue has elements or not
• Method MUST be invoked before removing, peeking
& printing items,
21
iv. isFull()
• Checks if the Queue is full
• Needed because we are using an array (Rem: fixed
size?)
• MUST be invoked before adding elements
24
vii. peek()
• Returns element in front, or null if there is none
public String peek(){
if (!isEmpty())
return queue[front];
else
return null; // Queue empty (no front element)
}
25
viii. search()
• Accepts a String value, and returns true when found,
and false otherwise
public boolean search(String key) { if (queue[i].equals(key))
if (isEmpty()) return true;
System.out.println(“Empty."); i++;
else { } //while
int i=0; } //else
while (i<n) { return false;
}
26
Incomplete sample tester class
Queue1 q= new Queue1(4); q.dequeue(); // remove elements
// add elements q.dequeue();
q.enqueue(“CSI247”); q.dequeue();
e.enqueue(“CSI243”); System.out.println(“Size: ”+ q.size());
q.enqueue(“CSI262”); // search
q.enqueue(“CSI142”); System.out.println(“Found: ”+
System.out.println(q1.size()); q.search(“CSI247”));
System.out.println(q1.isEmpty());
q1.enqueue("CSI141");
System.out.println(q1.size());
System.out.println(q1.peek());
27
Exercise 2
a) Type all code segments provided into a Java file
b) Compile the program.
• Fix errors, if any
c) Implement a tester class and test all methods
implemented in Queue class
d) Implement an array-based Queue that works with data
of any valid data type
• Generics!!
28
b) Implementing Queue using a Linked List
● Rem:
• LinkedList is a collection of Nodes
• Always create a Node before inserting new element!
• With LinkedList, there is no:
• Size restriction: List grows and shrinks dynamically
• Queue capacity
• isFull()
• As a queue:
• Insert: At the tail
• Remove: At the front
29
LinkedList Queue implementation
● Let us call our class Queue2
● It will store String values for simplicity
public class Queue2{
private class Node {
// … Node instance variables
// … Node methods
}
// … Queue2 instance variables
// … Queue2 methods
}
30
● Node class
• Rem: LinkedList keeps Nodes
• We need this inner class!
private class Node{
String data; // node's data part
Node next; // a pointer to next node in the Queue
Node(String data) {
this.data = data;
this.next = null; // the last Node in the list
}
} 31
● Queue class
• Common Queue operations (methods) to implement:
• enqueue(): inserts an element (back)
• dequeue(): removes an element (front)
• peek(): returns the element in front of queue
• Item is NOT removed!!!
• isEmpty(): Checks if queue has elements
• Cannot dequeue if empty!
• isFull(): Checks if queue is full
• size(): returns length of the queue
• search(): checks existence of some item in the queue
32
i. Instance variables (For storing data)
public Node front; // tracks front node
public Node tail; // tracks last node
public Queue2(){
// Initially, queue has no Nodes
front = null;
tail = null;
}
35
ii. size()
• Returns number of nodes in the Queue
public int size(){
// add code to count nodes here!
}
iii. isEmpty()
• Checks if Queue has elements or not
public boolean isEmpty(){
return size() == 0;
} 36
iv. enqueue()
• Add an element to the Queue
41
Exercises
a) Type all code segments provided into a Java file
b) Compile it.
• Fix errors, if any
c) Implement a tester class and test all the methods
d) Implement a generic LinkedList-based Queue class
e) Implement a tester class to test methods implemented in
d) above, using UB student names
f) Implement another tester class to test methods
implemented in d) above, using Dog class objects
42
Next lesson …
43
Q &A
1644