September 22, 2012 / IST / Language, Software Development.

A linear queue models the FIFO(first in first out) data structure, much like a line in real life. The first person in line will be the first person served, in queues the first element to be added is the first that can be removed. The only adding point is to the end of the list and the only removal point is the beginning of the list.

/*Q-11b-> WAP in C++ to Implement Linear Queue Using Linkedlist*/

#include<iostream.h>
#include<conio.h>
#include<process.h>
class queue
{                                  //Queue Class
   private:
	  struct Node
	  { int info;                   //Declaration of Node Structure
	    Node *next;
	  }*ptr,*first,*last,*temp;
   public:
	  queue()                        //Constructor
	  {ptr=first=last=temp=NULL;}
	  void insert(int);
	  void delitem();                 //Member functions
	  void dispque();
};
void queue::insert(int n)
{  ptr=new Node;
   ptr->info=n;
   ptr->next=NULL;
    if(ptr==NULL)
      cout<<"\nCannot Create Node!!!";

   if(first==NULL)
      first=last=ptr;
   else                            //Function to insert an item
     {
       last->next=ptr;
       last=ptr;
     }
}
void queue::delitem()
{ if(first==NULL)
     cout<<"\nQueue is Empty!!!";
  else
     { ptr=first;
       first=first->next;             //Fn to delete an item
       delete ptr;
     }
}
void queue::dispque()
{ temp=first;
  if(first==NULL)
    cout<<"\nQueue is Empty";
  while(temp!=NULL)               //Fn to Display the Queue
   { cout<<temp->info<<"  ";
     temp=temp->next;
   }
}
void main()                    //Main Function
{
  queue ob;
  char choice;
  int ch,data;
  do
  {clrscr();
   cout<<"\n\n\t\t\tQ U E U E    O P E R A T I O N S";
   cout<<"\n\t\t\t--------------------------------";
   cout<<"\n\n1.INSERT AN ITEM";
   cout<<"\n2.DELETE AN ITEM";
   cout<<"\n3.DISPLAY THE QUEUE";             //Menu
   cout<<"\n4.EXIT";
   cout<<"\n\nWhich Operation Do You Want To Perform(1-4):";
   cin>>ch;
	  switch(ch)
		   {
		     case 1:  cout<<"\nEnter the Data To Be Inserted:";
			      cin>>data;
			      ob.insert(data);
			      break;
		     case 2:  ob.delitem();
			      break;             //Calling of the Functions
		     case 3:  ob.dispque();
			      break;
		     case 4:  exit(0);
		     default: cout<<"\nPlease Enter a Valid Choice(1-4)!!!";
		   }

   cout<<"\nDo You Want To Continue(Y/N):";
   cin>>choice;
  }while(choice=='y' || choice=='Y');
}                                            //End of Program