QUEUE USING ARRAY ....
#include<iostream>
#include<conio.h>
#define MAX 10
using namespace std;
void insert();
void delet();
void display();
int queue[MAX], rear=-1, front=-1, item;
int main()
{
int ch;
do {
cout<<"Press\n1. Insert\n2. Delete\n3. Display\n4. Exit\nAns.\t";
cin>>ch;
switch(ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
return 0;
default:
cout<<"\n\nInvalid entry. Please try again...\n";
}
}
while(1);
getch();
}
void insert()
{
if(rear == MAX-1)
cout<<"\n\nQueue is full.\n\n";
else
{
cout<<"\nEnter ITEM: ";
cin>>item;
if(rear == -1 && front == -1)
{
rear = 0;
front = 0;
}
else
rear++;
queue[rear] = item;
cout<<"\nItem inserted: "<< item<<endl;
}
}
void delet()
{
if(front == -1)
cout<<"\n\n\t\tQueue is empty.\n\n";
else
{
item = queue[front];
if (front == rear)
{
front = -1;
rear = -1;
}
else
front++;
cout<<item;
}
}
void display()
{
int i;
if(front == -1)
cout<<"\n\t\tQueue is empty.\n\n";
else
{
cout<<"\n";
for(i=front; i<=rear; i++)
cout<<queue[i]<<"\t";
}
cout<<"\n\n";
}
#include<conio.h>
#define MAX 10
using namespace std;
void insert();
void delet();
void display();
int queue[MAX], rear=-1, front=-1, item;
int main()
{
int ch;
do {
cout<<"Press\n1. Insert\n2. Delete\n3. Display\n4. Exit\nAns.\t";
cin>>ch;
switch(ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
return 0;
default:
cout<<"\n\nInvalid entry. Please try again...\n";
}
}
while(1);
getch();
}
void insert()
{
if(rear == MAX-1)
cout<<"\n\nQueue is full.\n\n";
else
{
cout<<"\nEnter ITEM: ";
cin>>item;
if(rear == -1 && front == -1)
{
rear = 0;
front = 0;
}
else
rear++;
queue[rear] = item;
cout<<"\nItem inserted: "<< item<<endl;
}
}
void delet()
{
if(front == -1)
cout<<"\n\n\t\tQueue is empty.\n\n";
else
{
item = queue[front];
if (front == rear)
{
front = -1;
rear = -1;
}
else
front++;
cout<<item;
}
}
void display()
{
int i;
if(front == -1)
cout<<"\n\t\tQueue is empty.\n\n";
else
{
cout<<"\n";
for(i=front; i<=rear; i++)
cout<<queue[i]<<"\t";
}
cout<<"\n\n";
}