IT245 - Module 5
IT245 - Module 5
IT245 - Module 5
26/12/2021
1
College of Computing and
Informatics
Data Structure
2
Data Structure
Module 5
Queue
3
1. Queue Model
2. Array implementation of Queue
3. Different Operations on Queue
4. Linked list implementation of Queue
5. Applications of Queue
Contents
4
1. Understand Queue Abstract Data Type Model
5
Required Reading
1. Chapter 3 (Data structures and algorithm
analysis in Java by Mark Allen Weiss)
Recommended Reading
1. Chapter 10 (al, Cormen Thomas H et. Introduction to
Algorithms. Cambridge, MA: MIT Press, 2009)
2. Queue Class:
https://docs.oracle.com/javase/7/docs/api/java/util/Queue.h
tml
3. Java Queue: 6
• Queue Model
7
Queue Model
8
Queue Model
9
Queue Model
Reference: https://www.geeksforgeeks.org/queue-data-structure/
10
• Array implementation of Queue
11
Array implementation of Queue
Like stacks, both the linked list and array implementations give fast O(1) running
times for every operation.
12
Array implementation of Queue
13
Array implementation of Queue
14
Array implementation of Queue
The simple solution is that whenever front or back gets to the end of the array,
it is wrapped around to the beginning.
15
Array implementation of Queue
• Circular array:
16
Array implementation of Queue
17
Array implementation of Queue
18
Array implementation of Queue
19
Array implementation of Queue
20
Array implementation of Queue
Reference: Chapter 10 (al, Cormen Thomas H et. Introduction to Algorithms. Cambridge, MA: MIT Press, 2009)
21
Array implementation of Queue
• Simple Algorithm:
Reference: Chapter 10 (al, Cormen Thomas H et. Introduction to Algorithms. Cambridge, MA: MIT Press, 2009)
22
• Different Operations on Queue
23
Different Operations on Queue
24
Different Operations on Queue
• The following code shows the different operations on Queue:
(https://www.geeksforgeeks.org/array-implementation-of-queue-simple/)
25
Different Operations on Queue
26
Different Operations on Queue
27
• Linked list implementation of Queue
28
Linked list implementation of Queue
package org.arpit.java2blog;
• Java Program to implement public class QueueUsingLinkedListMain
Queue using Linked List:
{
• ( private Node front, rear;
https://java2blog.com/implement-q
ueue-using-linked-list-in-java/ private int currentSize; // number of items
) //class to define linked node
private class Node
{
int data;
Node next;
}
//Zero argument constructor
public QueueUsingLinkedListMain()
{
front = null;
rear = null;
currentSize = 0;
}
29
Linked list implementation of Queue
public boolean isEmpty()
• Java Program to implement {
Queue using Linked List: return (currentSize == 0);
• ( }
https://java2blog.com/implement-q
ueue-using-linked-list-in-java/ //Remove item from the beginning of the list.
) public int dequeue()
{
int data = front.data;
front = front.next;
if (isEmpty())
{
rear = null;
}
currentSize--;
System.out.println(data+ " removed from the queue");
return data;
}
30
Linked list implementation of Queue
//Add data to the end of the list.
• Java Program to implement public void enqueue(int data)
Queue using Linked List: {
31
Linked list implementation of Queue
33
Applications of Queue
34
Applications of Queue
Reference: https://examples.javacodegeeks.com/java-queue-example/
37
Applications of Queue
Reference: http://math.oxford.emory.edu/site/cs171/probSetUsingQueues/
38
Applications of Queue
import java.util.Scanner;
public class BinaryCounter {
public static void main(String[] args) {
private Queue<String> q;
System.out.println("Count in binary to what decimal value?");
public void countTo(int n) {
Scanner scanner = new Scanner(System.in);
q = new QueueArray<String>();
q.enqueue("1"); int n = scanner.nextInt();
39
References
40
Thank
You
41