Queues Data Structure in C++

The queue data structure follow the FIFO rule which is the abbreviation of first in first out. The Queue is the example of the linear data structure and it has large usage in computer science , electronic devices and in transport. In this problem we just make a queue and then apply its function on different values.

Source Code

 #include <iostream>
 #include <windows.h>
 using namespace std;
 class linkedlist
 {
  private:
  struct node
         {
          int value;
          node *next;             
         };
          node *front;
          node *rear;
          int item;

 public:
     linkedlist()
     {
          front=NULL;
          rear=NULL;
  }
  void enqueue(int num)
      {
        node *newnode;
        newnode = new node;
        newnode->value= num;
        newnode->next=NULL;
   if(front==NULL && rear == NULL)
           {
           front = newnode;
           rear = newnode;               
           }
   else
           {
            rear->next=newnode;   
            rear= newnode;   
           }
           }
  void deqeue()
      {
       node *newnode;
       newnode= front;
       front=front->next;
       delete newnode;
      }

  void display()
 {
       node *result;
       result = front;
 while(result!=NULL)
{
       cout<<result->value<<endl;
       result= result-> next;           
}   
}
}; // end of class
 int main()
 {
    
     linkedlist l;
     cout<<"Before DeQuoue we have these Numbers \n\n";
     l.enqueue(2);
     l.enqueue(3);
     l.enqueue(4);
     l.enqueue(5);
     l.enqueue(6);
     l.display();
     cout<<"after deque we have the following values"<<endl;
     l.deqeue();
     l.display();
     system("pause"); 
 }

Output of the Program

Queues Data Structure in C++

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment