Join Regular Classroom : Visit ClassroomTech

Basic Implementation of Dequeue using LinkedList – Codewindow

Hot Topics

Data Structure

Dequeue using LinkedList

Algorithm

  1. Create a node structure with data and next as two data members

  2. Also create two pointers front and rear and set them to NULL

  3. Check if the queue is empty

  4. pushFront() – To push a node at front we have 2 case

  • When the dequeue is empty we create a new node and set front and rear pointers on it.

  • When there is atleast 1 node we create a new node and put it before the existing node and reassign front to the newly created node

  1. pushRear() – To push at the rear we can have 2 cases

  • We are creating a new node in dequeue and setting both front and rear pointer on that node

  • We already have atleast one node in dequeue and we will add other nodes by creating a new node and set rear to the last created node

  1. popFront() – To remove a node from front we have 2 cases

  • When the dequeue is empty we can’t delete any element

  • When there is atleast one node present we remove that node and reassign front to the next node of front

  1. popRear() – To remove a node from rear we have 3 cases

  • When the dequeue is empty we can’t delete anything

  • When there is only one node present we have to remove that node and reassign front and rear to NULL to say that it is empty

  • When more than one node is present we have to traverse the list until we reach the second last node and then remove the last node and reassign rear to the second last node

  1. getFront() – Get the data pointed by the front pointer when the dequeue is not empty

  2. getRear() – Get the data pointed by the rear pointer when dequeue is not empty

  3. display() – To display elements in dequeue attach a temp pointer to the front node and traverse until we reach the last node.

#include 

struct QNode
{
    int data;
    QNode *next;
    QNode(int d)
    {
        data = d;
        next = NULL;
    }
};
struct Queue
{
    QNode *front, *rear;
    Queue() { front = rear = NULL; }

    bool isEmpty()
    {
        if (front == NULL && rear == NULL)
        {
            return true;
        }
        return false;
    }

    void pushFront(int x)
    {
        if (front == NULL)
        {
            QNode *temp = new QNode(x);
            temp->data = x;
            temp->next = NULL;
            front = temp;
            rear = temp;
        }
        else
        {
            QNode *temp = new QNode(x);
            temp->data = x;
            temp->next = front;
            front = temp;
        }
        std::cout << "Element " << x << " is added at front" <data = x;
            temp->next = NULL;
            front = temp;
            rear = temp;
        }
        else
        {
            QNode *temp = new QNode(x);
            temp->data = x;
            rear->next = temp;
            rear = temp;
        }
        std::cout << "Element " << x << " is added at rear" << std::endl;
    }

    void popFront()
    {
        if (front == NULL)
        {
            std::cout << "Queue is empty" <next;
            std::cout <data << " is deleted from front" << std::endl;
            delete temp;
            if (front == NULL)
            {
                rear = NULL;
            }
        }
    }

    void popRear()
    {
        if (front == NULL)
        {
            std::cout << "Queue is empty" <next == NULL)
        {
            std::cout <data <next != rear)
            {
                temp = temp->next;
            }
            std::cout <data << " is deleted from rear" <next = NULL;
        }
    }

    void getFront()
    {
        if (isEmpty())
        {
            std::cout << "Dequeue is empty" << std::endl;
        }
        std::cout << "The data at front node is:" <data << std::endl;
    }

    void getRear()
    {
        if (isEmpty())
        {
            std::cout << "Dequeue is empty" << std::endl;
        }
        std::cout << "The data at rear node is:" <data << std::endl;
    }

    void display()
    {

        if (front != NULL)
        {
            QNode *temp = front;
            while (temp != NULL)
            {
                std::cout <data <next;
            }
            std::cout << std::endl;
        }
    }
};
int main()
{
    Queue q;
    q.pushFront(10);
    q.pushFront(20);
    q.display();
    q.pushRear(30);
    q.pushRear(40);
    q.display();
    q.getFront();
    q.getRear();
    q.popFront();
    q.display();
    q.popRear();
    q.display();
    return 0;
}
Output
Element 10 is added at front
Element 20 is added at front
20 10 
Element 30 is added at rear
Element 40 is added at rear
20 10 30 40 
The data at front node is:20
The data at rear node is:40
20 is deleted from front
10 30 40 
40 is deleted from rear
10 30 

Nagarro Solved

Automata Fixing

      

We Love to Support you

Go through our study material. Your Job is awaiting.

Recent Posts
Categories