forked from jaspreetkaur96/CodingQuestions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack wit 2 queues.java
64 lines (55 loc) · 1.09 KB
/
stack wit 2 queues.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
{
import java.util.*;
class StackUsingQueues
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t>0)
{
Queues g = new Queues();
int q = sc.nextInt();
while(q>0)
{
int QueryType = sc.nextInt();
if(QueryType == 1)
{
int a = sc.nextInt();
g.push(a);
}
else if(QueryType == 2)
System.out.print(g.pop()+" ");
q--;
}
System.out.println();
t--;
}
}
}
}
/*This is a function problem.You only need to complete the function given below*/
class Queues
{
Queue<Integer> q1 = new LinkedList<Integer>();
Queue<Integer> q2 = new LinkedList<Integer>();
/*The method pop which return the element poped out of the stack*/
int pop()
{
if(!q1.isEmpty())
return q1.poll();
else return -1;
}
/* The method push to push element into the stack */
void push(int a)
{
q2.add(a);
while(!q1.isEmpty())
{
q2.add(q1.poll());
}
Queue<Integer> temp = q1;
q1 = q2;
q2 = temp;
}
}