DATA STRUCTURES

  • Home
  • DATA STRUCTURES USING C++


Admission Enquiry Form

Queue Data Structure

It is a linear data structure in which elements are arranged in First In First Out (FIFO) order. A queue enables insert operations to be performed at one end called REAR and delete operations to be performed at another end called FRONT.

Operations performed in Queue.

insertion()/enqueue()

deletion()/dequeue()

 




Queue data structure

queue-data-structure-by-compuhelp

Insertion/Enqueue operation in Queue

Enqueue operation means to insert an element in the queue


Algorithm of Enqueue operation in Stack

Enqueue Algorithm
Step 1: START
Step 2: REAR : = 0, FRONT : =1[initialize REAR to 0 and FRONT to 1]
Step 3: If Rear=MAXSIZE-1
Write “Overflow: Queue is full” and Exit
[End of If]
Step 4: REAR :=REAR+1
Step 5: Queue [REAR] :=ITEM
Step 6: End




Deletion/Dequeue operation in Queue

Dequeue operation means to delete an element from the queue



Algorithm of Dequeue operation in Stack

Step 1: START
Step 2: If REAR: = 0
Write “Underflow: Queue is empty” and Exit
[End of if]
Step 3: ITEM:=Queue [FRONT]  [ITEM contains the deleted element]
Step 4: If FRONT=REAR
Set REAR: =0 and FRONT : = 1
Else
FRONT=FRONT+1
[End of If Else]
Step 5: Return ITEM
Step 6: End




Program of Static Queue Using Array Operation

//static queue using array
#include<iostream>
using namespace std;
int queue[10];
int front=0;
int rear=-1;
void enqueue(int value)
{
if(rear<9)
{
rear++;
queue[rear]=value;
cout<<endl<<"Item added to Queue...";
}
else{
cout<<endl<<"Queue is full...";
}
}//end of function
void display()
{
int i;
for(i=front;i<=rear;i++)
{
cout<<queue[i]<<" ";
}
}//end of function
void dequeue()
{
if(front<=rear)
{
front++;
cout<<endl<<"Item deleted from Queue...";
}
else
{
cout<<"Queue is Empty...";
}
}//end of function

int main()
{
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
dequeue();
display();
dequeue();
display();
dequeue();
display();
enqueue(90);
display();
}//end of main




Program of Menu Driven Queue Using Array

#include<iostream>
using namespace std;

int queue[100],size=100,front=0,rear=-1;
void Insert(int val)
{
if(rear==size-1)
cout<<"Overflow: Queue is full \n";
else
{
rear++;
queue[rear]=val;
}

}

void Delete()
{
if(rear==-1 || front>rear)
{
cout<<"Queue Underflow \n";
}
else
{
cout<<"The deleted element = "<<queue[front];
front++;
}
}

void Show()
{
if(front==-1)
cout<<"Queue is empty\n";
else
{
cout<<"Queue Element are";
for(int i=front;i<=rear;i++)
cout<<queue[i]<<" ";
}
}
int main()
{
int choice,val;

do
{

cout<<"\nMenu";
cout<<"\n1. Insert Element to Queue";
cout<<"\n2. Delete Element from Queue ";
cout<<"\n3. SHOW Queue Element";
cout<<"\n4.Exit";
cout<<"\nEnter your choice 1 to 4 = ";
cin>>choice;
switch (choice)
{
case 1:
cout<<"Enter value to be inserted= ";
cin>>val;
Insert(val);
break;
case 2:
Delete();
break;

case 3:
Show();
break;
case 4:
exit(0);
break;

default:
cout<<"Invalid choice !";
break;
}

}while (choice<=3);
return 0;
}//end of main