AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
SList.java
Go to the documentation of this file.
1package aud;
2
3import java.util.NoSuchElementException;
4
6
52//@<slist:class
53public class SList<T> implements Iterable<T>,
55 protected class Node {
56 T data_ = null;
57 Node next_ = null;
58
59 public Node(T data,Node next) {
60 data_=data; next_=next;
61 }
62 }
63
64 protected Node head_=null;
65 //@>slist:class
66
67 //
68 // NOTE: Nested class Node and attribute head_ are defined protected
69 // (not package private) for use in an assignment.
70 //
71
74 //@<slist:ctor
75 public SList() {}
76 //@>slist:ctor
77
80 //@<slist:size
81 public int size() {
82 int n=0;
83 Node node=head_;
84 while (node!=null) {
85 node=node.next_;
86 ++n;
87 }
88 return n;
89 }
90 //@>slist:size
91
94 public boolean empty() {
95 return head_==null;
96 }
97
102 //@<slist:check
103 Node check(Node node) {
104 if (node==null)
105 throw new IndexOutOfBoundsException();
106 return node;
107 }
108 //@>slist:check
109
112 //@<slist:front
113 public T front() {
114 return check(head_).data_;
115 }
116 //@>slist:front
117
118
121 public T back() {
122 check(head_);
123 Node node=head_;
124 while (node.next_!=null)
125 node=node.next_;
126 return node.data_;
127 }
128
131 //@<slist:at
132 public T at(int i) {
133 Node node=head_;
134 for (int j=0;node!=null && j<i;++j)
135 node=node.next_;
136 return check(node).data_;
137 }
138 //@>slist:at
139
142 //@<slist:push_front
143 public void push_front(T obj) {
144 head_=new Node(obj,head_);
145 }
146 //@>slist:push_front
147
150 //@<slist:push_back
151 public void push_back(T obj) {
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 }
161 //@>slist:push_back
162
165 //@<slist:insert_after
166 void insert_after(Node node,T obj) {
167 if (node==null)
168 head_=new Node(obj,head_);
169 else {
170 Node nxt=node.next_;
171 node.next_=new Node(obj,nxt);
172 }
173 }
174 //@>slist:insert_after
175
178 //@<slist:insert
179 public void insert(int i,T obj) {
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 }
189 //@>slist:insert
190
193 //@<slist:pop_front
194 public void pop_front() {
195 head_=check(head_).next_;
196 }
197 //@>slist:pop_front
198
201 //@<slist:pop_back
202 public void pop_back() {
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 }
213 //@>slist:pop_back
214
217 //@<slist:erase
218 public void erase(int i) {
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 }
231 //@>slist:erase
232
235 //@<slist:clear
236 public void clear() { head_=null; }
237 //@>slist:clear
238
239 //
240 // iterators
241 //
242
244 //@<slist:iterator:class
245 public class Iterator implements java.util.Iterator<T> {
246
247 Node node_ = null;
248 Iterator(Node node) { node_=node; }
249 //@>slist:iterator:class
250
252 @Override
253 //@<slist:iterator:hasnext
254 public boolean hasNext() {
255 return node_!=null;
256 }
257 //@>slist:iterator:hasnext
258
260 @Override
261 //@<slist:iterator:next
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 }
269 //@>slist:iterator:next
273 @Override
274 //@<slist:iterator:remove
275 public void remove() {
276 throw new UnsupportedOperationException();
277 }
278 //@>slist:iterator:remove
279 @Override
280 @SuppressWarnings("unchecked")
281 public boolean equals(Object other) {
282 return node_==((Iterator) other).node_;
283 }
284 }
285
287 public void insert_after(Iterator i,T object) {
288 check(i.node_);
289 insert_after(i.node_,object);
290 }
291
293 @Override
294 //@<slist:iterator
296 return new Iterator(head_);
297 }
298 //@>slist:iterator
299
300 //
301 // output
302 //
303
304 @Override
305 public String toString() {
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 }
317
318 @Override
319 public String toDot() {
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 }
332}
Forward iterator.
Definition: SList.java:245
boolean equals(Object other)
Definition: SList.java:281
boolean hasNext()
return true unless "advanced" over tail
Definition: SList.java:254
T next()
return current entry and advance
Definition: SList.java:262
Node(T data, Node next)
Definition: SList.java:59
Implementation of a singly linked list.
Definition: SList.java:54
String toDot()
Get dot representation.
Definition: SList.java:319
void erase(int i)
erase entry at position i [O(n)]
Definition: SList.java:218
Node head_
Definition: SList.java:64
T front()
retrieve first entry (must exist) [O(1)]
Definition: SList.java:113
void clear()
Erase all entries.
Definition: SList.java:236
Iterator iterator()
get forward iterator
Definition: SList.java:295
T at(int i)
retrieve i-th entry [O(n)]
Definition: SList.java:132
void insert(int i, T obj)
insert new entry obj at position i [O(n)]
Definition: SList.java:179
String toString()
Definition: SList.java:305
int size()
determine number of entries [O(n)]
Definition: SList.java:81
void pop_front()
erase first entry (must exist) [O(1)]
Definition: SList.java:194
void push_front(T obj)
insert entry at front of list [O(1)]
Definition: SList.java:143
T back()
retrieve last entry (must exist) [O(n)]
Definition: SList.java:121
void insert_after(Iterator i, T object)
insert entry after iterator position
Definition: SList.java:287
void pop_back()
erase last entry (must exist) [O(n)]
Definition: SList.java:202
boolean empty()
determine if list is empty [O(1)]
Definition: SList.java:94
void push_back(T obj)
append entry obj at end of list [O(n)]
Definition: SList.java:151
SList()
create empty list
Definition: SList.java:75
Interface for GraphViz rendering.
utilities (not related to AuD lecture)
Definition: Colormap.java:1
AuD lecture: Data structures, algorithms, examples.