this is a program that my teacher gave me in the class. but i didn't understand it....please explain it to me step by step...
#include<iostream.h>
class queue
{
int element;
queue* next;
public:
queue* enqueue(queue*,int);
queue* dequeue(queue*);
void queue_display(queue*);
}*head,*tail,object;
queue* queue::enqueue(queue* head,int key)
{
queue* temp;
temp=new queue;
temp->element=key;
temp->next=NULL;
if(head==NULL)
head=temp;
else
tail->next=temp;
tail=temp;
return head;
}
queue* queue::dequeue(queue* head)
{
queue* temp;
if(head==NULL)
{
cout<<"\nit is impossible to dequeue an element as ";
return NULL;
}
else if(head->next==NULL)
{
cout<<"\nthe element dequeued from the queue is: "<<head->element<<endl;
return NULL;
}
else
{
cout<<"\nthe element dequeued from the queue is "<<head->element<<endl;
temp=head->next;
head=temp;
cout<<"\nthe elements of queue after dequeueing are \n";
return head;
}
}
void queue::queue_display(queue* head)
{
if(head!=NULL)
{
while(head->next!=NULL)
{
cout<<head->element<<"->";
head=head->next;
}
cout<<head->element;
cout<<endl;
}
else
cout<<"the queue is empty\n";
}
void choice()
{
int key,ch;
head=tail=NULL;
cout<<"\nchoose the operation\n";
cout<<"\n1.enqueue\t2.dequeue\t3.exit\n\n";
cin>>ch;
while(ch!=3)
{
switch(ch)
{
case 1:
cout<<"\nenter the key to be inserted\n";
cin>>key;
head=object.enqueue(head,key);
cout<<"\nthe elements of queue after inserting "<<key<<" are\n";
object.queue_display(head);
break;
case 2:
head=object.dequeue(head);
object.queue_display(head);
break;
case 3:
break;
default:
cout<<"\nenter correct choice\n";
break;
}
cout<<"\n——————————————————————————\n";
cout<<"\nchoose the operation\n";
cout<<"\n1.enqueue\t2.dequeue\t3.exit\n\n";
cin>>ch;
cout<<"\n——————————————————————————\n";
}
}
void main()
{
choice();
}