1 11 12 package org.jivesoftware.util; 13 14 15 22 public class LinkedList { 23 24 28 private LinkedListNode head = new LinkedListNode("head", null, null); 29 30 33 public LinkedList() { 34 head.next = head.previous = head; 35 } 36 37 42 public LinkedListNode getFirst() { 43 LinkedListNode node = head.next; 44 if (node == head) { 45 return null; 46 } 47 return node; 48 } 49 50 55 public LinkedListNode getLast() { 56 LinkedListNode node = head.previous; 57 if (node == head) { 58 return null; 59 } 60 return node; 61 } 62 63 68 public LinkedListNode addFirst(LinkedListNode node) { 69 node.next = head.next; 70 node.previous = head; 71 node.previous.next = node; 72 node.next.previous = node; 73 return node; 74 } 75 76 83 public LinkedListNode addFirst(Object object) { 84 LinkedListNode node = new LinkedListNode(object, head.next, head); 85 node.previous.next = node; 86 node.next.previous = node; 87 return node; 88 } 89 90 97 public LinkedListNode addLast(Object object) { 98 LinkedListNode node = new LinkedListNode(object, head, head.previous); 99 node.previous.next = node; 100 node.next.previous = node; 101 return node; 102 } 103 104 107 public void clear() { 108 LinkedListNode node = getLast(); 110 while (node != null) { 111 node.remove(); 112 node = getLast(); 113 } 114 115 head.next = head.previous = head; 117 } 118 119 125 public String toString() { 126 LinkedListNode node = head.next; 127 StringBuilder buf = new StringBuilder (); 128 while (node != head) { 129 buf.append(node.toString()).append(", "); 130 node = node.next; 131 } 132 return buf.toString(); 133 } 134 } 135 | Popular Tags |