Queue
Queue
Car lined in Toll Gate. Luggage kept on conveyor belts in the Airport.
A B C D E A B C D E F B C D E F
Insert an element Delete an element
Initial Queue
(ENQUEUE) (DEQUEUE)
For Deletion, the element at front position is deleted and the value of front
is incremented by 1.
When insertion is done in an initially empty queue, i.e. if the value of front
is -1, then value of front is made 0.
Queue is empty:
front = = -1 || front = = rear+1
Queue is full:
rear = = MAX-1
Basic Operations on Queue
In order to make manipulations in a stack, there are certain operations
provided to us.
void display( )
{
int i;
if(isEmpty())
{
printf(“queue is empty\n”);
return;
}
printf(“Queue elements are: ”);
for(i=front; i<=rear; i++)
printf(“%d\n”, queue_arr(i));
printf(“\n”);
}
Applications of Linear Queue
The major applications of the queue data structure are:
3. Used as buffers
Queues are useful as a buffer to store the data primarily whenever required. For example, the
queue is used as a buffer in the case of MP3 players. CD players, etc.
4. Interrupt handling
Whenever a process gets interrupted, it is sent back and made to wait in the queue. Thus, queue
helps in interrupt handling.
5. Maintain playlists
Queues help maintain any kinds of lists. We can maintain a playlist of songs or other media with
the help of queues.
Disadvantages of array implementation
• The main advantage of using a linked list over arrays is that it is possible to
implement a queue that can shrink or grow as much as needed.
struct node
{
int info;
struct node *link;
};
struct node *front = NULL;
struct node *rear = NULL;
(a) Empty Queue Enqueue(5) Enqueue(10)
front= -1 rear= -1 front rear front rear
5 5 10
Enqueue(15) Dequeue
front rear front rear
5 10 15 10 15
Enqueue(20) Dequeue
front rear front rear
10 15 20 15 20
Enqueue operation: Enqueue( ) insert an item to the queue at the end of the
linked list.
void enqueue(int item)
{
struct node *tmp;
tmp = (struct node*)malloc(sizeof(struct node));
if(tmp = = NULL) /*Memory is full*/
{
printf(“memory not available\n”);
return;
}
tmp -> info = item;
tmp->link = NULL;
if(front = = NULL)
front = tmp;
else
rear->link = tmp;
rear = tmp;
}
Enqueue(5) Enqueue(10)
front rear front rear
5 5 10
Dequeue operation: Dequeue( ) delete an item to the queue from the front side.
If the queue is already empty, in that case, we return an underflow error. If the
queue is not empty, we delete the front node and move the front pointer to the
next node.
int Dequeue( )
{ front rear
struct node * tmp;
int item;
10 15 20
if(isEmpty())
{
printf(“Queue underflow”); Dequeue
return; front rear
}
tmp = front; 15 20
item = tmp->info;
front = front->1ink;
free(tmp);
return item;
}
Peek Operation:
int peek( )
{
if(isEmpty())
{
printf(“Queue underflow”);
return;
}
return front->info;
}
[0]
20 [2]
30 [1]
[3]
Note: After the (n-1)th position, 0th position
occurs. We can be inserted at 0th position. 25
[4]
(a) enqueue 30
front =3 rear=0
20 [2]
30 20 25
[0] [1] [2] [3] [4] [3]
Insertion and deletion operations in a circular queue can be performed in a
manner similar to that of queue but we have to take care of two things.
When the only element of the queue is deleted, front and rear are reset
to -1. We can check for empty queue just by checking the value of front,
if
front == -1 then queue is empty.
}
Dequeue operation: Dequeue( ) delete an item to the queue from the front side.
int dequeue()
{
int item;
if(isEmpty()) //If the queue is empty
{
printf(“Queue Underflow");
return 0;
}
item = cqueue_arr[front];
if(front = = rear) //If queue has only one element
{
front = -1;
rear = -1;
}
else if(front == MAX-1)
front = 0;
else
front = front + 1;
return item;
}
Peek Operation: This operation returns the value of the data element at the
front of the queue without deleting it.
int peek( )
{
if(isEmpty())
{
printf(“Queue underflow”);
return;
}
return cqueue_arr[front];
}
isEmpty( ) Operation
int isEmpty( )
{
if(front == -1)
return 1;
else
return 0;
}
isFull( ) Operation
int isFull( )
{
if(front == 0 && rear == MAX-1)|| (front == rear+1))
return 1;
else
return 0;
}
display( ): print all elements of the circular queue.
void display( )
{
int i;
if(isEmpty())
{
printf(“queue is empty\n”);
return;
}
printf(“Queue elements are: ”);
i = front;
if (front < = rear)
{
while(i<=rear)
printf(“%d\n”, cqueue_arr[i++]);
}
else
{
while(i<=MAX-1)
printf(“%d\n”, cqueue_arr[i++]);
i=0;
while(i<=rear)
printf(“%d\n”, cqueue_arr[i++]);
}
}
Priority Queue