Site icon T4Tutorials.com

Queue Implementation Using Linked List C++

Queue Implementation Using Linked List C++

Animated picture is here. So need to wait for some seconds to load it completely.

which of the following is used to Implement Queue
#include<iostream>
using namespace std;

struct Node
{
    int data;
    Node *next ;
};
class Queue
{
    Node *front, *rear;

public:
    Queue()
    {
        front = rear = NULL;  

    }
    void Enqueue(int elem)    
	// insertion values from rear side
    {
        Node *newnode;
        newnode = new Node;
        newnode->data = elem;
        newnode->next = NULL;

        if(front == NULL)
            front = rear = newnode;
        else
        {
            rear->next = newnode;
            rear = newnode;
        }
    }
    void Dequeue()            
	// delete values from front side
    {
        Node *temp;
        if(front == NULL)
            cout<<"Queue is Empty";
        else
        {
            temp= front;
            front = front->next;
            delete temp;
        }

    }
    void show()
    {
        Node *temp;
        temp= front;
         while(temp!=NULL) 
        {
            cout<<temp->data<<"\t";
            temp = temp->next;
        }
        cout<<endl;
    }
};
int main()

{
    Queue T4TUTORIALS;
    T4TUTORIALS.Enqueue(10);
    cout<<"Queue after inserting the 1st values is :";
    T4TUTORIALS.show();
    T4TUTORIALS.Enqueue(4);
    cout<<"Queue after inserting the 2nd value is :";
    T4TUTORIALS.show();
    T4TUTORIALS.Dequeue();
    cout<<"Queue after deleting a value from the queue:";
    T4TUTORIALS.show();
}

Download Animated Slides of how to implement queue using link list in C++

Exit mobile version