-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue using Two Stacks.java
More file actions
73 lines (68 loc) · 1.75 KB
/
Queue using Two Stacks.java
File metadata and controls
73 lines (68 loc) · 1.75 KB
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
64
65
66
67
68
69
70
71
72
73
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
class Main
{
static Stack<Integer> s1=new Stack<>();
static Stack<Integer> s2=new Stack<>();
static void enqueue(int value){
s1.push(value);
}
static void dequeue(){
if(s1.isEmpty())
{
System.out.println("Queue is Empty");
return;
}
while(!s1.isEmpty())
{
s2.push(s1.pop());
}
s2.pop();
while(!s2.isEmpty())
{
s1.push(s2.pop());
}
}
static int front(){
if(s1.isEmpty())
{
return Integer.MAX_VALUE;
}
while(!s1.isEmpty())
{
s2.push(s1.pop());
}
int y=s2.peek();
while(!s2.isEmpty())
{
s1.push(s2.pop());
}
return y;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String input=sc.nextLine();
StringTokenizer st=new StringTokenizer(input,",");
while(st.hasMoreTokens())
{
String str=st.nextToken();
StringTokenizer temp=new StringTokenizer(str);
int operation=Integer.parseInt(temp.nextToken());
if(operation==1)
{
int value=Integer.parseInt(temp.nextToken());
enqueue(value);
}
else if(operation==2)
{
dequeue();
}
else if(operation==3)
{
System.out.println(front());
}
}
}
}