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

Implementation of a singly linked list. More...

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

Classes

class  Iterator
 Forward iterator. More...
 
class  Node
 

Public Member Functions

 SList ()
 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(n)] 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(n)] 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(n)] More...
 
void erase (int i)
 erase entry at position i [O(n)] More...
 
void clear ()
 Erase all entries. More...
 
void insert_after (Iterator i, T object)
 insert entry after iterator position More...
 
Iterator iterator ()
 get forward iterator More...
 
String toString ()
 
String toDot ()
 Get dot representation. More...
 
String toDot ()
 Get dot representation. More...
 

Protected Attributes

Node head_ =null
 

Detailed Description

Implementation of a singly linked list.

The implementation is inspired by std::forward_list (formerly extension std::slist) of the C++ standard library.

Note that we implement also operations like back and push_back and pop_back, which are O(n) and inefficient for singly linked lists. They should not be implemented (or used) for a forward iterable container! (They are incldued only to contrast Vector and DList.)

SList defines a (forward) Iterator.

  • Iterators implement the java.util.Iterator interface. Note that Iterator#hasNext checks whether the current entry is valid, and Iterator#next returns the current entry whil advancing to the subsequent entry.

    This is the standard semantic, but I still find the terms confusing.

  • 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.

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 53 of file SList.java.

Constructor & Destructor Documentation

◆ SList()

aud.SList< T >.SList ( )

create empty list

Definition at line 75 of file SList.java.

75{}

Member Function Documentation

◆ at()

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

retrieve i-th entry [O(n)]

Definition at line 132 of file SList.java.

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

References aud.SList< T >.head_.

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

+ Here is the caller graph for this function:

◆ back()

T aud.SList< T >.back ( )

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

Definition at line 121 of file SList.java.

121 {
122 check(head_);
123 Node node=head_;
124 while (node.next_!=null)
125 node=node.next_;
126 return node.data_;
127 }

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

+ Here is the caller graph for this function:

◆ clear()

void aud.SList< T >.clear ( )

Erase all entries.

[O(1)]

Definition at line 236 of file SList.java.

236{ head_=null; }

References aud.SList< T >.head_.

◆ empty()

boolean aud.SList< T >.empty ( )

determine if list is empty [O(1)]

Definition at line 94 of file SList.java.

94 {
95 return head_==null;
96 }

References aud.SList< T >.head_.

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

+ Here is the caller graph for this function:

◆ erase()

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

erase entry at position i [O(n)]

Definition at line 218 of file SList.java.

218 {
219 if (i==0)
220 head_=check(head_).next_;
221 else {
222 Node node=head_;
223 for (int j=0;j<i-1 && node!=null;++j)
224 node=node.next_;
225 check(node);
226 check(node.next_);
227
228 node.next_=node.next_.next_;
229 }
230 }

References aud.SList< T >.head_.

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

+ Here is the caller graph for this function:

◆ front()

T aud.SList< T >.front ( )

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

Definition at line 113 of file SList.java.

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

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

+ Here is the caller graph for this function:

◆ insert()

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

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

Definition at line 179 of file SList.java.

179 {
180 if (i==0)
181 insert_after((Node) null,obj);
182 else {
183 Node node=head_;
184 for (int j=0;j<i-1 && node!=null;++j)
185 node=node.next_;
186 insert_after(check(node),obj);
187 }
188 }

Referenced by aud.test.SListTest.testList().

+ Here is the caller graph for this function:

◆ insert_after()

void aud.SList< T >.insert_after ( Iterator  i,
object 
)

insert entry after iterator position

Definition at line 287 of file SList.java.

287 {
288 check(i.node_);
289 insert_after(i.node_,object);
290 }

◆ iterator()

Iterator aud.SList< T >.iterator ( )

get forward iterator

Definition at line 295 of file SList.java.

295 {
296 return new Iterator(head_);
297 }

References aud.SList< T >.head_.

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

+ Here is the caller graph for this function:

◆ pop_back()

void aud.SList< T >.pop_back ( )

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

Definition at line 202 of file SList.java.

202 {
203 Node node=check(head_), prv=null;
204 while (node.next_!=null) {
205 prv=node;
206 node=node.next_;
207 }
208 if (prv==null)
209 head_=null;
210 else
211 prv.next_=null;
212 }

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

+ Here is the caller graph for this function:

◆ pop_front()

void aud.SList< T >.pop_front ( )

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

Definition at line 194 of file SList.java.

194 {
195 head_=check(head_).next_;
196 }

References aud.SList< T >.head_.

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

+ Here is the caller graph for this function:

◆ push_back()

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

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

Definition at line 151 of file SList.java.

151 {
152 if (head_==null) // special case!
153 head_=new Node(obj,null);
154 else {
155 Node node=head_;
156 while (node.next_!=null)
157 node=node.next_;
158 node.next_=new Node(obj,null);
159 }
160 }

References aud.SList< T >.head_.

Referenced by aud.test.SListTest.testIterators(), and aud.test.SListTest.testList().

+ Here is the caller graph for this function:

◆ push_front()

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

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

Definition at line 143 of file SList.java.

143 {
144 head_=new Node(obj,head_);
145 }

References aud.SList< T >.head_.

Referenced by aud.test.SListTest.testEntries(), aud.test.SListTest.testInvalid_at(), aud.test.SListTest.testInvalid_erase(), aud.test.SListTest.testList(), and aud.test.SListTest.testSize().

+ Here is the caller graph for this function:

◆ size()

int aud.SList< T >.size ( )

determine number of entries [O(n)]

Definition at line 81 of file SList.java.

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

References aud.SList< T >.head_.

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

+ Here is the caller graph for this function:

◆ toDot()

String aud.SList< T >.toDot ( )

Get dot representation.

Implements aud.util.Graphvizable.

Definition at line 319 of file SList.java.

319 {
320 String rv="digraph SList {\n\t\n\tnode [shape=box];\n\t";
321 Node node=head_;
322
323 while (node!=null) {
324 String nxt=node.next_!=null ? node.next_.data_.toString() : "null";
325 rv+="\""+node.data_.toString()+"\"";
326 rv+=" -> \""+nxt+ "\" [color=blue,label=next];\n\t";
327 node=node.next_;
328 }
329 rv+="\n}\n";
330 return rv;
331 }

References aud.SList< T >.head_.

◆ toString()

String aud.SList< T >.toString ( )

Definition at line 305 of file SList.java.

305 {
306 String rv="[";
307 Node node=head_;
308 while (node!=null) {
309 rv+=node.data_.toString();
310 if (node.next_!=null)
311 rv+=",";
312 node=node.next_;
313 }
314 rv+="]";
315 return rv;
316 }

References aud.SList< T >.head_.

Member Data Documentation

◆ head_


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