Tuesday 2 April 2013

Queue using Array in C++


What Is Queue ???

queue is an ordered list in which all insertions take place at one end, the rear, while all deletions take place at the other end, the front. All Queue Operation like insertion , deletion or display as elements can understand by following Program.

Source Code :



#include<conio.h>
#include<iostream.h>
#include<stdio.h>
#define max 6
int q[10],rear=-1,front=-1;
void insertInQ(int data)
{
if(rear==-1&&front==-1)
{
rear=0;
front=0;
q[rear]=data;
return;
 
}
if(front==0&&rear==max-1||front==rear+1)
{
cout<<"overflow";
return;
}
if(rear==max-1)
rear=0;
else
rear=rear+1;
q[rear]=data;
}
 
int deleteFromQ(){
int data;
if(front==-1&&rear==-1)
{
cout<<"underflow";
return 0;
}
 
data=q[front];
q[front]=NULL;
 
if(front==rear)
{
front=rear=-1;
}
else
{
if(front==max-1)
front=0;
else
front=front+1;
}
return data;
 
}
void printQ()
{
int i;
for(i=0;i<max;i++)
{
if(q[i]==NULL)
cout<<"t";
else
cout<<"t"<<q[i];
}
}
int main()
{
int ch,d;
while(1)
{
cout<<"n=========queue operations===========n 1.insert n 2.delete n 3.show n 4.exit n ========================= n Enter your choice : " ;
cin>>ch;
switch(ch)
{
case 1 :
 
cout<<"n enter data :" ;
cin>>d;
insertInQ(d);
break;
 
case 2:
d=deleteFromQ();
cout<<"n data is :"<<d;
break;
 
case 3:
printQ();
break;
 
case 4:
exit(0);
 
default :
cout<<"n wrong choice------ n";
break;
}
}
getch();
return 0;
}


Output :



1 comment:

  1. 1xbet korean | Legalbet.co.kr
    1xbet korean. You can also 1xbet вход use the option of depositing funds in Betway, the popular casino and poker online games.

    ReplyDelete