Tuesday, 17 September 2013

Queue

HTML Online Editor Sample

 

#include<iostream>

using namespace std;
class queue
{private: struct node
{int element;
node*next;
}*head;
public:
queue();
void inqueue(int element);
int  dqueue();
void inject(int element);
int  eject();
void diepaly();
bool isempty();
void display();
~queue();};
queue::queue()
{
  head=NULL;}
bool queue::isempty()
{if(head==NULL)
return true;
else
return false;}
void queue::inqueue(int element)
{if(isempty())
{
head=new node;
head->element=element;
head->next=NULL;
return;}
 
node*curr=head;
while(curr->next!=NULL)
curr=curr->next;
node* t=new node;
t->element=element;
t->next=NULL;
curr->next=t;
cout<<"ram";
}
int queue::dqueue()
{
if(isempty())
{cout<<"\nqueue is empty";
return -10000;}
node*t=head;
head=head->next;
int data=t->element;
delete(t);
return data;
}
void queue::inject(int element)
{if(isempty())
{
head=new node;
head->element=element;
head->next=NULL;
return;}
node*t=new node;
t->element=element;
t->next=head;
head=t;
}
int queue::eject()
{if(isempty())
{cout<<"\n queue is empty";
return -10000;}
node*curr;
curr=head;
if(head->next==NULL){
int data=head->element;
head=NULL;
}
while(curr->next->next!=NULL)
curr=curr->next;
node*t=curr->next;
int data=t->element;
curr->next=NULL;
delete(t);
return data;
 
}
void  queue::display()
{if(isempty()){
cout<<"\n queue is empty";
return;
}
 
node*curr=head;
cout<<endl;
while(curr!=NULL){
cout<<curr->element<<"->->";
curr=curr->next;}}
queue::~queue(){
while(head!=NULL){
node*t=head;
head=head->next;
delete(t);}}
int main()
{ int a;
char c;
queue q;
cout<<"\t\t********WELCOME TO QUEUE OPERATION************\n";
cout<<"\t----------------------------------------------------------------------\n";
do
{cout<<"\n\t\t\tpress 1 for  inqueue\n\t\t\tpress 2 for dqueue\n\t\t\tpress 3 for enject in queue\n\t\t\tpress 4 for  eject from queue\n\t\t\t 5 to display queue element\n";
cout<<"\n enter your choice\t";
cin>>a;
switch(a)
{case 1:cout<<"\n enter the element you want to inqueue\t";
int b;
cin>>b;
q.inqueue(b);
break;
case 2:int d;
d=q.dqueue();
if(d!=-10000)
cout<<"the element dqueued is\t"<<d;
break;
case 3:
cout<<"\n enter the element you want to injuct in queue\t";
int e;
cin>>e;
q.inject(e);
break;
case 4:
int f;
f=q.eject();
if(f!=-10000)
cout<<"\n the element ejected is\t"<<f;
break;
case 5:
cout<<"\n-------------------------------------------------------------------------------";
q.display();
cout<<"\n---------------------------------------------------------------------------";
break;
defoult:
cout<<"\n sorry you entered wrong option";}
cout<<"\n do yow you want to continue  if yes press y";
cin>>c;}while(c=='y');
 
 
}

No comments:

Post a Comment