| 1 package com.quadcap.util; 2 3 40 41 import java.io.*; 42 43 import java.util.Enumeration ; 44 56 public class DList { 57 59 int size; 60 61 63 DListItem head; 64 65 68 public DList() { 69 head = null; 70 size = 0; 71 } 72 73 79 public void resize(int newsize) { 80 while (size < newsize) addBack((Object )null); 81 try { 82 while (size > newsize) popBack(); 83 } catch (ListException e) { 84 throw new RuntimeException (e.toString()); 86 } 87 } 88 89 94 public int size() { 95 return this.size; 96 } 97 98 103 public DListItem addFront(Object obj) { 104 DListItem d = new DListItem(obj); 105 addFront(d); 106 return d; 107 } 108 109 115 public void addAfter(DListItem d, Object obj) { 116 DListItem e = new DListItem(obj); 117 e.next = d.next; 118 e.next.prev = e; 119 e.prev = d; 120 d.next = e; 121 } 122 123 129 public void addBefore(DListItem d, Object obj) { 130 DListItem e = new DListItem(obj); 131 e.next = d; 132 e.prev = d.prev; 133 e.prev.next = e; 134 d.prev = e; 135 } 136 137 141 public DListItem addBack(Object obj) { 142 DListItem d = new DListItem(obj); 143 addBack(d); 144 return d; 145 } 146 147 153 public DListItem head() throws ListException { 154 if (head == null) throw new ListException("head() of empty list"); 155 return head; 156 } 157 158 164 public DListItem tail() throws ListException { 165 if (head == null) throw new ListException("tail() of empty list"); 166 return head.prev; 167 } 168 169 174 public DListItem popFront() throws ListException { 175 if (head == null) throw new ListException("popFront() of empty list"); 176 DListItem d = head; 177 unlink(d); 178 if (d == d.next) head = null; 179 else head = d.next; 180 return d; 181 } 182 183 188 public DListItem popBack() throws ListException { 189 if (head == null) throw new ListException("popBack() of empty list"); 190 DListItem d = head.prev; 191 unlink(d); 192 if (d == d.next) head = null; 193 return d; 194 } 195 196 200 public String toString() { 201 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 202 show(new PrintStream(bos)); 203 return bos.toString(); 204 } 205 206 209 public void show(PrintWriter os, String delim) { 210 String delimiter = ""; 211 os.print("("); 212 if (head != null) { 213 boolean started = false; 214 for (DListItem d = head; !started || d != head; d = d.next) { 215 if (d.obj == null) continue; 216 started = true; 217 os.print(delimiter + d.obj); 218 delimiter = delim; 219 } 220 } 221 os.println(")"); 222 } 223 224 229 public void show(PrintStream os) { 230 show(new PrintWriter(os), ", "); 231 } 232 233 238 public void show(PrintWriter os) { 239 show(os, ", "); 240 } 241 242 246 252 public final void moveFront(DListItem d) { 253 if (d != head) { 254 d.next.prev = d.prev; 255 d.prev.next = d.next; 256 d.next = head != null ? head : d; 257 d.prev = head != null ? head.prev : d; 258 d.prev.next = d; 259 d.next.prev = d; 260 head = d; 261 } 262 } 263 264 265 final void addFront(DListItem d) { 266 addBack(d); 267 head = d; 268 } 269 270 271 final void addBack(DListItem d) { 272 size++; 273 d.next = head != null ? head : d; 274 d.prev = head != null ? head.prev : d; 275 d.prev.next = d; 276 d.next.prev = d; 277 if (head == null) head = d; 278 } 279 280 281 public final void unlink(DListItem d) { 282 if (d != null) { 283 d.next.prev = d.prev; 284 d.prev.next = d.next; 285 if (d == d.next) { 286 head = null; 287 } else if (head == d) { 288 head = d.next; 289 } 290 size--; 291 } 292 } 293 294 297 public Enumeration elements() { 298 final DList thislist = this; 299 final DListItem thisd = head; 300 301 return new Enumeration () { 302 DList dlist = thislist; 303 DListItem d = thisd; 304 boolean first = true; 305 public boolean hasMoreElements() { 306 return d != null && first || d != dlist.head; 307 } 308 public Object nextElement() { 309 Object obj = d.obj; 310 d = d.next; 311 first = false; 312 return obj; 313 } 314 }; 315 } 316 } 317 | Popular Tags |