0% found this document useful (0 votes)
50 views

DSA Queue & Its Application

The document discusses different types of queues and their applications. It defines a queue as a first-in, first-out (FIFO) data structure with two ends - a front and rear. Elements are inserted at the rear and deleted from the front. Common queue operations are enqueue, dequeue, and display. Queues can be implemented using arrays or linked lists. Variations include linear, circular, priority, and double-ended queues. Queues are used in data buffering, asynchronous data transfer, shared resource allocation, and traffic analysis. The document provides examples of implementing a linear queue using both an array and linked list.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

DSA Queue & Its Application

The document discusses different types of queues and their applications. It defines a queue as a first-in, first-out (FIFO) data structure with two ends - a front and rear. Elements are inserted at the rear and deleted from the front. Common queue operations are enqueue, dequeue, and display. Queues can be implemented using arrays or linked lists. Variations include linear, circular, priority, and double-ended queues. Queues are used in data buffering, asynchronous data transfer, shared resource allocation, and traffic analysis. The document provides examples of implementing a linear queue using both an array and linked list.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

QUEUE & ITS

APPLICATION
Queue
• It’s a linear as well as non-primitive data structure.
• It’s an ordered collection of items that works upon a simple formula called as FIFO (First In First Out).
• There are 2 ends: front & rear.
• Elements are inserted at rear and deleted from front.
• Queue can be a homogeneous/non-homogeneous, static/dynamic data structure.
• Queue can be created using Array and Linked List.
• Queue has 3 operations:
• EnQueue (insert): check for queue full/overflow
• DeQueue (delete): check for queue empty/underflow Front Rear
• Display/Peek
• Variants of Queue:
1. Linear Queue 2. Circular Queue Queue
3. Priority Queue 4. Double-ended Queue
Stack Queue
• LIFO (Last In First Out). • FIFO (First In First Out).
• There is only 1 end: top. • There are 2 ends: front & rear.
• Elements are inserted and deleted from top. • Elements are inserted at rear and deleted from front.
• Stack has 3 operations: • Queue has 3 operations:
• Push (insert): check for stack full/overflow • Enqueue (insert): check for queue full/overflow
• Pop (delete): check for stack empty/underflow • Dequeue (delete): check for queue empty/underflow
• Display/Peek • Display/Peek
• Applications of Stack • Applications of Queue
• Parsing in a compiler • Data Buffers
• Java virtual machine (JVM) • Asynchronous data transfer (file IO, pipes, sockets)
• Back button in a Web browser • Allotting requests on a shared resource (printer,
• Implementing function calls in a compiler processor)
• Traffic analysis
• Determine the number of cashiers to have at a
supermarket
Linear Queue using Array
Queue declaration: public static final int MAXSIZE = 4;
int queue [ ] = new int [MAXSIZE]; // Queue creation
Queue empty: public static int front = -1; // front & rear declaration
public static int rear = -1;
queue empty
Queue full: if (rear = = MAXSIZE - 1)
front = 0 rear = 3
67 15 34 90
void display(int queue[ ])
{ 0 1 2 3
System.out.println ("Elements present in queue."); Queue
for (int i = front; i <= rear; i++)
System.out.println (queue[i]);
}
void insert (int queue[ ]) Linear Queue using Array
{ boolean isFull ()
if (isFull ()) {
System.out.println (“Queue is Full!"); if (rear = = MAXSIZE - 1)
else return true;
{ else
Scanner sc = new Scanner (System.in); return false;
System.out.println ("Enter element"); }
rear + +;
queue [rear] = sc.nextInt(); Note: If the Queue contains a single element, then both
} front & rear is at 0.
if (rear = = 0) front = 0;
front = 0 rear = 1
}
0 1 2 3
20 20 89
Insert 89
Insert 20 0 1 2 3 Insert 96

1 2 3 front = 0 front = 0 rear = 3 front = 0 rear = 2


0
front = -1 rear = 0 0 1 2 3 0 1 2 3
rear = -1 Empty
20 89 96 45 20 89 96
Insert 45
Full
void delete (int queue[ ]) Linear Queue using Array
{
if (isEmpty ()) boolean isEmpty ()
System.out.println ("Underflow!"); {
else if (front = = -1 || front > rear || rear = = -1)
{ return true;
System.out.println ("Deleted Element is "+ queue[front]); else
front + +; return false;
} }
}

front = 1 rear =3
front = 0 rear = 3 delete delete front = 2
89 96 45 rear = 3
20 89 96 45 2 3
0 1 96 45
0 1 2 3 front = 3
front = 4 rear = 3 0 1 2 3
rear = 3
45 delete
delete 0 1 2 3
0 1 2 3
Empty
Linear Queue using Linked List
Node declaration: class node
{
int info;
node next;
}

Queue empty: node front = null; // front declaration

No Queue full condition is there except when node creating heap area is full

Node Insertion: just like inserting a new node at the end of the linked list

Node Deletion: just like deleting a node from the beginning of the linked list
Linked Queue using Linked List void insert (node front)
{
void delete (node front) Scanner sc = new Scanner(System.in);
{ node temp = new node();
if (front = = null) node s = new node();
System.out.println (“List Empty"); temp.info = sc.nextInt();
else temp.next = null;
{ if (front = = null)
System.out.println ("Deleted Element is "+ front); {
front = front.next; front = temp;
} }
} else
void display (node front) {
{ s = front;
node s = front; while (s.next != null)
while (s != null) {
{ s = s.next;
System.out.println (s.info); }
s = s.next; s.next = temp;
} }
} }
Linear Queue vs Circular Queue
• In a Linear queue, once the queue is completely full, it's not possible to insert
more elements.
• Even if we delete some elements, no new elements can be inserted.
• Because, we are moving the front of the queue forward and we cannot insert
new elements, because the rear pointer is still at the end of the queue.
• So, the Circular Queue is used to overcome this issue, which also uses FIFO
(First In First Out).
• The last position is connected back to the first position to make a circle.
• Application of Circular Queue:
• Computer controlled Traffic Signal System uses circular queue
• CPU scheduling and Memory management
Circular Queue
Queue declaration: public static final int MAXSIZE = 3;
int circ_queue [ ] = new int [MAXSIZE]; // Queue creation

Queue empty: public static int front = -1; // front & rear declaration
public static int rear = -1; circular queue empty

• While inserting (enqueuing), we circularly increase the value of REAR index and place the new element in the
position pointed to by REAR.
• While deleting (dequeuing), we return the value pointed by FRONT and circularly increase the FRONT index.
• Before enqueuing, we check if the queue is already full.
• Before dequeuing, we check if the queue is already empty.
• When enqueuing the first element, we set the value of FRONT to 0.
• When dequeuing the last element, we reset the values of both FRONT and REAR to -1.
Circular Queue

1 1 0
0 1 0 1
0

2 2
2 2
4 4 4 4
3 3 3 3

0 1 0 1
1 0 1 1 0
0

2
2 2 2 2
4 4 4 4 4
3 3 3 3 3
Circular Queue public static void insert (int circ_queue[])
{
public static void display (int circ_queue[]) if (isFull())
{ System.out.println (“Circular Queue is Full!");
int i; else
if (isEmpty()) {
System.out.println ("Empty Circular Queue"); if(front = = -1)
else front = 0;
{ Scanner sc = new Scanner(System.in);
System.out.println ("Items are "); System.out.println ("Insert element");
for (i = front; i != rear; i = (i + 1) % MAXSIZE) rear = (rear + 1) % MAXSIZE;
System.out.println (circ_queue [i] + " "); queue [rear] = sc.nextInt();
System.out.println (circ_queue [i]); }
} }
} public static boolean isFull()
{
if ((front = = 0 && rear = = MAXSIZE - 1) || (front = = rear + 1))
return true;
else
return false;
}
Circular Queue
public static void delete (int circ_queue[]) public static boolean isEmpty()
{ {
if (isEmpty()) if (front = = -1)
System.out.println (“Circular Queue is Empty!"); return true;
else else
{ return false;
System.out.println ("Deleted "+ circ_queue [front]); }
if (front = = rear)
{
front = - 1;
rear = - 1;
}
else
front = (front + 1) % MAXSIZE;
}
}

You might also like