/*
In queue there are following operations are performed such as
1 . Insertion from 'rear' side.
2 . Deletion from 'front' side.
*/
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
node *next;
};
class Queue
{
public :
node *rear,*front,*temp;
int val;
Queue()
{
front=NULL;
rear=NULL;
}
void Qinsert()
{
cout<<"\nEnter element to insert:";
cin>>val;
temp=new node[1];
temp->data=val;
temp->next=NULL;
if(rear==NULL && front==NULL)
{
rear=temp;
front=temp;
}
else
{
rear->next=temp;
rear=temp;
cout<<"\nInserted Successfully\n";
}
}
void Qdelete()
{
if(front==NULL)
{
cout<<"Queue is empty";
}
else
{
temp=front;
front=front->next;
delete(temp);
cout<<"\nDeleted successfully\n";
}
}
void Qdisplay()
{
if(front==NULL)
cout<<"\nQueue is empty\n";
else
{
for(temp=front;temp!=NULL;temp=temp->next)
{
cout<<temp->data<<",";
}
}
}
};
void main()
{
Queue q;
char ch;
int n;
do{
cout<<"\n1.Qinsert\t2.Qdelete\t3.Qdisplay\t4.Exit\nEnter your choice :";
n=getch();
switch(n)
{
case '1':q.Qinsert();
break;
case '2':q.Qdelete();
break;
case '3':q.Qdisplay();
break;
case '4':exit(0);
}
cout<<"\nDo u want to continue (y/n):";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}
Output
In queue there are following operations are performed such as
1 . Insertion from 'rear' side.
2 . Deletion from 'front' side.
*/
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
node *next;
};
class Queue
{
public :
node *rear,*front,*temp;
int val;
Queue()
{
front=NULL;
rear=NULL;
}
void Qinsert()
{
cout<<"\nEnter element to insert:";
cin>>val;
temp=new node[1];
temp->data=val;
temp->next=NULL;
if(rear==NULL && front==NULL)
{
rear=temp;
front=temp;
}
else
{
rear->next=temp;
rear=temp;
cout<<"\nInserted Successfully\n";
}
}
void Qdelete()
{
if(front==NULL)
{
cout<<"Queue is empty";
}
else
{
temp=front;
front=front->next;
delete(temp);
cout<<"\nDeleted successfully\n";
}
}
void Qdisplay()
{
if(front==NULL)
cout<<"\nQueue is empty\n";
else
{
for(temp=front;temp!=NULL;temp=temp->next)
{
cout<<temp->data<<",";
}
}
}
};
void main()
{
Queue q;
char ch;
int n;
do{
cout<<"\n1.Qinsert\t2.Qdelete\t3.Qdisplay\t4.Exit\nEnter your choice :";
n=getch();
switch(n)
{
case '1':q.Qinsert();
break;
case '2':q.Qdelete();
break;
case '3':q.Qdisplay();
break;
case '4':exit(0);
}
cout<<"\nDo u want to continue (y/n):";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}
Output
No comments:
Post a Comment