Skip to content

Commit 35529bf

Browse files
Queue DSA Coding Ninjas Completed
1 parent 3ee2737 commit 35529bf

File tree

5 files changed

+451
-0
lines changed

5 files changed

+451
-0
lines changed

06_Queue/01_Queue_MCQ.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
### Queue
2+
1. A queue is a?
3+
4+
Ans: **FIFO(First in First Out)**
5+
6+
### Correct Order
7+
2. If the elements "p","q","r" and "s" are placed in a queue and are deleted one at a time, in what order will they be removed?
8+
9+
Ans: **pqrs**
10+
11+
### Insert Element
12+
3. In optimized linked list implementation of a queue, where does a new element be inserted?
13+
a
14+
Ans: **At the tail of the linked list**
15+
16+
### Worst Case
17+
4. In the linked list implementation of queue, if only front pointer is maintained, which of the following operation take worst case time (i.e. O(n))?
18+
19+
Ans: **Insertion from rear**

06_Queue/02_Queue_Using_LL.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
'''
2+
Queue Using LL
3+
4+
Implement a Queue Data Structure specifically to store integer data using a Singly Linked List.
5+
The data members should be private.
6+
You need to implement the following public functions :
7+
1. Constructor:
8+
It initialises the data members as required.
9+
2. enqueue(data) :
10+
This function should take one argument of type integer. It enqueues the element into the queue and returns nothing.
11+
3. dequeue() :
12+
It dequeues/removes the element from the front of the queue and in turn, returns the element being dequeued or removed. In case the queue is empty, it returns -1.
13+
4. front() :
14+
It returns the element being kept at the front of the queue. In case the queue is empty, it returns -1.
15+
5. getSize() :
16+
It returns the size of the queue at any given instance of time.
17+
6. isEmpty() :
18+
It returns a boolean value indicating whether the queue is empty or not.
19+
Operations Performed on the Stack:
20+
Query-1(Denoted by an integer 1): Enqueues an integer data to the queue.
21+
22+
Query-2(Denoted by an integer 2): Dequeues the data kept at the front of the queue and returns it to the caller.
23+
24+
Query-3(Denoted by an integer 3): Fetches and returns the data being kept at the front of the queue but doesn't remove it, unlike the dequeue function.
25+
26+
Query-4(Denoted by an integer 4): Returns the current size of the queue.
27+
28+
Query-5(Denoted by an integer 5): Returns a boolean value denoting whether the queue is empty or not.
29+
Input Format:
30+
The first line contains an integer 'q' which denotes the number of queries to be run against each operation on the queue.
31+
Then the test cases follow.
32+
33+
Every 'q' lines represent an operation that needs to be performed.
34+
35+
For the enqueue operation, the input line will contain two integers separated by a single space, representing the type of the operation in integer and the integer data being enqueued into the queue.
36+
37+
For the rest of the operations on the queue, the input line will contain only one integer value, representing the query being performed on the queue.
38+
Output Format:
39+
For Query-1, you do not need to return anything.
40+
For Query-2, prints the data being dequeued from the queue.
41+
For Query-3, prints the data kept on the front of the queue.
42+
For Query-4, prints the current size of the queue.
43+
For Query-5, prints 'true' or 'false'(without quotes).
44+
45+
Output for every query will be printed in a separate line.
46+
Note:
47+
You are not required to print anything explicitly. It has already been taken care of. Just implement the functions.
48+
Constraints:
49+
1 <= q <= 10^5
50+
1 <= x <= 5
51+
-2^31 <= data <= 2^31 - 1 and data != -1
52+
53+
Where 'q' is the total number of queries being performed on the queue, 'x' is the range for every query and data represents the integer pushed into the queue.
54+
55+
Time Limit: 1 second
56+
Sample Input 1:
57+
7
58+
1 17
59+
1 23
60+
1 11
61+
2
62+
2
63+
2
64+
2
65+
Sample Output 1:
66+
17
67+
23
68+
11
69+
-1
70+
Sample Input 2:
71+
3
72+
2
73+
1 10
74+
4
75+
Sample Output 2:
76+
-1
77+
1
78+
'''
79+
80+
class Node:
81+
def __init__(self, data):
82+
self.data = data
83+
self.next = None
84+
85+
class Queue:
86+
def __init__(self):
87+
self.__head = None
88+
self.__tail = None
89+
self.__size = 0
90+
91+
def enqueue(self, data):
92+
new_node = Node(data)
93+
if self.__tail is None:
94+
self.__head = new_node
95+
self.__tail = new_node
96+
else:
97+
self.__tail.next = new_node
98+
self.__tail = new_node
99+
self.__size += 1
100+
101+
def dequeue(self):
102+
if self.isEmpty():
103+
return -1
104+
data = self.__head.data
105+
self.__head = self.__head.next
106+
self.__size -= 1
107+
if self.__head is None:
108+
self.__tail = None
109+
return data
110+
111+
def front(self):
112+
if self.isEmpty():
113+
return -1
114+
return self.__head.data
115+
116+
def getSize(self):
117+
return self.__size
118+
119+
def isEmpty(self):
120+
return self.__size == 0
121+
122+
123+
# Driver code
124+
if __name__ == "__main__":
125+
queue = Queue()
126+
127+
q = int(input())
128+
for _ in range(q):
129+
query = list(map(int, input().split()))
130+
131+
if query[0] == 1:
132+
queue.enqueue(query[1])
133+
elif query[0] == 2:
134+
print(queue.dequeue())
135+
elif query[0] == 3:
136+
print(queue.front())
137+
elif query[0] == 4:
138+
print(queue.getSize())
139+
elif query[0] == 5:
140+
print(queue.isEmpty())

