A program to implement Queues in arrays. The program has four options as given below and it continues till the user wants
a) Insert
b) Delete
c) Display
d) Exit

 

#include<iostream.h>
#include<stdio.h>
#include<conio.h>

int q[10];
int f=-1,r=-1;

void insert()
{
if(r<9)
{
r++;
cout<<"\nEnter the element at "<<r<<" location: ";
cin>>q[r];
}
else
cout<<"\nQueue Overflow.\n";
}

void del()
{
if(f<r)
{
f++;
cout<<"\nDeleted value: "<<q[f]<<endl;
}
else
cout<<"\nQueue Underflow.\n";
}

void display()
{
if(f!=r)
{
cout<<"\nQueue: ";
for(int i=(f+1);i<=r;i++)
{
cout<<q[i]<<" ";
}
cout<<endl;
}
else
cout<<"\nQueue Empty.\n";
}

void main()
{
 clrscr();
 int ch;

 do
  {
   cout<<"1. Insert"<<endl;
   cout<<"2. Delete"<<endl;
   cout<<"3. Display"<<endl;
   cout<<"4. Exit"<<endl;
   cout<<"Enter your choice: ";
   cin>>ch;

   switch(ch)
    {
     case 1: {
                 insert();
                 break;
                }
     case 2: {
                 del();
                 break;
                }
     case 3: {
                 display();
                 break;
                }
     case 4: {
                 break;
                }
     default:{
                 cout<<"\nWrong choice entered.\n";
                }
    }
   cout<<endl;
  }
 while(ch!=4);
}

 

< Back