C++ program to implement the Queue ADT using a single linked list

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

/* Write C++ programs to implement the Queue ADT using a singly linked list */

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node
{
      public:
             class node *next;
             int data;
};
 
class queue : public node
{
            node *head;
            int front,rare;
	public:
           queue()
           	{
            	front=-1;
             	rare=-1;
             }
           void push(int x)
           	{
            	if (rare < 0 )
             		{
               			head =new node;
                  		head->next=NULL;
                    	head->data=x;
                     	rare ++;
                     }
             else
                    {
                    	node *temp,*temp1;
                     	temp=head;
                      	if(rare >= 4)
                          {
                          	cout <<"queue over flow";
                           	return;
                           }
                        rare++;
                        while(temp->next != NULL)
                        	temp=temp->next;
                        temp1=new node;
                        temp->next=temp1;
                        temp1->next=NULL;
                        temp1->data=x;
                    }  }
 
           void display()
           	{
              node *temp;
              temp=head;
              if (rare < 0)
                {
                    cout <<" queue under flow";
                    return;
                 }
              while(temp != NULL)
               {
               	   cout <<temp->data<< " ";
                   temp=temp->next;
                }
             }
             void pop()
              {
              	node *temp;
               	temp=head;
                if( rare < 0)
                  {
                  	cout <<"queue under flow";
                   	return;
                   }
                if(front == rare)
                  {
                  	front = rare =-1;
                   	head=NULL;
                    return;
                   }
                front++;
                head=head->next;
                }
};
main()
{
	queue s1;
	int ch;
	while(1)
      {
		cout <<"\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n enter ru choice:";
		cin >> ch;
		switch(ch)
		{
            case 1:
                 	cout <<"\n enter a element";
                  	cin >> ch;
                   	s1.push(ch); break;
 
            case 2: s1.pop();break;
            case 3: s1.display();break;
        	case 4: exit(0);
		  }
       }
return (0);
}

OUTPUT

1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:1
enter a element23

1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:1
enter a element54

1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:3
23 54
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2

1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2

1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2
queue under flow
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:4

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

9 Responses to “C++ program to implement the Queue ADT using a single linked list”

  1. Chidambaram

    There is a errro. Push n element fist. Pop it. Display it. No result comes. Pop now. No result comes

    Reply
  2. Aamir Shahzad

    Great sir………….. Thanks a lot of your efforts………………

    Reply
  3. sravani sravz

    dis is an error in in display function plzz check it once sir..
    and too…many error are accuring…sir
    plzz check it once

    Reply
  4. sir my program crashed as i run it… exception unhandled pointer is not accessing memory location

    Reply
  5. Pretty! This was a really wonderful post. Thanks for providing these details.

    Reply
  6. Thanks for your personal marvelous posting! I definitely enjoyed reading it, you could be a great author.

    I will make sure to bookmark your blog and may come back
    very soon. I want to encourage you to continue your great writing,
    have a nice holiday weekend!

    Reply
  7. Sweet blog! I found it while browsing on Yahoo News.
    Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to
    get there! Thanks

    Reply
  8. It’s appropriate time to make a few plans for the future and
    it is time to be happy. I have read this publish and if I may I want to counsel you
    some attention-grabbing issues or suggestions. Perhaps you could write subsequent articles relating to
    this article. I desire to read even more things about it!

    Reply

Leave a Reply