public LinkedList append(LinkedList r) { LinkedList p = this; r.next = null; while (p.next != null) p = p.next; p.next = r; return (r.prev = p); }
public void eraseList() { LinkedList t; LinkedList f = first(); t = f.next; f.next = null; // remove references while (t != null) { f = t.next; t.prev = null; t.next = null; t = f; } ; }
public LinkedList prepend(LinkedList r) { r.prev = this.prev; r.next = this; if (this.prev != null) this.prev.next = r; this.prev = r; return r; }
public LinkedList insert(LinkedList r) { r.prev = this; r.next = this.next; if (this.next != null) this.next.prev = r; this.next = r; return r; }
public static LinkedList reverseQueue(LinkedList head) { LinkedList current = head; if (current == null) { return null; } LinkedList next = current.next; if (next == null) { head = current; return head; } head = reverseQueue(next); next.next = current; current.prev = next; next.prev = null; current.next = null; return head; }
public LinkedList detach() { // returns new head relative to this node LinkedList p = prev, n = next; if (this == first()) { if (n != null) n.prev = null; next = null; return n; } if (this == last()) { if (p != null) p.next = null; prev = null; return p; } prev = null; next = null; // if (p == null && n == null) return null; if (p != null) p.next = n; if (n != null) n.prev = p; if (p == null) return n; else return p.first(); }
public static void main(String[] args) { LinkedList p = null, q = null, head = null; for (int i = 1; i <= 10; i++) { q = new LinkedList(); q.data = i; q.next = null; if (head == null) { head = q; p = q; } else { p.next = q; p = q; } } displayQueue(head); head = reverseQueue(head); displayQueue(head); }
public LinkedList cloneList() { LinkedList t; LinkedList f = first(); LinkedList k = (LinkedList) f.clone(); LinkedList nf = k; while (f.hasMoreElements() != false) { f = (LinkedList) f.nextElement(); t = (LinkedList) f.clone(); t.prev = k; k.next = t; k = t; } ; return nf; }
public LinkedList allBut(Object key) { LinkedList t; LinkedList f = first(); if (f.value.equals(key)) if (f.next != null) f = f.next; else return null; LinkedList k = (LinkedList) f.clone(); LinkedList nf = k; while (f.hasMoreElements() != false) { f = (LinkedList) f.nextElement(); if (f.value.equals(key)) continue; t = (LinkedList) f.clone(); t.prev = k; k.next = t; k = t; } ; return nf; }