-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueueUsing2Stack.java
107 lines (91 loc) · 2.82 KB
/
queueUsing2Stack.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Queue<T>{ // This Queue class moves data for dequeue operation from 1 stack1 to stack2
Stack<T> newStack = new Stack<T>();
Stack<T> oldStack = new Stack<T>();
public void enqueue(T value)
{
oldStack.push(value);
}
public void dequeue()
{
while(!oldStack.empty()) // transfer data to newStack
{
newStack.push(oldStack.peek());
oldStack.pop();
}
newStack.pop(); // pop data from newstack (which in the sence pop front of oldStack)
while(!newStack.empty()) // move data again in it's place
{
oldStack.push(newStack.peek());
newStack.pop();
}
}
public void printFront()
{
System.out.println(oldStack.firstElement()); // return the bottom element in the stack
}
}
/* 1. this OptimalQueue class saves data moving in dequeue operation every time
2. for first time dequeue move all element from oldStack to newStack
3. then for every pop operation pop element from newStack
4. emqueue every element in oldStack
5. if newStack is empty repeat from second processure
*/
class OptimalQueue<T>{
Stack<T> newStack = new Stack<T>();
Stack<T> oldStack = new Stack<T>();
public void enqueue(T value)
{
oldStack.push(value);
}
public void dequeue()
{
cheack();
newStack.pop();
}
public void printFront()
{ cheack();
System.out.println(newStack.peek()); // print front element in the stack
}
public void cheack()
{
if(newStack.empty()) // cheack is newStack empty if yes rearrange data again
{
while(!oldStack.empty())
{
newStack.push(oldStack.peek());
oldStack.pop();
}
}
}
}
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
//Queue<Integer> Q = new Queue<Integer>();
OptimalQueue<Integer> Q = new OptimalQueue<Integer>();
Scanner s = new Scanner(System.in);
int n = s.nextInt();
for(int i=1; i <= n; i++)
{
int num = s.nextInt();
if(num == 1)
{
Q.enqueue(s.nextInt());
}
if(num == 2)
{
Q.dequeue();
}
if(num == 3)
{
Q.printFront();
}
}
s.close();
}
}