0

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();
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Ancient Dragon
0

>>#include<iostream.h>
>>void main()

Get another teacher because that one doesn't know what he/she's doing. Better yet, go to a different school because that one is probably trying to teach you how to program in the 21st century with a compiler that was written in the 2nd century (e.g. Turbo C++)

But besides that, did you bother to compile and run that program yourself so that you can see what it does? If not then you should do so.

Edited by Ancient Dragon: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.