Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Queue:
A queue is a linear list of elements in which deletion of an element can take place only at
one end called the front and insertion can take place on the other end which is termed as
the rear. The term front and rear are frequently used while describing queues in a linked list.
In the concept of a queue, the first element to be inserted in the queue will be the first
element to be deleted or removed from the list. So Queue is said to follow the FIFO (First In
First Out) structure. A real-life scenario in the form of example for queue will be the queue
of people waiting to accomplish a particular task where the first person in the queue is the
first person to be served first.
Other examples can also be noted within a computer system where the queue of tasks
arranged in the list to perform for the line printer, for accessing the disk storage, or even in
the time-sharing system for the use of CPU. So basically queue is used within a single
program where there are multiple programs kept in the queue or one task may create other
tasks which must have to be executed in turn by keeping them in the queue.
First, there should be a particular way in which components are related to each other
Second, a statement for the operation that can be performed on elements of abstract data
type must have to be specified
Thus for defining a Queue as an abstract data type, these are the following criteria:
Example:
#include <iostream>
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cin>>val;
rear++;
queue[rear] = val;
void Delete() {
return ;
} else {
front++;;
void Display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<queue[i]<<" ";
cout<<endl;
int main() {
int ch;
cout<<"4) Exit"<<endl;
do {
cin<<ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
} while(ch!=4);
return 0;
In the above program, the function Insert() inserts an element into the queue. If the rear
is equal to n-1, then the queue is full and overflow is displayed. If front is -1, it is
incremented by 1. Then rear is incremented by 1 and the element is inserted in index of
rear. This is shown below −
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cin>>val;
rear++;
queue[rear] = val;
In the function Delete(), if there are no elements in the queue then it is underflow
condition. Otherwise the element at front is displayed and front is incremented by one.
This is shown below −
void Delete() {
return ;
else {
front++;;
In the function display(), if front is -1 then queue is empty. Otherwise all the queue
elements are displayed using a for loop. This is shown below −
void Display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<queue[i]<<" ";
cout<<endl;
The function main() provides a choice to the user if they want to insert, delete or display
the queue. According to the user response, the appropriate function is called using
switch. If the user enters an invalid response, then that is printed. The code snippet for
this is given below −
int main() {
int ch;
cout<<"4) Exit"<<endl;
do {
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
} while(ch!=4);
return 0;
Tasks:
1. Write a Program to Implement a Queue using an Array.
2. Write a Program to Implement Queue Data Structure using Linked list .
Task#1
Write a Program to Implement a Queue using an Array.
Program:
#include <stdio.h>
#include <stdlib.h>
int MAX=50;
void insert();
void Delete();
void display();
int queue_array[50];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \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(EXIT_FAILURE);
default:
printf("Wrong choice \n");
}
}
}
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}
void Delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element Deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
} Output:
}
void 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");
}
}
Task#2
Write a Program to Implement Queue Data Structure using Linked list.
Program:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();
int count = 0;
int main()
{
int no, ch, e;
printf("\n 1 - Enque");
printf("\n 2 - Deque");
printf("\n 3 - Front element");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Queue size");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
printf("Front element : %d", e);
else
printf("\n No front element in Queue as queue is empty");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
return 0;
}
void create()
{
front = rear = NULL;
}
void queuesize()
{
printf("\n Queue size : %d", count);
}
if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
}
else
{
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;
rear = temp;
}
count++;
}
void display()
{
front1 = front;
void deq()
{
front1 = front;
if (front1 == NULL)
{
printf("\n Error: Trying to display elements from empty queue");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed value : %d", front->info);
free(front);
front = front1;
}
else
{
printf("\n Dequed value : %d", front->info); Output:
free(front);
front = NULL;
rear = NULL;
}
count--;
}
int frontelement()
{
if ((front != NULL) && (rear != NULL))
return(front->info);
else
return 0;
}
void empty()
{
if ((front == NULL) && (rear == NULL))
printf("\n Queue empty");
else
printf("Queue not empty");
}