Tuesday, 12 September 2017

Implementation Queue Data Structure using array with all possible operational cases

Queue can be easily implemented using Linear Array.In Queue, element is inserted from FRONT and deleted from REAR variables and vice-versa. It is a FIRST IN FIRST OUT (FIFO) data Structure which means that the element which inserted first is the first one to be taken out. It can also be 
implemented with the help of Linked List.

Implementation Queue Data Structure using array 

#include <iostream>
using namespace std;
#define size 3
class queue
{
int a[size];
int rear,front;
public:
queue()
{
front=-1;
rear=-1;
}
void enqueue(int value)
{
if( front=rear == -1)
{
front++;
rear++;
a[rear]=value;
}
else if(rear==size)
{
cout<<"queue is full";
}
else
{
rear++;
a[rear]=value;
}
}    
void dequeue()

{      


if(front ==-1 && rear==-1)
{
cout<<"queue empty";
}
else if(front == size && rear== size)
{
front=-1;
rear=-1;
}
else if(front == rear)
{
front=-1;
rear=-1;
}
else
{
 front=front+1;;
}
}
void display()
{
for(int i=front;i<=rear;i++)
cout<<a[i]<<" ";  
}
};
int main() 


{
queue q;
for(int i=0;i<=size;i++)
{
cin>>i;
q.enqueue(i);
}
q.display();
q.dequeue();
q.dequeue();
q.display();
}



Thanks
Mukesh Rajput

No comments:

Post a Comment

Thanks
Mukesh Rajput