Skip to content

Commit beeb3fe

Browse files
authored
Create Queue-Stack Using 2 Queues
1 parent 54e98dd commit beeb3fe

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

Queue-Stack Using 2 Queues

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
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.
2+
There should be two data members, both being Queues to store the data internally. You may use the inbuilt Queue.
3+
Implement the following public functions :
4+
1. Constructor:
5+
It initialises the data members as required.
6+
2. push(data) :
7+
This function should take one argument of type integer. It pushes the element into the stack and returns nothing.
8+
3. pop() :
9+
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.
10+
4. top :
11+
It returns the element being kept at the top of the stack. In case the stack is empty, it returns -1.
12+
5. size() :
13+
It returns the size of the stack at any given instance of time.
14+
6. isEmpty() :
15+
It returns a boolean value indicating whether the stack is empty or not.
16+
Operations Performed on the Stack:
17+
Query-1(Denoted by an integer 1): Pushes an integer data to the stack.
18+
19+
Query-2(Denoted by an integer 2): Pops the data kept at the top of the stack and returns it to the caller.
20+
21+
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.
22+
23+
Query-4(Denoted by an integer 4): Returns the current size of the stack.
24+
25+
Query-5(Denoted by an integer 5): Returns a boolean value denoting whether the stack is empty or not.
26+
Input Format:
27+
The first line contains an integer 'q' which denotes the number of queries to be run against each operation in the stack.
28+
Then the test cases follow.
29+
30+
Every 'q' lines represent an operation that needs to be performed.
31+
32+
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.
33+
34+
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.
35+
Output Format:
36+
For Query-1, you do not need to return anything.
37+
For Query-2, prints the data being popped from the stack.
38+
For Query-3, prints the data kept on the top of the stack.
39+
For Query-4, prints the current size of the stack.
40+
For Query-5, prints 'true' or 'false'(without quotes).
41+
42+
Output for every query will be printed in a separate line.
43+
Note:
44+
You are not required to print anything explicitly. It has already been taken care of. Just implement the function.
45+
Constraints:
46+
1 <= q <= 100
47+
1 <= x <= 5
48+
-2^31 <= data <= 2^31 - 1 and data != -1
49+
50+
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.
51+
52+
Time Limit: 1 second
53+
Sample Input 1:
54+
6
55+
1 13
56+
1 47
57+
4
58+
5
59+
2
60+
3
61+
Sample Output 1:
62+
2
63+
false
64+
47
65+
13
66+
Sample Input 2:
67+
4
68+
5
69+
2
70+
1 10
71+
5
72+
Sample Output 2:
73+
true
74+
-1
75+
false
76+
77+
***************************************************Code******************************************************
78+
import java.util.Queue;
79+
import java.util.LinkedList;
80+
81+
public class Stack {
82+
Queue<Integer> q1;
83+
Queue<Integer> q2;
84+
85+
public Stack() {
86+
//Implement the Constructor
87+
q1=new LinkedList<Integer>();
88+
q2=new LinkedList<Integer>();
89+
}
90+
91+
92+
93+
/*----------------- Public Functions of Stack -----------------*/
94+
95+
96+
public int getSize() {
97+
return q1.size();
98+
}
99+
100+
public boolean isEmpty() {
101+
return q1.isEmpty();
102+
}
103+
104+
public void push(int element) {
105+
106+
if(q1.size()==0)
107+
{
108+
q1.add(element);
109+
}
110+
else{
111+
while(!q1.isEmpty())
112+
{
113+
q2.add(q1.remove());
114+
}
115+
q1.add(element);
116+
while(!q2.isEmpty())
117+
{
118+
q1.add(q2.remove());
119+
}
120+
}
121+
}
122+
123+
public int pop() {
124+
125+
if(q1.isEmpty())
126+
return -1;
127+
else{
128+
return q1.remove();
129+
}
130+
}
131+
132+
public int top() {
133+
134+
if(q1.isEmpty())
135+
return -1;
136+
137+
return q1.peek();
138+
}
139+
140+
}

0 commit comments

Comments
 (0)