2 var <>prev, <>next, <>obj;
3 // this class supports the LinkedList class
9 if (prev.notNil, { prev.next_(next); });
10 if (next.notNil, { next.prev_(prev); });
15 LinkedList : SequenceableCollection {
16 var head, tail, <size = 0;
20 var copy = LinkedList.new;
21 this.do {|item| copy.add(item) }
24 species { ^this.class }
28 while ({ node.notNil },{
29 function.value(node.obj, i);
34 reverseDo { arg function;
37 while ({ node.notNil },{
38 function.value(node.obj, i);
44 var node = LinkedListNode.new(obj);
56 var node = LinkedListNode.new(obj);
68 var node = this.findNodeOfObj(obj);
70 if (head == node, { head = node.next; });
71 if (tail == node, { tail = node.prev; });
81 if (head == node, { head = nil; });
95 if (tail == node, { tail = nil; });
105 first { if (head.notNil, { ^head.obj },{ ^nil }) }
106 last { if (tail.notNil, { ^tail.obj },{ ^nil }) }
109 var node = this.nodeAt(index);
116 put { arg index, item;
117 var node = this.nodeAt(index);
122 removeAt { arg index;
123 var node = this.nodeAt(index);
125 if (head == node, { head = node.next; });
126 if (tail == node, { tail = node.prev; });
137 if ( index >= 0 and: { index < size }, {
138 if (index < (size - index), {
140 while ({ i != index },{
148 while ({ i != index },{
155 this.indexOutOfRange;
160 findNodeOfObj { arg obj;
162 while ({ node.notNil },{
163 if (node.obj == obj, { ^node });