Implement Queue using Linked List | Practice | GeeksforGeeks

Implement a Queue using Linked List. 
A Query Q is of 2 Types
(i) 1 x   (a query of this type means  pushing 'x' into the queue)
(ii) 2     (a query of this type means to pop an element from the queue and print the poped element)
Example 1:
Input:
Q = 5
Queries = 1 2 1 3 2 1 4 2
Output: 2 3
Explanation: n the first testcase
1 2 the queue will be {2}
1 3 the queue will be {2 3}
2   poped element will be 2 the
    queue will be {3}
1 4 the queue will be {3 4}
2   poped element will be 3.


Example 2:
Input:
Q = 4
Queries = 1 2 2 2 1 3 
Output: 2 -1
Explanation: In the second testcase 
1 2 the queue will be {2}
2   poped element will be {2} then
    the queue will be empty. 
2   the queue is empty and hence -1
1 3 the queue will be {3}.

Your Task:
Complete the function push() which takes an integer as input parameter and pop() which will remove and return an element(-1 if queue is empty).
Expected Time Complexity: O(1).
Expected Auxiliary Space: O(1).
Constraints:
1 <= Q <= 100
1 <= x <= 100
0
ns2847195 hours ago
void MyQueue:: push(int x){       // Your Code        QueueNode *q = new QueueNode(x);  if(front == NULL)  {  rear = q;  front = rear;  }  else  {      rear->next = q;      rear = q;  }   }
//Function to pop front element from the queue.int MyQueue :: pop(){       // Your Code           if(front == NULL)   return -1;      int ans = front->data;   front = front->next;   return ans;}
 
-1
priyeshchoukse1 week ago
//Java Easy to understand
 
class MyQueue{   QueueNode front, rear;      void push(int a){       QueueNode  q = new QueueNode(a);       if(front==null)       {           rear =q;           front=rear;       }       else       {           rear.next=q;           rear=q;       }   }   int pop(){            if(front==null)       {           return -1;       }              int pops=front.data;       front=front.next;       return pops;       }} 
0
csedeepak
This comment was deleted.
+2
harshscode2 weeks ago
       QueueNode *n=new QueueNode(x);         if(front==NULL)         {             front =rear=n;             return;         }       rear->next=n;       rear=n;}
//Function to pop front element from the queue.int MyQueue :: pop(){   int ans;      if(front==NULL)      ans =-1;      else      {          ans=front->data;          front =front->next;                }            return ans;}
0
tarunkanade2 weeks ago
0.39/2.2
void push(int a)
	{
        // Your code here
        QueueNode x = new QueueNode(a);
        
        if(rear == null){
            rear = front = x;
        }else{
            rear.next = x;
            rear = x;
        }
	}
	
    //Function to pop front element from the queue.
	int pop()
	{
        // Your code here
        if(front == null){
            return -1;
        }
        int x = front.data;
        front = front.next;
        if(front == null){
            rear = front;
        }
        return x;
	}
+1
swastikp17111 month ago
Easy to Understand Java Solution
class MyQueue
{
    QueueNode front, rear;
    
    MyQueue(){
        front=rear=null;
    }
    
    //Function to push an element into the queue.
	void push(int a)
	{
	    QueueNode newNode=new QueueNode(a);
	    
        if(front==null){         // Queue is Empty
            front=rear=newNode;
        }else{                   // Queue is not Empty
            rear.next=newNode;
            rear=newNode;
        }
	}
	
    //Function to pop front element from the queue.
	int pop()
	{
        if(front==null) return -1;  // Queue is Empty
        
        // Queue is not Empty then,
        int ans=front.data;
        front=front.next;
        
        return ans;
	}
}
0
sriutkarsh19032 months ago
// Simple Java Solution
 
class MyQueue{   QueueNode front, rear;      //Function to push an element into the queue.void push(int a){       // Your code here       QueueNode newnode=new QueueNode(a);       if(front==null)       {           front=rear=newnode; return;       }       rear.next=newnode;       rear=newnode;       }   //Function to pop front element from the queue.int pop(){       // Your code here       if(front==null){return -1;}       int res=front.data;       front=front.next;       return res;}}
0
chinmayee7313 months ago
#Python Code
class Node: 
    def __init__(self, data): 
        self.data = data 
        self.next = None
        
class MyQueue:
    def __init__(self):
        self.head = None
        self.tail = None
        
    def push(self, item):
        new_node = Node(item)
        if self.head is None or self.tail is None:
            self.head = new_node
            self.tail = new_node
        else:
            self.tail.next = new_node
            self.tail = self.tail.next
            
    def pop(self):
        if self.head==None:
            return -1
        item = self.head.data
        self.head = self.head.next
        return item
+1
kronizerdeltac4 months ago
JAVA SOLUTION:
class MyQueue{   QueueNode front, rear;      //Function to push an element into the queue.void push(int a){    //addLast    QueueNode node = new QueueNode(a);       if(front == null)           front = rear = node;       else {           rear.next = node;           rear = node;       }}   //Function to pop front element from the queue.int pop(){       //removeFirst       if(front == null) {           return -1;       } else if(front.next == null) {           int val = front.data;           front = rear = null;           return val;       } else {           QueueNode fwd = front.next;           int val = front.data;           front.next = null;           front = fwd;           return val;       }}} 
-1
sharmarahul996116 months ago
void MyQueue:: push(int x){       // Your Code       QueueNode *temp = new QueueNode(x);       if(front==NULL){           front = rear =temp;       }else{           rear->next =temp;           rear=temp;       }}
//Function to pop front element from the queue.int MyQueue :: pop(){       // Your Code          if(front==NULL){           return -1;       }           int result =front->data;           front=front->next;              return result;}
 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.