Hi, guys Queue using array in c++ code given below
#include<iostream>
using namespace std;
int queue [5];
int front=-1;
int rear=-1;
void enqueue(int x) {
if(rear==5-1) {
cout<<"overflow condition";
}
else if(rear==-1 and front==-1)
{
front=rear=0;
queue[front]=x;
}
else {
rear++;
queue[rear]=x;
}
}
void dequeue() {
if(front==-1 and rear==-1)
{
cout<<"underflow cond";
}
else if(rear==front)
{
front=rear=-1;
}
else {
cout<<"dequeued element is"<<queue[front]<<endl;
front ++;
}
}
void display () {
if(front==-1 && rear==-1) {
cout<<"list is empty";
}
else
{
cout<<"display"<<endl;
}
while(front!=rear+1) {
cout<<queue[front];
front++;
}
}
main() {
enqueue(1) ;
enqueue(2) ;
enqueue(3) ;
enqueue(4) ;
enqueue(5) ;
dequeue() ;
dequeue() ;
display () ;
}
Comments
Post a Comment