-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathQueueUsingStackEnqueueEff.java
More file actions
75 lines (65 loc) · 1.44 KB
/
QueueUsingStackEnqueueEff.java
File metadata and controls
75 lines (65 loc) · 1.44 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
package lecture9a15;
import lecture9a13.Stack;
public class QueueUsingStackEnqueueEff {
DynamicStack s = new DynamicStack();
public void enqueue(int item) throws Exception {
try {
s.push(item);
} catch (Exception e) {
throw new Exception("Queue is full!");
}
}
public int dequeue() throws Exception {
try {
DynamicStack temp = new DynamicStack();
for (int i =1 ; i < s.size(); ) {
temp.push(s.pop());
}
int rv = s.pop();
while (!temp.isEmpty()) {
s.push(temp.pop());
}
return rv;
} catch (Exception e) {
throw new Exception("Queue is Empty");
}
}
public int size() {
return s.size();
}
public int front() throws Exception {
try {
DynamicStack temp = new DynamicStack();
for (int i = 1; i < s.size(); ) {
temp.push(s.pop());
}
int rv = s.peek();
while (!temp.isEmpty()) {
s.push(temp.pop());
}
return rv;
} catch (Exception e) {
throw new Exception("Queue is Empty");
}
}
public void display() throws Exception {
DynamicStack temp = new DynamicStack();
while(!s.isEmpty()) {
temp.push(s.pop());
}
System.out.println("------------");
while (!temp.isEmpty()) {
int r = temp.pop();
s.push(r);
System.out.print(r + " ");
}
System.out.println(".");
System.out.println("-----------------");
}
public boolean isEmpty() {
return this.isEmpty();
}
public boolean isFull() {
return this.isFull();
}
}