Skip to content

Commit

Permalink
implement queue using LL
Browse files Browse the repository at this point in the history
  • Loading branch information
sachuverma committed May 3, 2021
1 parent 2eb9f21 commit 68bb9df
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"complex": "cpp",
"iostream": "cpp",
"cmath": "cpp",
"*.tcc": "cpp"
"*.tcc": "cpp",
"any": "cpp",
"type_traits": "cpp"
}
}
99 changes: 99 additions & 0 deletions Striver Sheet/Day-13/Implement Queue using Linked List.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
Implement Queue using Linked List
=================================
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
*/

/* Structure of a node in Queue
struct QueueNode
{
int data;
QueueNode *next;
QueueNode(int a)
{
data = a;
next = NULL;
}
};
And structure of MyQueue
struct MyQueue {
QueueNode *front;
QueueNode *rear;
void push(int);
int pop();
MyQueue() {front = rear = NULL;}
}; */

//Function to push an element into the queue.
void MyQueue::push(int x)
{
QueueNode *nN = new QueueNode(x);
if (!front && !rear)
{
front = nN;
rear = nN;
}
else
{
rear->next = nN;
rear = nN;
}
}

//Function to pop front element from the queue.
int MyQueue ::pop()
{
if (front == NULL)
return -1;
int ans = front->data;
if (front == rear)
{
front = NULL;
rear = NULL;
}
else
{
front = front->next;
}
return ans;
}
2 changes: 1 addition & 1 deletion Striver Sheet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
- [Implement stack using array](https://practice.geeksforgeeks.org/problems/implement-stack-using-array/1#) - [Cpp Soultion](./Day-13/Implement%20stack%20using%20array.cpp)
- [Implement Queue using array](https://practice.geeksforgeeks.org/problems/implement-queue-using-array/1#) - [Cpp Soultion](./Day-13/Implement%20Queue%20using%20array.cpp)
- [Implement Stack using Linked List](https://practice.geeksforgeeks.org/problems/implement-stack-using-linked-list/1) - [Cpp Soultion](./Day-13/Implement%20Stack%20using%20Linked%20List.cpp)
- []() - [Cpp Soultion](./Day-13/.cpp)
- [Implement Queue using Linked List](https://practice.geeksforgeeks.org/problems/implement-queue-using-linked-list/1) - [Cpp Soultion](./Day-13/Implement%20Queue%20using%20Linked%20List.cpp)
- []() - [Cpp Soultion](./Day-13/.cpp)
- []() - [Cpp Soultion](./Day-13/.cpp)
- []() - [Cpp Soultion](./Day-13/.cpp)
Expand Down

0 comments on commit 68bb9df

Please sign in to comment.