C++ Program to implement a QUEUE using linked list

C++ Program to implement a QUEUE using linked list.

/**************************************************************	
	Author: Arun Vishnu M V
	Web: www.arunmvishnu.com
	Description: C++ Program to implement a QUEUE using linked list.
***************************************************************/
#include<conio.h>   	
#include<iostream.h> 
#include<process.h>  
#include<malloc.h>  
 
//   Creating a NODE Structure
struct node
{
   int data;
   struct node *next;
};
 
// Creating a class QUEUE
class queue
{
   struct node *frnt,*rear;
   public:
      queue() // constructure
      {
	 frnt=rear=NULL;
      }
      void insert(); // to insert an element
      void del();  // to delete an element
      void show(); // to show the stack
};
// Insertion
void queue::insert()
{
   int value;
   struct node *ptr;
   cout<<"\nInsertion\n";
   cout<<"Enter a number to insert: ";
   cin>>value;
   ptr=new node;
   ptr->data=value;
   ptr->next=NULL;
   if(frnt==NULL)
      frnt=ptr;
   else
      rear->next=ptr;
   rear=ptr;
   cout<<"\nNew item is inserted to the Queue!!!";
   getch();
}
 
// Deletion
void queue::del()
{
   if(frnt==NULL)
   {
      cout<<"\nQueue is empty!!";
      getch();
      return;
   }
   struct node *temp;
   temp=frnt;
   frnt=frnt->next;
   cout<<"\nDeletion Operation........\nDeleted value is "<<temp->data;
   delete temp;
   getch();
}
 
// Show Queue
void queue::show()
{
   struct node *ptr1=frnt;
   if(frnt==NULL)
   {
      cout<<"The Queue is empty!!";
      getch();
      return;
   }
   cout<<"\nThe Queue is\n";
   while(ptr1!=NULL)
   {
      cout<<ptr1->data<<" ->";
      ptr1=ptr1->next;
   }
   cout<<"END";
   getch();
}
 
// Main function
int main()
{
   clrscr();
   queue q;
   int choice;
   while(1)
   {
      cout<<"\n-----------------------------------------------------------";
      cout<<"\n\t\tQUEUE USING LINKED LIST\n\n";
      cout<<"1:INSERTION\n2:DELETION\n3:DISPLAY QUEUE\n4:EXIT";
      cout<<"\nEnter your choice(1-4): ";
      cin>>choice;
      switch(choice)
      {
       case 1:
	  q.insert();
	  break;
       case 2:
	  q.del();
	  break;
       case 3:
	  q.show();
	  break;
       case 4:
	  exit(0);
	  break;
       default:
	  cout<<"Please enter correct choice(1-4)!!";
	  getch();
	  break;
       }
   }
   return 0;
}
 
//---------------------- END--------------------

You may also like...

19 Responses

  1. Anoop says:

    The best code.
    It compiled without any bug

  2. Matty says:

    Been searching for one of these for a while and this IS the best one.

  3. THARUN says:

    HELLO SIR.THIS IS THARUN.
    SIR I WANT TO KNOW MORE PROGRAMMES ON C .CAN U PLEASE SEND ME UR PERSONNAL WEBSITE ADDRESS PLEASE.

  4. a class 12 programmer says:

    thank a loooooooooooooooooooooot. my practs start form 1st and it was helpful to find an error free program…

  5. Denver says:

    Once u make a sample program can u please make it in a simple and short.
    Thank’s..

  6. Chandan says:

    thank u so much for this program. i am really impressed for this program..

  7. sadaf says:

    well, its gud code

  8. prashant verma says:

    literally a life saver, keep posting such a nice literatures. Thanks 🙂

  9. Owais says:

    Thanks.. It has help me… Thank you very much..

  10. Anjali says:

    seriously the best code for linked list …..really helped me

  11. Mithun says:

    awsome work dude,really helped me a lot.

  12. TiliusGrandiflora says:

    I have a number of PDF files e.g. ~100 x PDF’ that are named 1) Part1_A5Flyer.pdf, 2) Part2_A5Flyer.pdf… #) Part10_A5Flyer.pdf, Part11_A5Flyer.pdf,..Part100_A5Flyer.pdf.

    I would like to place the small PDF files in an ordered queue based on their file-name i.e. doc1.pdf thru doc100.pdf checking to ensure the 100 or so PDFs files are in name order,. I would like the choice of writing the ordered queue direct to print processing or to a folder which can be be later accessed for print production. Also I would like to set a delay of around 2 to 5 seconds between writing the successive top of queue items (PDF duplex page pairs) such that our print processing ,that is very fast has a chance to deal with the next top of queue PDF in name order. I can send say a handful of PDF files if this is helpful. , Not having written C for a long time what compiler IDE do you recommended

    Thanks for your help!
    End.

    I jsb i would like both to be ble to write the ordered data than comes out of the queses either to a folder or direct to the PDF print-n

  13. Ronnel says:

    pls help me

  14. waf says:

    increadible….

  15. Uma N T says:

    Thanks a lot.will u please give code for linked representation of binary tree?

  16. Erin says:

    It started with i – Phone and it has now moved to i – Pad 2; the reason- deficiency of availability.

    s no secret that illegal file sharing is still rampant in several countries, such as US, however,
    may possibly not be government interference that eventually leads for the end with the age of illegal downloads.
    Trading invites a number of sites is very frowned upon inside the exclusive Bit – Torrent community mainly because it allows anti-piracy groups to infiltrate private
    trackers with less effort.

  17. priya says:

    your code is very helpful.i will prepare in control structures of c and c++.what will work in
    control structures. send on the website. Easy to understand of methods. i will developing a
    programming concepts

  18. priya says:

    please reply me

  19. Rohit Sharma says:

    Woo !!! man .
    the best code .
    finally i got it .
    yes.
    keep it up Dude.

Leave a Reply

Your email address will not be published. Required fields are marked *