AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
aud.DList< T > Class Template Reference

Implementation of a doubly linked list. More...

+ Inheritance diagram for aud.DList< T >:
+ Collaboration diagram for aud.DList< T >:

Classes

class  BackwardIterator
 Backward iterator. More...
 
class  Backwards
 Utility class to obtain a BackwardIterator. More...
 
class  ForwardIterator
 Forward iterator. More...
 
class  Node
 

Public Member Functions

 DList ()
 create empty list More...
 
int size ()
 determine number of entries [O(n)] More...
 
boolean empty ()
 determine if list is empty [O(1)] More...
 
front ()
 retrieve first entry (must exist) [O(1)] More...
 
back ()
 retrieve last entry (must exist) [O(1)] More...
 
at (int i)
 retrieve i-th entry [O(n)] More...
 
void push_front (T obj)
 insert entry at front of list [O(1)] More...
 
void push_back (T obj)
 append entry obj at end of list [O(1)] More...
 
void insert (int i, T obj)
 insert new entry obj at position i [O(n)] More...
 
void pop_front ()
 erase first entry (must exist) [O(1)] More...
 
void pop_back ()
 erase last entry (must exist) [O(1)] More...
 
void erase (int i)
 erase entry at position i [O(n)] More...
 
void clear ()
 Erase all entries. More...
 
ForwardIterator iterator ()
 get forward iterator More...
 
BackwardIterator reverse_iterator ()
 get backward iterator iterator More...
 
Backwards backwards ()
 Get instance that provides a BackwardIterator. More...
 
void insert_after (ForwardIterator i, T object)
 insert entry after iterator position More...
 
void insert_before (ForwardIterator i, T object)
 insert entry before iterator position More...
 
String toString ()
 
String toDot ()
 Get dot representation. More...
 
String toDot ()
 Get dot representation. More...
 

Protected Attributes

Node head_ = null
 
Node tail_ = null
 

Detailed Description

Implementation of a doubly linked list.

The implementation is inspired by std::list of the C++ standard library.

DList does not define a bidirectional ListIterator.

DList defines ForwardIterator (standard) iterator and BackwardIterator reverse_iterator.

  • Iterators remain valid after insertion.

  • Iterators remain valid after removing entries, unless the current entry (that an iterator "points to") is removed.

  • There are methods to insert entries before and after an interator.

The utility class Backwards allows for using loops like


for (T item : dlist.backwards()) { ... }

Implementation note: Methods that access (or erase) entries generally assume that entries exist (!empty). A java.lang.IndexOutOfBoundsException is thrown otherwise (see utility method check()).

Definition at line 46 of file DList.java.

Constructor & Destructor Documentation

◆ DList()

aud.DList< T >.DList ( )

create empty list

Definition at line 73 of file DList.java.

73{}

Member Function Documentation

◆ at()

T aud.DList< T >.at ( int  i)

retrieve i-th entry [O(n)]

Exceptions
IndexOutOfBoundsException

Definition at line 131 of file DList.java.

131 {
132 Node node=head_;
133 for (int j=0;node!=null && j<i;++j)
134 node=node.next_;
135 return check(node).data_;
136 }
Node head_
Definition: DList.java:59

References aud.DList< T >.head_.

Referenced by aud.test.DListTest.testEntries(), aud.test.DListTest.testInvalid_at(), and aud.test.DListTest.testList().

+ Here is the caller graph for this function:

◆ back()

T aud.DList< T >.back ( )

retrieve last entry (must exist) [O(1)]

Definition at line 122 of file DList.java.

122 {
123 return check(tail_).data_;
124 }
Node tail_
Definition: DList.java:60

Referenced by aud.test.DListTest.testInvalid_back(), and aud.test.DListTest.testList().

+ Here is the caller graph for this function:

◆ backwards()

Backwards aud.DList< T >.backwards ( )

Get instance that provides a BackwardIterator.

You cannot use reverse_iterator directly in a for-each loop, because this loop assumes the iterator is provided via the Iterable interface, which is already implemented (in forward direction).

The utility class Backwards implements Iterable and provides a BackwardIterator. Then in a for-each loop, instead of list just write list.backwards.

Definition at line 346 of file DList.java.

346 {
347 return new Backwards(this);
348 }

Referenced by aud.test.DListTest.testIterators().

+ Here is the caller graph for this function:

◆ clear()

void aud.DList< T >.clear ( )

Erase all entries.

[O(1)]

Definition at line 245 of file DList.java.

245{ head_=tail_=null; }

References aud.DList< T >.head_, and aud.DList< T >.tail_.

◆ empty()

boolean aud.DList< T >.empty ( )

determine if list is empty [O(1)]

Definition at line 92 of file DList.java.

92 {
93 assert((head_==null&&tail_==null)||
94 (head_!=null && tail_!=null));
95 return head_==null;
96 }

References aud.DList< T >.head_, and aud.DList< T >.tail_.

Referenced by aud.test.DListTest.testCtor(), and aud.test.DListTest.testList().

+ Here is the caller graph for this function:

◆ erase()

void aud.DList< T >.erase ( int  i)

