AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
DList.java
Go to the documentation of this file.
1package aud;
2
3import java.util.NoSuchElementException;
4
6
45//@<dlist:class
46public class DList<T> implements Iterable<T>,
48 protected class Node {
49 T data_ = null;
50 Node next_ = null;
51 Node prev_ = null;
52
53 public Node(T obj,
54 Node prv,Node nxt) {
55 data_=obj; prev_=prv; next_=nxt;
56 }
57 }
58
59 protected Node head_ = null;
60 protected Node tail_ = null;
61 //@>dlist:class
62
63 //
64 // NOTE: Nested class Node and attributes head_ and tail_ are
65 // defined protected (not package private) for use in an
66 // assignment.
67 //
68
69
72 //@<dlist:ctor
73 public DList() {}
74 //@>dlist:ctor
75
78 //@<dlist:size
79 public int size() {
80 int n=0;
81 Node node=head_;
82 while (node!=null) {
83 node=node.next_;
84 ++n;
85 }
86 return n;
87 }
88 //@>dlist:size
89
92 public boolean empty() {
93 assert((head_==null&&tail_==null)||
94 (head_!=null && tail_!=null));
95 return head_==null;
96 }
97
102 //@<dlist:check
103 Node check(Node node) {
104 if (node==null)
105 throw new IndexOutOfBoundsException();
106 return node;
107 }
108 //@>dlist:check
109
112 //@<dlist:front
113 public T front() {
114 return check(head_).data_;
115 }
116 //@>dlist:front
117
118
121 //@<dlist:back
122 public T back() {
123 return check(tail_).data_;
124 }
125 //@>dlist:back
126
130 //@<dlist:at
131 public T at(int i) {
132 Node node=head_;
133 for (int j=0;node!=null && j<i;++j)
134 node=node.next_;
135 return check(node).data_;
136 }
137 //@>dlist:at
138
141 //@<dlist:push_front
142 public void push_front(T obj) {
143 head_=new Node(obj,null,head_);
144 if (head_.next_!=null)
145 head_.next_.prev_=head_;
146 if (tail_==null)
147 tail_=head_;
148 }
149 //@>dlist:push_front
150
153 //@<dlist:push_back
154 public void push_back(T obj) {
155 tail_=new Node(obj,tail_,null);
156 if (tail_.prev_!=null)
157 tail_.prev_.next_=tail_;
158 if (head_==null)
159 head_=tail_;
160 }
161 //@>dlist:push_back
162
165 //@<dlist:insert_after
166 void insert_after(Node node,T obj) {
167 Node prv,cur,nxt;
168 if (node==null)
169 push_front(obj);
170 else if (node==tail_)
171 push_back(obj);
172 else {
173 prv=node; // !=null !
174 nxt=node.next_; // !=null !
175 cur=new Node(obj,prv,nxt);
176 prv.next_=cur;
177 nxt.prev_=cur;
178 }
179 }
180 //@>dlist:insert_after
181
184 //@<dlist:insert
185 public void insert(int i,T obj) {
186 if (i==0)
187 insert_after((Node) null,obj);
188 else {
189 Node node=head_;
190 for (int j=0;j<i-1 && node!=null;++j)
191 node=node.next_;
192 insert_after(check(node),obj);
193 }
194 }
195 //@>dlist:insert
196
199 //@<dlist:pop_front
200 public void pop_front() {
201 head_=check(head_).next_;
202 if (head_==null)
203 tail_=null;
204 else
205 head_.prev_=null;
206 }
207 //@>dlist:pop_front
208
211 //@<dlist:pop_back
212 public void pop_back() {
213 tail_=check(tail_).prev_;
214 if (tail_==null)
215 head_=null;
216 else
217 tail_.next_=null;
218 }
219 //@>dlist:pop_back
220
223 //@<dlist:erase
224 public void erase(int i) {
225 Node node=head_;
226 for (int j=0;j<i && node!=null;++j)
227 node=node.next_;
228 check(node);
229
230 if (node.prev_!=null)
231 node.prev_.next_=node.next_;
232 else
233 head_=node.next_; // erased first!
234
235 if (node.next_!=null)
236 node.next_.prev_=node.prev_;
237 else
238 tail_=node.prev_; // erased last!
239 }
240 //@>dlist:erase
241
244 //@<dlist:clear
245 public void clear() { head_=tail_=null; }
246 //@>dlist:clear
247
248 //
249 // iterators
250 //
251
253 public class ForwardIterator implements java.util.Iterator<T> {
254 Node node_ = null;
255 ForwardIterator(Node node) { node_=node; }
256
258 @Override
259 public boolean hasNext() { return node_!=null; }
261 @Override
262 public T next() {
263 if (node_==null)
264 throw new NoSuchElementException();
265 T data=node_.data_;
266 node_=node_.next_;
267 return data;
268 }
270 public T previous() {
271 if (node_==null)
272
273 throw new NoSuchElementException();
274 T data=node_.data_;
275 node_=node_.prev_;
276 return data;
277 }
281 @Override
282 public void remove() {
283 throw new UnsupportedOperationException();
284 }
285 @Override @SuppressWarnings("unchecked")
286 public boolean equals(Object other) {
287 return node_==((ForwardIterator) other).node_;
288 }
289 }
290
295 public class BackwardIterator extends ForwardIterator {
296 BackwardIterator(Node node) { super(node); }
297 @Override public T next() { return super.previous(); }
298 @Override public T previous() { return super.next(); }
299 }
300
302 @Override
304 return new ForwardIterator(head_);
305 }
308 return new BackwardIterator(tail_);
309 }
310
322 public class Backwards implements Iterable<T> {
323 DList<T> list_ = null;
324
325 Backwards(DList<T> list) {
326 list_ = list;
327 }
328
330 @Override public BackwardIterator iterator() {
331 return list_.reverse_iterator();
332 }
333 }
334
347 return new Backwards(this);
348 }
349
351 public void insert_after(ForwardIterator i,T object) {
352 check(i.node_);
353 insert_after(i.node_,object);
354 }
356 public void insert_before(ForwardIterator i,T object) {
357 check(i.node_);
358 insert_after(i.node_.prev_,object);
359 }
360
361 //
362 // output
363 //
364
365 @Override
366 public String toString() {
367 String rv="[";
368 Node node=head_;
369 while (node!=null) {
370 rv+=node.data_.toString();
371 if (node.next_!=null)
372 rv+=",";
373 node=node.next_;
374 }
375 rv+="]";
376 return rv;
377 }
378
379 @Override
380 public String toDot() {
381 String rv="digraph DList {\n\t\n\tnode [shape=box];\n\t";
382 Node node=head_;
383
384 while (node!=null) {
385 String prv=node.prev_!=null ? node.prev_.data_.toString() : "null";
386 String nxt=node.next_!=null ? node.next_.data_.toString() : "null";
387
388 rv+="\""+node.data_.toString()+"\" -> \""+prv
389 +"\" [color=blue,label=prev];\n\t"
390 +"\""+node.data_.toString()+"\" -> \""+nxt
391 +"\" [color=blue,label=next];\n\t";
392
393 node=node.next_;
394 }
395 rv+="\n}\n";
396 return rv;
397 }
398}
Backward iterator.
Definition: DList.java:295
T previous()
return current entry and step backwards
Definition: DList.java:298
T next()
return current entry and advance forward
Definition: DList.java:297
Utility class to obtain a BackwardIterator.
Definition: DList.java:322
BackwardIterator iterator()
get backward iterator for list_
Definition: DList.java:330
Forward iterator.
Definition: DList.java:253
boolean equals(Object other)
Definition: DList.java:286
T previous()
return current entry and step backwards
Definition: DList.java:270
T next()
return current entry and advance forward
Definition: DList.java:262
boolean hasNext()
return true unless "advanced" over tail
Definition: DList.java:259
Node(T obj, Node prv, Node nxt)
Definition: DList.java:53
Implementation of a doubly linked list.
Definition: DList.java:47
String toString()
Definition: DList.java:366
void clear()
Erase all entries.
Definition: DList.java:245
Node head_
Definition: DList.java:59
void pop_front()
erase first entry (must exist) [O(1)]
Definition: DList.java:200
void insert_after(ForwardIterator i, T object)
insert entry after iterator position
Definition: DList.java:351
T back()
retrieve last entry (must exist) [O(1)]
Definition: DList.java:122
int size()
determine number of entries [O(n)]
Definition: DList.java:79
Node tail_
Definition: DList.java:60
void erase(int i)
erase entry at position i [O(n)]
Definition: DList.java:224
void push_front(T obj)
insert entry at front of list [O(1)]
Definition: DList.java:142
void push_back(T obj)
append entry obj at end of list [O(1)]
Definition: DList.java:154
T at(int i)
retrieve i-th entry [O(n)]
Definition: DList.java:131
void pop_back()
erase last entry (must exist) [O(1)]
Definition: DList.java:212
BackwardIterator reverse_iterator()
get backward iterator iterator
Definition: DList.java:307
T front()
retrieve first entry (must exist) [O(1)]
Definition: DList.java:113
DList()
create empty list
Definition: DList.java:73
Backwards backwards()
Get instance that provides a BackwardIterator.
Definition: DList.java:346
ForwardIterator iterator()
get forward iterator
Definition: DList.java:303
String toDot()
Get dot representation.
Definition: DList.java:380
void insert(int i, T obj)
insert new entry obj at position i [O(n)]
Definition: DList.java:185
boolean empty()
determine if list is empty [O(1)]
Definition: DList.java:92
void insert_before(ForwardIterator i, T object)
insert entry before iterator position
Definition: DList.java:356
Interface for GraphViz rendering.
utilities (not related to AuD lecture)
Definition: Colormap.java:1
AuD lecture: Data structures, algorithms, examples.
Definition: A234Tree.java:1