954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help me with linked ques

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();
}
jasleen12345
Newbie Poster
18 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

>>#include
>>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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: