blob: 7eb146a338f6493f69e97e8fccb971961c308965 (
plain) (
blame)
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
114
115
|
class Deque<Base> {
private Node head;
private class Node {
private Base object;
private Node left;
private Node right;
private Node(Base o, Node l, Node r) {
object = o;
left = l; right = r;
}
}
public Deque() {
head = new Node(null, null, null);
head.left=head; head.right=head;
}
public void enqueueRear(Base object) {
Node tempL = head;
Node newNode = new Node(object, tempL.left, tempL);
tempL.left.right = newNode;
tempL.left = newNode;
}
public void enqueueFront(Base object) {
Node tempR = head;
Node newNode = new Node(object, tempR, tempR.right);
tempR.right.left = newNode;
tempR.right = newNode;
}
public Base dequeueRear() {
if (isEmpty()) {
throw new IllegalStateException();
} else {
Node temp = head.left;
temp.left.right = head;
head.left = temp.left;
return temp.object;
}
}
public Base dequeueFront() {
if (isEmpty()) {
throw new IllegalStateException();
} else {
Node temp = head.right;
temp.right.left = head;
head.right = temp.right;
return temp.object;
}
}
public Boolean isEmpty() {
return head.right==head && head.left==head;
}
}
// OBSERVATION DEQUE. Test the class DEQUE. 40 points total.
class ObservationDeque{
// MAIN. Test the DEQUE on various example arguments.
public static void main(String [] args) {
Deque<String> deque = new Deque<String>();
System.out.println(deque.isEmpty()); // true 2 points.
try {
System.out.println(deque.dequeueFront());
} catch (IllegalStateException ignore) {
System.out.println("No dequeueFront."); // No dequeueFront. 2 points.
}
try { System.out.println(deque.dequeueRear());
} catch (IllegalStateException ignore) {
System.out.println("No dequeueRear."); // No dequeueRear. 2 points.
}
//Enqueueing to the rear and dequeueing from the rear makes the DEQUE act
// like a stack.
deque.enqueueRear("A");
deque.enqueueRear("B");
deque.enqueueRear("C");
System.out.println(deque.isEmpty()); // false 2 points.
System.out.println(deque.dequeueRear()); // C 2 points.
System.out.println(deque.dequeueRear()); // B 2 points.
System.out.println(deque.dequeueRear()); // A 2 points.
System.out.println(deque.isEmpty()); // true 2 points.
// Enqueueing to the rear and dequeueing from the front makes the DEQUE act
// like a queue.
deque.enqueueRear("A");
deque.enqueueRear("B");
deque.enqueueRear("C");
System.out.println(deque.dequeueFront()); // A 2 points.
System.out.println(deque.dequeueFront()); // B 2 points.
System.out.println(deque.dequeueFront()); // C 2 points.
System.out.println(deque.isEmpty()); // true 2 points.
//Enqueueing to the front and dequeueing from the front makes the DEQUE act
//like a stack.
deque.enqueueFront("A");
deque.enqueueFront("B");
deque.enqueueFront("C");
System.out.println(deque.dequeueFront()); // C 2 points.
System.out.println(deque.dequeueFront()); // B 2 points.
System.out.println(deque.dequeueFront()); // A 2 points.
System.out.println(deque.isEmpty()); // true 2 points.
// Enqueueing to the front and dequeueing from the rear makes the DEQUE act
// like a queue.
deque.enqueueFront("A");
deque.enqueueFront("B");
deque.enqueueFront("C");
System.out.println(deque.dequeueRear()); // A 2 points.
System.out.println(deque.dequeueRear()); // B 2 points.
System.out.println(deque.dequeueRear()); // C 2 points.
System.out.println(deque.isEmpty()); // true 2 points.
}
}
|