queue
queue
Linear Queue
The simple queue is a normal queue where insertion takes place at
the FRONT of the queue and deletion takes place at the END of the
queue.
Circular Queue
In a circular queue, the last node is connected to the first node.
Circular queue is also called as Ring Buffer.
Insertion in a circular queue happens at the FRONT and deletion at
the END of the queue.
Priority Queue
In a priority queue, the nodes will have some predefined priority.
Insertion in a priority queue is performed in the order of arrival of the
nodes.
The node having the least priority will be the first to be removed from
the priority queue.
Dequeue (Doubly Ended Queue)
In a Double Ended Queue, insertion and deletion operations can be done at
both FRONT and END of the queue.
Implementation of Queue
Queue can be implemented in two ways :
1. Static implementation (using arrays)
2. Dynamic implementation (using linked list)
Static implementation (using arrays)
A queue data structure can be implemented using one dimensional
array. The queue implemented using array stores only fixed
number of data values. The implementation of queue data
structure using array is very simple. Just define a one dimensional
array of specific size and insert or delete the values into that array
by using FIFO (First In First Out) principle with the help of
variables 'front' and 'rear'. Initially both 'front' and 'rear' are set
to -1. Whenever, we want to insert a new value into the queue,
increment 'rear' value by one and then insert at that position.
Whenever we want to delete a value from the queue, then delete
the element which is at 'front' position and increment 'front' value
by one.
enQueue(value) - Inserting value into the queue
In a queue data structure, enQueue( ) is a function used to insert a
new element into the queue. In a queue, the new element is always
inserted at rear position. The enQueue() function takes one
integer value as a parameter and inserts that value into the queue.
We can use the following steps to insert an element into the
queue...
Step 1 - Check whether queue is FULL. if(rear == SIZE-1)
Step 2 - If it is FULL, then display "Queue is FULL!!! Insertion is
not possible!!!" and terminate the function.
Step 3 - If it is NOT FULL, then increment rear value by one
(rear++) and set queue[rear] = value.
Algorithm
Step 1: IF REAR = MAX - 1
Write OVERFLOW
Go to step
[END OF IF]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: Set QUEUE[REAR] = NUM
Step 4: EXIT
deQueue() - Deleting a value from the Queue
In a queue data structure, deQueue() is a function used to delete
an element from the queue. In a queue, the element is always
deleted from front position. The deQueue() function does not take
any value as parameter. We can use the following steps to delete
an element from the queue...
Step 1 - Check whether queue is EMPTY. if(front == rear)
Step 2 - If it is EMPTY, then display "Queue is EMPTY!!! Deletion
is not possible!!!" and terminate the function.
Step 3 - If it is NOT EMPTY, then increment the front value by one
(front ++). Then display queue[front] as deleted element. Then
check whether both front and rear are equal (front == rear), if
it TRUE, then set both front and rear to '-1' (front = rear = -1).
Algorithm
Step 1: IF FRONT = -1 or FRONT > REAR
Write UNDERFLOW
ELSE
SET VAL = QUEUE[FRONT]
SET FRONT = FRONT + 1
[END OF IF]
Step 2: EXIT
#include <stdio.h>
#include<conio.h>
#define MAX 10
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
do
{
printf("1.Insert \n");
printf("2.Delete\n");
printf("3.Display \n");
printf("4.Exit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Inavlid choice \n");
} /*End of switch*/
}while(choice!=4); /*End of while*/
getch();
} /*End of main()*/
insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /*End of insert()*/
delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Deleted Element is : %d\n", queue_array[front]);
front = front + 1;
}
} /*End of delete() */
display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
} /*End of display() */