erase entry at position i [O(n)]

Definition at line 224 of file DList.java.

224 {
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 }

References aud.DList< T >.head_.

Referenced by aud.test.DListTest.testInvalid_erase(), and aud.test.DListTest.testList().

+ Here is the caller graph for this function:

◆ front()

T aud.DList< T >.front ( )

retrieve first entry (must exist) [O(1)]

Definition at line 113 of file DList.java.

113 {
114 return check(head_).data_;
115 }

Referenced by aud.test.DListTest.testEntries(), aud.test.DListTest.testInvalid_front(), and aud.test.DListTest.testList().

+ Here is the caller graph for this function:

◆ insert()

void aud.DList< T >.insert ( int  i,
obj 
)

insert new entry obj at position i [O(n)]

Definition at line 185 of file DList.java.

185 {
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 }

Referenced by aud.util.SingleStepperDemo.main(), and aud.test.DListTest.testList().

+ Here is the caller graph for this function:

◆ insert_after()

void aud.DList< T >.insert_after ( ForwardIterator  i,
object 
)

insert entry after iterator position

Definition at line 351 of file DList.java.

351 {
352 check(i.node_);
353 insert_after(i.node_,object);
354 }

◆ insert_before()

void aud.DList< T >.insert_before ( ForwardIterator  i,
object 
)

insert entry before iterator position

Definition at line 356 of file DList.java.

356 {
357 check(i.node_);
358 insert_after(i.node_.prev_,object);
359 }

Referenced by aud.test.DListTest.testIterators().

+ Here is the caller graph for this function:

◆ iterator()

ForwardIterator aud.DList< T >.iterator ( )

get forward iterator

Definition at line 303 of file DList.java.

303 {
304 return new ForwardIterator(head_);
305 }

References aud.DList< T >.head_.

Referenced by aud.test.DListTest.testIterators().

+ Here is the caller graph for this function:

◆ pop_back()

void aud.DList< T >.pop_back ( )

erase last entry (must exist) [O(1)]

Definition at line 212 of file DList.java.

212 {
213 tail_=check(tail_).prev_;
214 if (tail_==null)
215 head_=null;
216 else
217 tail_.next_=null;
218 }

References aud.DList< T >.tail_.

Referenced by aud.test.DListTest.testInvalid_pop_back(), and aud.test.DListTest.testList().

+ Here is the caller graph for this function:

◆ pop_front()

void aud.DList< T >.pop_front ( )

erase first entry (must exist) [O(1)]

Definition at line 200 of file DList.java.

200 {
201 head_=check(head_).next_;
202 if (head_==null)
203 tail_=null;
204 else
205 head_.prev_=null;
206 }

References aud.DList< T >.head_.

Referenced by aud.test.DListTest.testInvalid_pop_front(), and aud.test.DListTest.testList().

+ Here is the caller graph for this function:

◆ push_back()

void aud.DList< T >.push_back ( obj)

append entry obj at end of list [O(1)]

Definition at line 154 of file DList.java.

154 {
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 }

References aud.DList< T >.head_, and aud.DList< T >.tail_.

Referenced by aud.util.SingleStepperDemo.main(), aud.test.DListTest.testIterators(), and aud.test.DListTest.testList().

+ Here is the caller graph for this function:

◆ push_front()

void aud.DList< T >.push_front ( obj)

insert entry at front of list [O(1)]

Definition at line 142 of file DList.java.

142 {
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 }

References aud.DList< T >.head_, and aud.DList< T >.tail_.

Referenced by aud.util.SingleStepperDemo.main(), aud.test.DListTest.testEntries(), aud.test.DListTest.testInvalid_at(), aud.test.DListTest.testInvalid_erase(), aud.test.DListTest.testList(), and aud.test.DListTest.testSize().

+ Here is the caller graph for this function:

◆ reverse_iterator()

BackwardIterator aud.DList< T >.reverse_iterator ( )

get backward iterator iterator

Definition at line 307 of file DList.java.

307 {
308 return new BackwardIterator(tail_);
309 }

References aud.DList< T >.tail_.

Referenced by aud.test.DListTest.testIterators().

+ Here is the caller graph for this function:

◆ size()

int aud.DList< T >.size ( )

determine number of entries [O(n)]

Definition at line 79 of file DList.java.

79 {
80 int n=0;
81 Node node=head_;
82 while (node!=null) {
83 node=node.next_;
84 ++n;
85 }
86 return n;
87 }

References aud.DList< T >.head_.

Referenced by aud.test.DListTest.testEntries(), aud.test.DListTest.testList(), and aud.test.DListTest.testSize().

+ Here is the caller graph for this function:

◆ toDot()

String aud.DList< T >.toDot ( )

Get dot representation.

Implements aud.util.Graphvizable.

Definition at line 380 of file DList.java.

380 {
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 }

References aud.DList< T >.head_.

◆ toString()

String aud.DList< T >.toString ( )

Definition at line 366 of file DList.java.

366 {
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 }

References aud.DList< T >.head_.

Member Data Documentation

◆ head_

◆ tail_


The documentation for this class was generated from the following file: