1 28 29 package HTTPClient; 30 31 32 38 39 class LinkedList 40 { 41 42 private LinkElement head = null; 43 44 45 private LinkElement tail = null; 46 47 48 53 public synchronized void addToHead(Object elem) 54 { 55 head = new LinkElement(elem, head); 56 57 if (head.next == null) 58 tail = head; 59 } 60 61 62 67 public synchronized void addToEnd(Object elem) 68 { 69 if (head == null) 70 head = tail = new LinkElement(elem, null); 71 else 72 tail = (tail.next = new LinkElement(elem, null)); 73 } 74 75 76 82 public synchronized void remove(Object elem) 83 { 84 if (head == null) return; 85 86 if (head.element == elem) 87 { 88 head = head.next; 89 return; 90 } 91 92 LinkElement curr = head; 93 while (curr.next != null) 94 { 95 if (curr.next.element == elem) 96 { 97 if (curr.next == tail) tail = curr; 98 curr.next = curr.next.next; 99 return; 100 } 101 curr = curr.next; 102 } 103 } 104 105 106 112 public synchronized Object getFirst() 113 { 114 if (head == null) return null; 115 return head.element; 116 } 117 118 119 private LinkElement next_enum = null; 120 121 127 public synchronized Object enumerate() 128 { 129 if (head == null) return null; 130 131 next_enum = head.next; 132 return head.element; 133 } 134 135 136 143 public synchronized Object next() 144 { 145 if (next_enum == null) return null; 146 147 Object elem = next_enum.element; 148 next_enum = next_enum.next; 149 150 return elem; 151 } 152 153 154 public static void main(String args[]) throws Exception 155 { 156 158 System.err.println("\n*** Linked List Tests ..."); 159 160 LinkedList list = new LinkedList(); 161 list.addToHead("One"); 162 list.addToEnd("Last"); 163 if (!list.getFirst().equals("One")) 164 throw new Exception ("First element wrong"); 165 if (!list.enumerate().equals("One")) 166 throw new Exception ("First element wrong"); 167 if (!list.next().equals("Last")) 168 throw new Exception ("Last element wrong"); 169 if (list.next() != null) 170 throw new Exception ("End of list wrong"); 171 list.remove("One"); 172 if (!list.getFirst().equals("Last")) 173 throw new Exception ("First element wrong"); 174 list.remove("Last"); 175 if (list.getFirst() != null) 176 throw new Exception ("End of list wrong"); 177 178 list = new LinkedList(); 179 list.addToEnd("Last"); 180 list.addToHead("One"); 181 if (!list.getFirst().equals("One")) 182 throw new Exception ("First element wrong"); 183 if (!list.enumerate().equals("One")) 184 throw new Exception ("First element wrong"); 185 if (!list.next().equals("Last")) 186 throw new Exception ("Last element wrong"); 187 if (list.next() != null) 188 throw new Exception ("End of list wrong"); 189 if (!list.enumerate().equals("One")) 190 throw new Exception ("First element wrong"); 191 list.remove("One"); 192 if (!list.next().equals("Last")) 193 throw new Exception ("Last element wrong"); 194 list.remove("Last"); 195 if (list.next() != null) 196 throw new Exception ("End of list wrong"); 197 198 list = new LinkedList(); 199 list.addToEnd("Last"); 200 list.addToHead("Two"); 201 list.addToHead("One"); 202 if (!list.getFirst().equals("One")) 203 throw new Exception ("First element wrong"); 204 if (!list.enumerate().equals("One")) 205 throw new Exception ("First element wrong"); 206 if (!list.next().equals("Two")) 207 throw new Exception ("Second element wrong"); 208 if (!list.next().equals("Last")) 209 throw new Exception ("Last element wrong"); 210 if (list.next() != null) 211 throw new Exception ("End of list wrong"); 212 list.remove("Last"); 213 list.remove("Two"); 214 list.remove("One"); 215 if (list.getFirst() != null) 216 throw new Exception ("Empty list wrong"); 217 218 System.err.println("\n*** Tests finished successfuly"); 219 } 220 } 221 222 223 226 class LinkElement 227 { 228 Object element; 229 LinkElement next; 230 231 LinkElement(Object elem, LinkElement next) 232 { 233 this.element = elem; 234 this.next = next; 235 } 236 } 237 238 | Popular Tags |