l.len--;
}
-func (l *List) insertFront(e *Element) {
- e.prev = nil;
- e.next = l.front;
- l.front = e;
- if e.next != nil {
- e.next.prev = e;
+func (l *List) insertBefore(e *Element, mark *Element) {
+ if mark.prev == nil {
+ // new front of the list
+ l.front = e;
} else {
- l.back = e;
+ mark.prev.next = e;
}
+ e.prev = mark.prev;
+ mark.prev = e;
+ e.next = mark;
l.len++;
}
-func (l *List) insertBack(e *Element) {
- e.next = nil;
- e.prev = l.back;
- l.back = e;
- if e.prev != nil {
- e.prev.next = e;
+func (l *List) insertAfter(e *Element, mark *Element) {
+ if mark.next == nil {
+ // new back of the list
+ l.back = e;
} else {
- l.front = e;
+ mark.next.prev = e;
}
+ e.next = mark.next;
+ mark.next = e;
+ e.prev = mark;
l.len++;
}
-// PushFront inserts the value at the front of the list, and returns a new Element containing it.
+func (l *List) insertFront(e *Element) {
+ if l.front == nil {
+ // empty list
+ l.front, l.back = e, e;
+ e.prev, e.next = nil, nil;
+ l.len = 1;
+ return
+ }
+ l.insertBefore(e, l.front);
+}
+
+func (l *List) insertBack(e *Element) {
+ if l.back == nil {
+ // empty list
+ l.front, l.back = e, e;
+ e.prev, e.next = nil, nil;
+ l.len = 1;
+ return
+ }
+ l.insertAfter(e, l.back);
+}
+
+// PushFront inserts the value at the front of the list and returns a new Element containing the value.
func (l *List) PushFront(value interface {}) *Element {
if l.id == nil {
l.Init();
return e
}
-// PushBack inserts the value at the back of the list, and returns a new Element containing it.
+// PushBack inserts the value at the back of the list and returns a new Element containing the value.
func (l *List) PushBack(value interface {}) *Element {
if l.id == nil {
l.Init();
return e
}
+// InsertBefore inserts the value immediately before mark and returns a new Element containing the value.
+func (l *List) InsertBefore(value interface {}, mark *Element) *Element {
+ if mark.id != l.id {
+ return nil
+ }
+ e := &Element{ nil, nil, l.id, value };
+ l.insertBefore(e, mark);
+ return e
+}
+
+// InsertAfter inserts the value immediately after mark and returns a new Element containing the value.
+func (l *List) InsertAfter(value interface {}, mark *Element) *Element {
+ if mark.id != l.id {
+ return nil
+ }
+ e := &Element{ nil, nil, l.id, value };
+ l.insertAfter(e, mark);
+ return e
+}
+
// MoveToFront moves the element to the front of the list.
func (l *List) MoveToFront(e *Element) {
if e.id != l.id || l.front == e {
l.MoveToBack(e3); // should be no-op
checkListPointers(t, l, []*Element{ e1, e4, e3 });
+ e2 = l.InsertBefore(2, e1); // insert before front
+ checkListPointers(t, l, []*Element{ e2, e1, e4, e3 });
+ l.Remove(e2);
+ e2 = l.InsertBefore(2, e4); // insert before middle
+ checkListPointers(t, l, []*Element{ e1, e2, e4, e3 });
+ l.Remove(e2);
+ e2 = l.InsertBefore(2, e3); // insert before back
+ checkListPointers(t, l, []*Element{ e1, e4, e2, e3 });
+ l.Remove(e2);
+
+ e2 = l.InsertAfter(2, e1); // insert after front
+ checkListPointers(t, l, []*Element{ e1, e2, e4, e3 });
+ l.Remove(e2);
+ e2 = l.InsertAfter(2, e4); // insert after middle
+ checkListPointers(t, l, []*Element{ e1, e4, e2, e3 });
+ l.Remove(e2);
+ e2 = l.InsertAfter(2, e3); // insert after back
+ checkListPointers(t, l, []*Element{ e1, e4, e3, e2 });
+ l.Remove(e2);
+
// Clear all elements by iterating
for e := range l.Iter() {
l.Remove(e);