-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathQueueTwoStacks.java
More file actions
113 lines (101 loc) · 2.5 KB
/
QueueTwoStacks.java
File metadata and controls
113 lines (101 loc) · 2.5 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
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
108
109
110
111
112
113
import java.util.Set;
/**
* Created by Devang on 29-Dec-16.
*/
public class QueueTwoStacks<E> {
protected MyStack<E> in = new MyStack<>();
protected MyStack<E> out = new MyStack<>();
int size;
public QueueTwoStacks() {
size = 0;
}
public void enqueue(E item) {
in.push(item);
size++;
}
public E dequeue() {
if (size == 0)
return null;
if (out.isEmpty())
fill();
size--;
return out.pop();
}
protected void fill() {
while (!in.isEmpty()) {
out.push(in.pop());
}
}
public int size() {
return size;
}
public Boolean isEmpty() {
if (size == 0)
return true;
return false;
}
public E front() {
if (size == 0)
return null;
if (out.isEmpty())
fill();
return out.peek();
}
@Override
public String toString() {
if (size == 0)
return null;
if (!in.isEmpty() && !out.isEmpty()) {
MyStack<E> temp = new MyStack<>();
while (!in.isEmpty()) {
temp.push(in.pop());
}
while (!out.isEmpty()) {
in.push(out.pop());
}
while (!temp.isEmpty()) {
in.push(temp.pop());
}
} else if (!out.isEmpty()) {
while (!out.isEmpty()) {
in.push(out.pop());
}
}
return in.toString();
}
public static void main(String[] args) {
//Integer
QueueTwoStacks<Integer> q1 = new QueueTwoStacks<>();
System.out.println("Dequeue: " + q1.dequeue());
System.out.println("IsEmpty: " + q1.isEmpty());
q1.enqueue(2);
q1.enqueue(3);
q1.enqueue(5);
q1.enqueue(7);
q1.enqueue(11);
q1.enqueue(13);
System.out.println(q1);
System.out.println("Size: " + q1.size());
System.out.println("Dequeue: " + q1.dequeue());
System.out.println(q1);
System.out.println("IsEmpty: " + q1.isEmpty());
System.out.println("Front: " + q1.front());
System.out.println();
//String
QueueTwoStacks<String> q2 = new QueueTwoStacks<>();
q2.enqueue("Virat Kohli");
q2.enqueue("Yuvraj Singh");
q2.enqueue("Mahendra Singh Dhoni");
q2.enqueue("Ravindra Jadeja");
q2.enqueue("Rohit Sharma");
q2.enqueue("Umesh Yadav");
System.out.println(q2);
System.out.println("Dequeue: " + q2.dequeue());
System.out.println("Dequeue: " + q2.dequeue());
System.out.println(q2);
System.out.println("Size: " + q2.size());
System.out.println("IsEmpty: " + q2.isEmpty());
System.out.println("Front: " + q2.front());
System.out.println();
}
}