adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C++ to implement queue Using Class.


Program in C++ to implement queue Using Class.

#include<iostream>
#include<conio.h>
 using namespace std;
  class Queue
   {
     private:
       struct Node{
         int data;
         Node *next;
       };

       Node *front,*rear;
       int size;
     public:
        Queue(){
         front=rear=0;
         //front=rear=NULL;
         size=0;
        }

        void insert(int d);
        void del();
        void show();
        int length();
   };
   void Queue::insert(int d)
    {
        Node *newN;
        newN=new Node[sizeof(Node)];
        newN->data=d;
        newN->next=NULL;
        if(front==NULL)
        {
            front=newN;
            rear=newN;
        }
        else
        {
            rear->next=newN;
            rear=newN;
        }
    }
    void Queue::del()
     {
         Node *temp;
         int d;
         if(front==NULL){
            cout<<"Queue is empty!";
         }
         else
         {
            temp=front;
            d=temp->data;
            cout<<"\nDelete data = "<<d;
            front=temp->next;
            delete temp;
         }

     }
    void Queue::show()
     {
         Node *temp;
         temp=front;
         while(temp!=NULL)
         {
             cout<<" "<<temp->data;
             temp=temp->next;
         }
     }
     int Queue::length()
      {
         Node *temp;
         int count=0;
         temp=front;
         while(temp!=NULL)
         {
             count++;
             temp=temp->next;
         }
         return count;
      }
   int main()
    {
     Queue q1;
     q1.insert(23);
     q1.insert(89);
     q1.insert(56);
     q1.insert(23);
     q1.insert(12);
     q1.insert(17);
     q1.insert(67);
     cout<<"Elements of queue ";
     q1.show();
     cout<<endl<<"Size of queue = "<<q1.length();
     q1.del();
    }
Output :-




Related Programs


  1.  Program in C and C++ to implement queue Using Array.
  2.  Program in C and C++ to implement queue Using Array second method.
  3.  Program in C  to implement queue Using Structure.
  4. Program in  C++ to implement queue Using  Class.
  5.  Program in C and C++ to implement queue Using Array and Function.
  6.  Program in C++ to implement queue Using Template feature.

Post a Comment

0 Comments