3import java.util.NoSuchElementException;
53public class SList<T>
implements Iterable<T>,
60 data_=data; next_=next;
103 Node check(Node node) {
105 throw new IndexOutOfBoundsException();
114 return check(
head_).data_;
124 while (node.next_!=
null)
134 for (
int j=0;node!=
null && j<i;++j)
136 return check(node).data_;
156 while (node.next_!=
null)
158 node.next_=
new Node(obj,
null);
166 void insert_after(Node node,T obj) {
171 node.next_=
new Node(obj,nxt);
181 insert_after((
Node)
null,obj);
184 for (
int j=0;j<i-1 && node!=
null;++j)
186 insert_after(check(node),obj);
204 while (node.next_!=
null) {
223 for (
int j=0;j<i-1 && node!=
null;++j)
228 node.next_=node.next_.next_;
264 throw new NoSuchElementException();
275 public void remove() {
276 throw new UnsupportedOperationException();
280 @SuppressWarnings(
"unchecked")
282 return node_==((
Iterator) other).node_;
289 insert_after(i.node_,
object);
309 rv+=node.data_.toString();
310 if (node.next_!=
null)
320 String rv=
"digraph SList {\n\t\n\tnode [shape=box];\n\t";
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";
boolean equals(Object other)
boolean hasNext()
return true unless "advanced" over tail
T next()
return current entry and advance
Implementation of a singly linked list.
String toDot()
Get dot representation.
void erase(int i)
erase entry at position i [O(n)]
T front()
retrieve first entry (must exist) [O(1)]
void clear()
Erase all entries.
Iterator iterator()
get forward iterator
T at(int i)
retrieve i-th entry [O(n)]
void insert(int i, T obj)
insert new entry obj at position i [O(n)]
int size()
determine number of entries [O(n)]
void pop_front()
erase first entry (must exist) [O(1)]
void push_front(T obj)
insert entry at front of list [O(1)]
T back()
retrieve last entry (must exist) [O(n)]
void insert_after(Iterator i, T object)
insert entry after iterator position
void pop_back()
erase last entry (must exist) [O(n)]
boolean empty()
determine if list is empty [O(1)]
void push_back(T obj)
append entry obj at end of list [O(n)]
Interface for GraphViz rendering.
utilities (not related to AuD lecture)
AuD lecture: Data structures, algorithms, examples.