Queue
Rear
end – insert
Front
end – Delete
Queue
is a linear data structure in which one end called rear is used to insert data
element and another end called front is used to delete the data elements. In
queue the data element which is inserted at first is the item to be deleted first,
so we can say that queue as FIFO data structure.
Example of queue in real life are people waiting for a service in a bank, in a library, airport etc. in
computer multiple processes wait in a queue for getting services of CPU, printer or any other devices
Queue operations
1. Create empty queue
2. Check whether a queue is empty or not
3. Check whether a queue is full or not.
4. Insert one element at the rear of the queue if the queue is not full in case of implementating queue using array
5. Delete an element from the front of the queue is not empty.
6 .Print all queue elements
Implementation of Queue
#include<stdio.h>
#include<conio.h>
#define size 5
void main()
{
int queue[size];
int data i,choice;
int front = 0;
int rear = 0;
while(1)
{
printf("\n1. Insert data");
printf("\n2. Delete data");
printf("\n3. Display data");
printf("\n4. Exit");
printf("Enter your choice:");
printf("%d", &choice);
switch(choice)
{
case 1:
if(rear<size) // means queue is not full
{
printf("Enter data");
scanf("%d", &data);
queue[rear] = data;
rear++;
}
else
printf("Queue is full");
break;
case 2:
if(front<rear)
{
printf("The deleted data item is %d", queue[front]);
front++;
}
else
printf("Queue is empty");
break;
case 3:
if(front<rear)
{
printf("queue element are");
for(i=front; i<=rear; i++)
{
printf("%d", queue[i]);
}
}
else
printf("Queue is empty");
break;
case 4:
exit(0);
}
}
}
Circular Queue
In a circular queue after reaching to a last element, , the last element of the queue is the first element. So the queue moves in circularly and physically there is no first and last element. Circular queue eliminates the basic drawbacks of linear queue.
Array implementation of circular queue
#include<stdio.h>
#include<conio.h>
#define
size 5
void
main()
{
int cqueue[size];
int data, i, choice, j;
int rear = 0;
int front = 0;
int count = 0;//to count the total number
of elements in a queue
while(1)
{
printf("\n1. Insert
data");
printf("\n2. Delete
data");
printf("\n3.Display
data");
printf("\n4. exit");
printf("Enter your
choice:");
scanf("%d",
&choice);
switch(choice)
{
case 1:
if(count<size)//queue
is not full
{
printf("Enter
data");
scanf("%d",
&data);
queue[rear]
= data;
rear
= (rear+1)%size;
count++;
}
else
printf("Queue
is already full");
break;
case 2:
if(count
> 0)//queue is not empty
{
printf("Deleted
item is %d", cqueue[front]);
front
= (front+1)%size;
count--;
}
else
printf("queue
is empty");
break;
case 3:
i =
front;
j = 0;
while(j
< count)
{
printf("%d",cqueue[i]);
i
= (i+1)%size;
j++;
}
break;
case 4:
exit(0);
}
}
Priority queue
A priority queue is a
data structure in which the ordering of the elements does determine the results
of its basic operations. There are two types of priority queues.
-
Ascending
priority queue
-
Descending
priority queue
An ascending priority
queue is a collection of items into which items can be inserted arbitrarily and
from which only the smallest item can be removed and a descending priority
queue is a queue that allows deletion of only the largest item.
No comments:
Post a Comment