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

Queue

Queue is an abstract data structure that follows the FIFO (First In First Out) principle. Elements are inserted at the rear end and deleted from the front end. Like stacks, queues can be implemented using arrays, linked lists, or pointers. Circular queues overcome the limitations of linear queues by connecting the rear end to the front so that the queue space is used efficiently. Common operations on queues include enqueue, dequeue, and peek.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Queue

Queue is an abstract data structure that follows the FIFO (First In First Out) principle. Elements are inserted at the rear end and deleted from the front end. Like stacks, queues can be implemented using arrays, linked lists, or pointers. Circular queues overcome the limitations of linear queues by connecting the rear end to the front so that the queue space is used efficiently. Common operations on queues include enqueue, dequeue, and peek.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

QUEUE

Queue, like Stack, is also an abstract data structure. The thing that makes queue different from stack is that a queue
is open at both its ends. Hence, it follows FIFO (First-In-First-Out) structure, i.e. the data item inserted first will also
be accessed first. The data is inserted into the queue through one end and deleted from it using the other end. Similar
to the stack ADT, a queue ADT can also be implemented using arrays, linked lists, or pointers.

Applications of Queue

1. Due to the fact that queue performs actions on first in first out basis which is quite fair for the ordering of
actions. There are various applications of queues discussed as below. Queues are widely used as waiting
lists for a single shared resource like printer, disk, CPU.
2. Queues are used in asynchronous transfer of data (where data is not being transferred at the same rate
between two processes) for eg. pipes, file IO, sockets.
3. Queues are used as buffers in most of the applications like MP3 media player, CD player, etc.
4. Queue are used to maintain the play list in media players in order to add and remove the songs from the
play-list.
5. Queues are used in operating systems for handling interrupts.

Complexity

Data Time Complexity Space


Structur Compleity
e

Average Worst Worst

Access Searc Insertion Deletion Acces Search Insertion Deletio


h s n

Queue θ(n) θ(n) θ(1) θ(1) O(n) O(n) O(1) O(1) O(n)

Types of Queue

o Simple Queue or Linear Queue


o Circular Queue
o Priority Queue
o Double Ended Queue (or Deque)
o Simple Queue or Linear Queue: In Linear Queue, an insertion takes place from one end while the
deletion occurs from another end. The end at which the insertion takes place is known as the rear end, and
the end at which the deletion takes place is known as front end. It strictly follows the FIFO rule.

The major drawback of using a linear Queue is that insertion is done only from the rear end. If the first three
elements are deleted from the Queue, we cannot insert more elements even though the space is available in a Linear
Queue. In this case, the linear Queue shows the overflow condition as the rear is pointing to the last element of the
Queue.

o Circular Queue: In Circular Queue, all the nodes are represented as circular. It is similar to the linear
Queue except that the last element of the queue is connected to the first element. It is also known as Ring
Buffer, as all the ends are connected to another end. The representation of circular queue is shown in the
below image -

The drawback that occurs in a linear queue is overcome by using the circular queue. If the empty space is available
in a circular queue, the new element can be added in an empty space by simply incrementing the value of rear. The
main advantage of using the circular queue is better memory utilization.

Circular Queue works by the process of circular increment i.e. when we try to increment the pointer and we reach
the end of the queue, we start from the beginning of the queue. Here, the circular increment is performed by modulo
division with the queue size. That is,

if REAR + 1 == 5 (overflow!), REAR = (REAR + 1)%5 = 0 (start of queue)

Operations performed on queue: The fundamental operations that can be performed on queue are listed as follows

o Enqueue: The Enqueue operation is used to insert the element at the rear end of the queue. It returns void.
o Dequeue: It performs the deletion from the front-end of the queue. It also returns the element which has
been removed from the front-end. It returns an integer value.
o Peek: This is the third operation that returns the element, which is pointed by the front pointer in the queue
but does not delete it.
o Queue overflow (isfull): It shows the overflow condition when the queue is completely full.
o Queue underflow (isempty): It shows the underflow condition when the Queue is empty, i.e., no elements
are in the Queue.

There are two ways of implementing the Queue:

o Implementation using array: The sequential allocation in a Queue can be implemented using an array.
o Implementation using Linked list: The linked list allocation in a Queue can be implemented using a
linked list.

