Write a program to implement insertion and deletion operations in a queue using linear array.

#include<iostream>
using namespace std;
class test
{
int queue[10]={27,56,43,87,64};
int rear=4,front=0;
public:
void insert(){
if(rear==9){
cout<<"\nQueue is full";
exit(1);
}
else{
rear=rear+1;
cout<<"\nEnter the Element:";
cin>>queue[rear];
}
}
int del(){
int item;
if(rear==-1){
cout<<"\nQueue is empty";
exit(1);
}
else{
item=queue[front];
for(int i=front;i<rear;i++)
queue[i]=queue[i+1];
rear=rear-1;
return item;
}
}
void display(){
if(rear==-1){
cout<<"\nQueue is empty";
exit(1);
}
else{
for(int i=0;i<=rear;i++)
cout<<queue[i]<<" ";
}
cout<<endl;
}
};
int main(){
test t;
int a=1,o;
cout<<"\nThe Queue is:";
t.display();
while(a!=4){
cout<<"\n1. for INSERT\n2. for DELETE\n3. for DISPLAY\n4. for EXIT\nEnter your choice:";
cin>>a;
switch(a){
case 1:t.insert();
  cout<<"Queue after operation is:";
  t.display();
  break;
case 2:cout<<"\nElement DELETED is:"<<t.del();
  cout<<"\nQueue after operation is:";
  t.display();
  break;
case 3:t.display();
    break;
case 4:cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/";
exit(1);    
}
}
}

Output:

Output:




Comments

Popular posts from this blog

Program to illustrate the concept of templates.

To create an html file to implement the concept of margin, padding using cascading style sheets.

To configure the IP address for a computer connected to LAN