06_Queue/03_Stack_Using_2_Queues.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
'''
2+
Stack Using 2 Queues
3+
4+
Implement a Stack Data Structure specifically to store integer data using two Queues. You have to implement it in such a way that the push operation is done in O(1) time and the pop and top operations are done in O(N) time.
5+
There should be two data members, both being Queues to store the data internally. You may use the inbuilt Queue.
6+
Implement the following public functions :
7+
1. Constructor:
8+
It initialises the data members as required.
9+
2. push(data) :
10+
This function should take one argument of type integer. It pushes the element into the stack and returns nothing.
11+
3. pop() :
12+
It pops the element from the top of the stack and in turn, returns the element being popped or deleted. In case the stack is empty, it returns -1.
13+
4. top :
14+
It returns the element being kept at the top of the stack. In case the stack is empty, it returns -1.
15+
5. size() :
16+
It returns the size of the stack at any given instance of time.
17+
6. isEmpty() :
18+
It returns a boolean value indicating whether the stack is empty or not.
19+
Operations Performed on the Stack:
20+
Query-1(Denoted by an integer 1): Pushes an integer data to the stack.
21+
22+
Query-2(Denoted by an integer 2): Pops the data kept at the top of the stack and returns it to the caller.
23+
24+
Query-3(Denoted by an integer 3): Fetches and returns the data being kept at the top of the stack but doesn't remove it, unlike the pop function.
25+
26+
Query-4(Denoted by an integer 4): Returns the current size of the stack.
27+
28+
Query-5(Denoted by an integer 5): Returns a boolean value denoting whether the stack is empty or not.
29+
Input Format:
30+
The first line contains an integer 'q' which denotes the number of queries to be run against each operation in the stack.
31+
Then the test cases follow.
32+
33+
Every 'q' lines represent an operation that needs to be performed.
34+
35+
For the push operation, the input line will contain two integers separated by a single space, representing the type of the operation in integer and the integer data being pushed into the stack.
36+
37+
For the rest of the operations on the stack, the input line will contain only one integer value, representing the query being performed on the stack.
38+
Output Format:
39+
For Query-1, you do not need to return anything.
40+
For Query-2, prints the data being popped from the stack.
41+
For Query-3, prints the data kept on the top of the stack.
42+
For Query-4, prints the current size of the stack.
43+
For Query-5, prints 'true' or 'false'(without quotes).
44+
45+
Output for every query will be printed in a separate line.
46+
Note:
47+
You are not required to print anything explicitly. It has already been taken care of. Just implement the function.
48+
Constraints:
49+
1 <= q <= 100
50+
1 <= x <= 5
51+
-2^31 <= data <= 2^31 - 1 and data != -1
52+
53+
Where 'q' is the total number of queries being performed on the stack, 'x' is the range for every query and data represents the integer pushed into the stack.
54+
55+
Time Limit: 1 second
56+
Sample Input 1:
57+
6
58+
1 13
59+
1 47
60+
4
61+
5
62+
2
63+
3
64+
Sample Output 1:
65+
2
66+
false
67+
47
68+
13
69+
Sample Input 2:
70+
4
71+
5
72+
2
73+
1 10
74+
5
75+
Sample Output 2:
76+
true
77+
-1
78+
false
79+
'''
80+
81+
from queue import Queue
82+
83+
class StackUsingQueues:
84+
def __init__(self):
85+
self.q1 = Queue()
86+
self.q2 = Queue()
87+
self.current_size = 0
88+
89+
def push(self, data):
90+
self.q1.put(data)
91+
self.current_size += 1
92+
93+
def pop(self):
94+
if self.isEmpty():
95+
return -1
96+
97+
# Move all elements from q1 to q2 except the last one
98+
while self.q1.qsize() > 1:
99+
self.q2.put(self.q1.get())
100+
101+
# Pop the last element from q1
102+
popped_element = self.q1.get()
103+
self.current_size -= 1
104+
105+
# Swap q1 and q2
106+
self.q1, self.q2 = self.q2, self.q1
107+
108+
return popped_element
109+
110+
def top(self):
111+
if self.isEmpty():
112+
return -1
113+
114+
# Move all elements from q1 to q2 except the last one
115+
while self.q1.qsize() > 1:
116+
self.q2.put(self.q1.get())
117+
118+
# Get the last element from q1
119+
top_element = self.q1.get()
120+
121+
# Swap q1 and q2
122+
self.q1, self.q2 = self.q2, self.q1
123+
124+
# Put the last element back to q1
125+
self.q1.put(top_element)
126+
127+
return top_element
128+
129+
def size(self):
130+
return self.current_size
131+
132+
def isEmpty(self):
133+
return self.current_size == 0
134+
135+
# Driver code
136+
if __name__ == "__main__":
137+
stack = StackUsingQueues()
138+
139+
q = int(input())
140+
for _ in range(q):
141+
query = list(map(int, input().split()))
142+
143+
if query[0] == 1:
144+
stack.push(query[1])
145+
elif query[0] == 2:
146+
print(stack.pop())
147+
elif query[0] == 3:
148+
print(stack.top())
149+
elif query[0] == 4:
150+
print(stack.size())
151+
elif query[0] == 5:
152+
print(stack.isEmpty())

