Saturday, June 19, 2021
Home coding Write a program to implement queue using linkedlist

Write a program to implement queue using linkedlist

Given a linkedlist ,we have to implement queue using linkedlist.

queue has two operation:

1-enqueue operation.(push the element)

2-dequeue operation.(pop the element)

Logic:

STEP 1: implement push operation to.
STEP 2:implement pop operation.

Code

 void enQueue(int data) 
    { 
        Node* temp = new Node(data); 
        if (rear == NULL)
        { 
            front = rear = temp; 
            return; 
        } 
        rear->next = temp; 
        rear = temp; 
    } 
    void deQueue() 
    { 
        if (front == NULL) 
            return; 
 
        Node* temp = front; 
        front = front->next; 
  
        if (front == NULL)
        {
            rear = NULL; 
        }
  
        delete (temp); 
    }

Ouput:

pushed
popped

Time Complexity:O(N)

Space Complexity:O(1)

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments