-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeque.java
More file actions
147 lines (124 loc) · 3.88 KB
/
Deque.java
File metadata and controls
147 lines (124 loc) · 3.88 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Deque<Item> implements Iterable<Item> {
private final Node first;
private final Node last;
private int sizeStore;
private class Node {
Item item;
Node next;
Node prev;
}
private class Itera<Item2> implements Iterator<Item2> {
private int count;
private Node itemm;
public Itera() {
count = 0;
itemm = first;
}
public boolean hasNext() {
return (count < sizeStore);
}
public Item2 next() {
if (!hasNext()) {
throw new NoSuchElementException();
} else {
itemm = itemm.next;
++count;
return (Item2) itemm.item;
}
}
public void remove() {
throw new UnsupportedOperationException();
}
}
public Deque() {
first = new Node();
last = new Node();
first.next = last;
first.prev = null;
first.item = null;
last.next = null;
last.prev = first;
last.item = null;
sizeStore = 0;
}
public boolean isEmpty() {
return (sizeStore == 0);
}
public int size() {
return sizeStore;
}
public void addFirst(Item i) {
if (i == null) {
throw new IllegalArgumentException("Argument cannot be empty!");
} else {
Node temp = new Node();
temp.item = i;
temp.next = first.next;
temp.prev = first;
first.next = temp;
(temp.next).prev = temp;
++sizeStore;
}
}
public void addLast(Item i) {
if (i == null) {
throw new IllegalArgumentException("Argument cannot be empty!");
} else {
Node temp = new Node();
temp.item = i;
temp.prev = last.prev;
temp.next = last;
last.prev = temp;
(temp.prev).next = temp;
++sizeStore;
}
}
public Item removeFirst() {
if (isEmpty()) {
throw new NoSuchElementException();
} else {
Node delet = new Node();
delet.next = (first.next).next; // make copy of contents
delet.item = (first.next).item;
(first.next).next = null; // remove all stored content so it can be garbage collected
(first.next).prev = null;
(first.next).item = null;
first.next = delet.next; // set next of first to element next to deleted element
(delet.next).prev = first; // set prev of element next to deleted element to first
--sizeStore;
return delet.item; // return deleted item
}
}
public Item removeLast() {
if (isEmpty()) {
throw new NoSuchElementException();
} else {
Node delet = new Node();
delet.prev = (last.prev).prev;
delet.item = (last.prev).item;
(last.prev).next = null;
(last.prev).prev = null;
(last.prev).item = null;
last.prev = delet.prev;
(delet.prev).next = last;
--sizeStore;
return delet.item;
}
}
public Iterator<Item> iterator() {
Iterator<Item> itera = new Itera<Item>();
return itera;
}
public static void main(String[] args) {
Deque<Integer> deck = new Deque<Integer>();
deck.addFirst(32);
deck.addLast(52);
deck.addFirst(44);
deck.addLast(23);
deck.removeLast();
deck.removeFirst();
for (Integer i : deck) System.out.println(i);
}
}