Algorithm

 Insertion operation: enqueue(): The enqueue() is a data manipulation operation that is used to insert
elements into the stack. The following algorithm describes the enqueue() operation in a simpler way.

Algorithm
1 − START
2 – Check if the queue is full.
3 − If the queue is full, produce overflow error and exit.
4 − If the queue is not full, increment rear pointer to point the next empty space.
5 − Add data element to the queue location, where the rear is pointing.
6 − return success.
7 – END

 Deletion Operation: dequeue(): The dequeue() is a data manipulation operation that is used to remove
elements from the stack. The following algorithm describes the dequeue() operation in a simpler way.

Algorithm
1 – START
2 − Check if the queue is empty.
3 − If the queue is empty, produce underflow error and exit.
4 − If the queue is not empty, access the data where front is pointing.
5 − Increment front pointer to point to the next available data element.
6 − Return success.
7 – END

 The peek() Operation:The peek() is an operation which is used to retrieve the frontmost element in
the queue, without deleting it. This operation is used to check the status of the queue with the help of
the pointer.

Algorithm
1 – START
2 – Return the element at the front of the queue
3 – END
Queue Implementation:

// Queue implementation in C

#include <stdio.h>
#define SIZE 5

void enQueue(int);
void deQueue();
void display();

int items[SIZE], front = -1, rear = -1;

int main() {
//deQueue is not possible on empty queue
deQueue();

//enQueue 5 elements
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);

// 6th element can't be added to because the queue is full


enQueue(6);

display();

//deQueue removes element entered first i.e. 1


deQueue();

//Now we have just 4 elements


display();

return 0;
}

void enQueue(int value) {


if (rear == SIZE - 1)
printf("\nQueue is Full!!");
else {
if (front == -1)
front = 0;
rear++;
items[rear] = value;
printf("\nInserted -> %d", value);
}
}

void deQueue() {
if (front == -1)
printf("\nQueue is Empty!!");
else {
printf("\nDeleted : %d", items[front]);
front++;
if (front > rear)
front = rear = -1;
}
}

// Function to print the queue


void display() {
if (rear == -1)
printf("\nQueue is Empty!!!");
else {
int i;
printf("\nQueue elements are:\n");
for (i = front; i <= rear; i++)
printf("%d ", items[i]);
}
printf("\n");
}

Circular Queue Implementation:

// Circular Queue implementation in C

#include <stdio.h>

#define SIZE 5

int items[SIZE];
int front = -1, rear = -1;

// Check if the queue is full


int isFull() {
if ((front == rear + 1) || (front == 0 && rear == SIZE - 1)) return 1;
return 0;
}

// Check if the queue is empty


int isEmpty() {
if (front == -1) return 1;
return 0;
}

// Adding an element
void enQueue(int element) {
if (isFull())
printf("\n Queue is full!! \n");
else {
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
items[rear] = element;
printf("\n Inserted -> %d", element);
}
}

// Removing an element
int deQueue() {
int element;
if (isEmpty()) {
printf("\n Queue is empty !! \n");
return (-1);
} else {
element = items[front];
if (front == rear) {
front = -1;
rear = -1;
}
// Q has only one element, so we reset the
// queue after dequeing it. ?
else {
front = (front + 1) % SIZE;
}
printf("\n Deleted element -> %d \n", element);
return (element);
}
}

// Display the queue


void display() {
int i;
if (isEmpty())
printf(" \n Empty Queue\n");
else {
printf("\n Front -> %d ", front);
printf("\n Items -> ");
for (i = front; i != rear; i = (i + 1) % SIZE) {
printf("%d ", items[i]);
}
printf("%d ", items[i]);
printf("\n Rear -> %d \n", rear);
}
}

int main() {
// Fails because front = -1
deQueue();

enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);

// Fails to enqueue because front == 0 && rear == SIZE - 1


enQueue(6);

display();
deQueue();

display();

enQueue(7);
display();

// Fails to enqueue because front == rear + 1


enQueue(8);

return 0;
Circular Queue Complexity Analysis

The complexity of the enqueue and dequeue operations of a circular queue is O(1) for (array implementations).

Applications of Circular Queue

 CPU scheduling

 Memory management

 Traffic Management

You might also like