00001 package com.graphbuilder.struc; 00002 00003 public class LinkedList { 00004 00005 protected Node head = null; 00006 protected Node tail = null; 00007 protected int size = 0; 00008 00009 public LinkedList() {} 00010 00011 public static class Node { 00012 protected LinkedList list = null; 00013 protected Node next = null; 00014 protected Node prev = null; 00015 protected Object userObject = null; 00016 00017 protected Node(LinkedList list, Object userObject) { 00018 this.list = list; 00019 this.userObject = userObject; 00020 } 00021 00022 public LinkedList list() { 00023 return list; 00024 } 00025 00026 public Node next() { 00027 return next; 00028 } 00029 00030 public Node prev() { 00031 return prev; 00032 } 00033 00034 public Object getUserObject() { 00035 return userObject; 00036 } 00037 00038 public void setUserObject(Object userObject) { 00039 this.userObject = userObject; 00040 } 00041 00042 public void insertBefore(Object o) { 00043 list.insertBefore(this, o); 00044 } 00045 00046 public void insertAfter(Object o) { 00047 list.insertAfter(this, o); 00048 } 00049 00050 public void remove() { 00051 list.removeNode(this); 00052 } 00053 } 00054 00055 protected Node createNode(Object o) { 00056 return new Node(this, o); 00057 } 00058 00059 protected void insertBefore(Node n, Object o) { 00060 Node p = createNode(o); 00061 00062 if (size == 0) { 00063 head = p; 00064 tail = p; 00065 } 00066 else { 00067 if (n == head) { 00068 p.next = head; 00069 head.prev = p; 00070 head = p; 00071 } 00072 else { 00073 n.prev.next = p; 00074 p.prev = n.prev; 00075 n.prev = p; 00076 p.next = n; 00077 } 00078 } 00079 00080 size++; 00081 } 00082 00083 protected void insertAfter(Node n, Object o) { 00084 Node p = createNode(o); 00085 00086 if (size == 0) { 00087 head = p; 00088 tail = p; 00089 } 00090 else { 00091 if (n == tail) { 00092 p.prev = tail; 00093 tail.next = p; 00094 tail = p; 00095 } 00096 else { 00097 n.next.prev = p; 00098 p.next = n.next; 00099 n.next = p; 00100 p.prev = n; 00101 } 00102 } 00103 00104 size++; 00105 } 00106 00107 protected Object removeNode(Node n) { 00108 if (size == 0) 00109 return null; 00110 00111 Object o = n.userObject; 00112 00113 if (n == head) { 00114 head = head.next; 00115 00116 if (head == null) 00117 tail = null; 00118 else 00119 head.prev = null; 00120 } 00121 else if (n == tail) { 00122 tail = tail.prev; 00123 tail.next = null; 00124 } 00125 else { 00126 n.prev.next = n.next; 00127 n.next.prev = n.prev; 00128 } 00129 00130 n.list = null; 00131 size--; 00132 return o; 00133 } 00134 00135 public Node getHead() { 00136 return head; 00137 } 00138 00139 public Node getTail() { 00140 return tail; 00141 } 00142 00143 public void addToHead(Object o) { 00144 insertBefore(head, o); 00145 } 00146 00147 public void addToTail(Object o) { 00148 insertAfter(tail, o); 00149 } 00150 00151 public Object removeHead() { 00152 return removeNode(head); 00153 } 00154 00155 public Object removeTail() { 00156 return removeNode(tail); 00157 } 00158 00159 public int size() { 00160 return size; 00161 } 00162 00163 public boolean isEmpty() { 00164 return size == 0; 00165 } 00166 00167 public String toString() { 00168 StringBuffer sb = new StringBuffer(6 * size); 00169 sb.append("["); 00170 Node n = head; 00171 00172 if (n != null) { 00173 sb.append(n.userObject); 00174 n = n.next; 00175 } 00176 00177 while (n != null) { 00178 sb.append(", "); 00179 sb.append(n.userObject); 00180 n = n.next; 00181 } 00182 00183 return sb.append("]").toString(); 00184 } 00185 }