06_Queue/04_Reverse_Queue.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'''
2+
Reverse Queue
3+
4+
You have been given a queue that can store integers as the data. You are required to write a function that reverses the populated queue itself without using any other data structures.
5+
Example:
6+
7+
8+
Input Format:
9+
The first list of input contains an integer 't' denoting the number of test cases/queries to be run.
10+
Then the test cases follow.
11+
12+
The first line input for each test case/query contains an integer N, denoting the total number of elements in the queue.
13+
14+
The second line of input contains N integers separated by a single space, representing the order in which the elements are enqueued into the queue.
15+
Output Format:
16+
For each test case/query, the only line of output prints the order in which the queue elements are dequeued, all of them separated by a single space.
17+
18+
Output for every test case/query will be printed on a new line.
19+
Note:
20+
You are not required to print the expected output explicitly, it has already been taken care of. Just make the changes in the input queue itself.
21+
Constraints:
22+
1 <= t <= 100
23+
1 <= N <= 10^4
24+
-2^31 <= data <= 2^31 - 1
25+
26+
Time Limit: 1sec
27+
Sample Input 1:
28+
1
29+
6
30+
1 2 3 4 5 10
31+
Note:
32+
Here, 1 is at the front and 10 is at the rear of the queue.
33+
Sample Output 1:
34+
10 5 4 3 2 1
35+
Sample Input 2:
36+
2
37+
5
38+
2 8 15 1 10
39+
3
40+
10 20 30
41+
Sample Output 2:
42+
10 1 15 8 2
43+
30 20 10
44+
'''
45+
46+
from queue import Queue
47+
48+
def reverse_queue(queue):
49+
stack = []
50+
51+
while not queue.empty():
52+
stack.append(queue.get())
53+
54+
while stack:
55+
queue.put(stack.pop())
56+
57+
if __name__ == "__main__":
58+
t = int(input())
59+
for _ in range(t):
60+
n = int(input())
61+
elements = list(map(int,input().split()))
62+
63+
input_queue = Queue()
64+
for element in elements:
65+
input_queue.put(element)
66+
67+
reverse_queue(input_queue)
68+
69+
while not input_queue.empty():
70+
print(input_queue.get(),end=" ")
71+
print()

0 commit comments

Comments
 (0)