-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStackUsingQueuePushEff.java
More file actions
61 lines (56 loc) · 1.34 KB
/
StackUsingQueuePushEff.java
File metadata and controls
61 lines (56 loc) · 1.34 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
package lecture9a15;
import lecture9a13.queue;
import lecture9a14.QueueReverseDisplay;
public class StackUsingQueuePushEff {
DynamicQueue q = new DynamicQueue();
public void push(int item) throws Exception {
try {
q.enqueue(item);
}catch(Exception e ) {
throw new Exception("Stack is Full!");
}
}
public int pop() throws Exception {
try{for(int i=1 ; i<q.size() ; i++) {
q.enqueue(q.dequeue());
}
int rv= q.dequeue();
return rv;}catch(Exception e) {
throw new Exception("Stack is Empty!");
}
}
public int size() {
return this.size();
}
public boolean isEmpty() {
return this.isEmpty();
}
public boolean isFull() {
return this.isFull();
}
public void display() throws Exception {
System.out.println("-------------");
reverseDisplay(q,0);
System.out.println(".");
System.out.println("------------");
}
public static void reverseDisplay(DynamicQueue q , int i) throws Exception {
if(q.size()==i) {
return ;
}
int temp = q.dequeue();
q.enqueue(temp);
reverseDisplay(q,i+1);
System.out.println(temp);
}
public int peek() throws Exception {
try{for(int i=1 ; i<q.size() ; i++) {
q.enqueue(q.dequeue());
}
int rv= q.getFront();
q.enqueue(q.dequeue());
return rv;}catch(Exception e) {
throw new Exception("Stack is Empty!");
}
}
}