Here is a C++ program for implementing the queue .it performs basic operations such as insertion, deletion of the elements.
C++ program to implement queue using arrays
#include<iostream>
using namespace std;
class impqueue
{
int a[10],front,rear;
public:
impqueue()
{
front=0,rear=-1;
}
void insert()
{
if(rear<10)
{
rear++;
cout<<"Enter the element to be inserted ";
cin>>a[rear];
}
else
cout<<"Overflow";
}
void delet()
{
if(front>rear)
cout<<"Empty";
else
{
cout<<"Deleted element is "<<a[front]<<endl;
front++;
}
}
void display()
{
for(int i=front;i<=rear;i++)
cout<<a[i]<<" ";
}
};
int main()
{
int ch,flag=1;
impqueue queue;
while(flag)
{
cout<<"Enter your choice\n1.)Insert\n2.)Delete\n3.)Display\n4.)Exit\nEnter your choice ";
cin>>ch;
switch(ch)
{
case 1:
queue.insert();
break;
case 2:
queue.delet();
break;
case 3:
queue.display();
break;
case 4:
flag=0;
break;
default:
cout<<"Wrong choice";
}
}
}
using namespace std;
class impqueue
{
int a[10],front,rear;
public:
impqueue()
{
front=0,rear=-1;
}
void insert()
{
if(rear<10)
{
rear++;
cout<<"Enter the element to be inserted ";
cin>>a[rear];
}
else
cout<<"Overflow";
}
void delet()
{
if(front>rear)
cout<<"Empty";
else
{
cout<<"Deleted element is "<<a[front]<<endl;
front++;
}
}
void display()
{
for(int i=front;i<=rear;i++)
cout<<a[i]<<" ";
}
};
int main()
{
int ch,flag=1;
impqueue queue;
while(flag)
{
cout<<"Enter your choice\n1.)Insert\n2.)Delete\n3.)Display\n4.)Exit\nEnter your choice ";
cin>>ch;
switch(ch)
{
case 1:
queue.insert();
break;
case 2:
queue.delet();
break;
case 3:
queue.display();
break;
case 4:
flag=0;
break;
default:
cout<<"Wrong choice";
}
}
}
C++ program to implement queue using linked list
#include<iostream>
using namespace std;
struct links
{
int data;
links* next;
};
class impqueue
{
links *front,*rear;
public:
impqueue()
{
front=NULL,rear=NULL;
}
void insert()
{
links *ne;
ne=new links;
if(ne!=NULL)
{
if(front==NULL)
front=ne;
else
rear->next=ne;
rear=ne;
rear->next=NULL;
cout<<"Enter the element to be inserted ";
cin>>rear->data;
}
else
cout<<"Overflow";
}
void delet()
{
links *temp;
if(front==NULL)
cout<<"Empty"<<endl;
else
{
cout<<"Deleted element is "<<front->data<<endl;
temp=front;
front=front->next;
delete temp;
}
}
void display()
{
links *temp=front;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<"NULL"<<endl;
}
};
int main()
{
int ch,flag=1;
impqueue queue;
while(flag)
{
cout<<"Enter your choice\n1.)Insert\n2.)Delete\n3.)Display\n4.)Exit\nEnter your choice ";
cin>>ch;
switch(ch)
{
case 1:
queue.insert();
break;
case 2:
queue.delet();
break;
case 3:
queue.display();
break;
case 4:
flag=0;
break;
default:
cout<<"Wrong choice";
}
}
}
using namespace std;
struct links
{
int data;
links* next;
};
class impqueue
{
links *front,*rear;
public:
impqueue()
{
front=NULL,rear=NULL;
}
void insert()
{
links *ne;
ne=new links;
if(ne!=NULL)
{
if(front==NULL)
front=ne;
else
rear->next=ne;
rear=ne;
rear->next=NULL;
cout<<"Enter the element to be inserted ";
cin>>rear->data;
}
else
cout<<"Overflow";
}
void delet()
{
links *temp;
if(front==NULL)
cout<<"Empty"<<endl;
else
{
cout<<"Deleted element is "<<front->data<<endl;
temp=front;
front=front->next;
delete temp;
}
}
void display()
{
links *temp=front;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<"NULL"<<endl;
}
};
int main()
{
int ch,flag=1;
impqueue queue;
while(flag)
{
cout<<"Enter your choice\n1.)Insert\n2.)Delete\n3.)Display\n4.)Exit\nEnter your choice ";
cin>>ch;
switch(ch)
{
case 1:
queue.insert();
break;
case 2:
queue.delet();
break;
case 3:
queue.display();
break;
case 4:
flag=0;
break;
default:
cout<<"Wrong choice";
}
}
}
No comments:
Post a Comment