1 23 24 29 30 package com.sun.jdo.spi.persistence.utility; 31 32 42 public class DoubleLinkedList 43 { 44 45 47 50 public Linkable head; 51 52 55 public Linkable tail; 56 57 60 public int size; 61 62 65 public DoubleLinkedList() 66 { 67 this.head = null; 68 this.size = 0; 69 this.tail = null; 70 } 71 72 73 75 79 public synchronized Linkable getHead() 80 { 81 return this.head; 82 } 83 84 88 public synchronized Linkable getTail() 89 { 90 return this.tail; 91 } 92 93 94 98 public synchronized int getSize() 99 { 100 return this.size; 101 } 102 103 104 108 public synchronized void insertAtHead(Linkable node) 109 { 110 if (node instanceof Linkable) 111 { 112 if (this.head == null) 113 { 114 node.setNext(null); node.setPrevious(null); this.head = node; } 118 else 119 { 120 Linkable oldHead = this.head; oldHead.setPrevious(node); node.setNext(oldHead); node.setPrevious(null); this.head = node; } 126 if (this.tail == null) { 128 this.tail = node; } 130 this.size++; 131 } 132 } 133 134 138 public synchronized void insertAtTail(Linkable node) 139 { 140 if (node instanceof Linkable) 141 { 142 if (this.tail == null) 143 { 144 node.setNext(null); node.setPrevious(null); this.tail = node; } 148 else 149 { 150 Linkable oldTail = this.tail; oldTail.setNext(node); node.setNext(null) ; node.setPrevious(oldTail); this.tail = node; } 156 if (this.head == null) { 158 this.head = node; } 160 this.size++; 161 } 162 } 163 164 165 166 170 public synchronized Linkable removeFromHead() 171 { 172 Linkable node = this.head; 173 if (node instanceof Linkable) 174 { 175 this.head = node.getNext(); if (this.head == null) { 178 this.tail = null; } 180 else 181 { 182 this.head.setPrevious(null); } 184 node.setNext(null); node.setPrevious(null); this.size--; 187 } 188 return node; 189 } 190 191 195 public synchronized Linkable removeFromTail() 196 { 197 Linkable node = this.tail; 198 if (node instanceof Linkable) 199 { 200 this.tail = node.getPrevious(); if (this.tail == null) { 203 this.head = null; } 205 else 206 { 207 this.tail.setNext(null); } 209 node.setNext(null); node.setPrevious(null); this.size--; 212 } 213 return node; 214 } 215 216 222 public synchronized void removeFromList(Linkable node) 223 { 224 if ((this.size <= 0) || ((this.head == null) && (this.tail == null))) 225 { 226 return; 227 } 228 if (node instanceof Linkable) 229 { 230 Linkable p = node.getPrevious(); Linkable n = node.getNext(); 233 if (p == null) { 235 this.head = n; } 237 else 238 { 239 p.setNext(n); } 241 242 if (n == null) { 244 this.tail = p; } 246 else 247 { 248 n.setPrevious(p); } 250 251 node.setNext(null); 252 node.setPrevious(null); 253 this.size--; 254 } 255 } 256 257 258 264 public synchronized void insertIntoList(Linkable afternode, Linkable newnode) 265 { 266 if ((newnode instanceof Linkable) && (afternode instanceof Linkable)) 267 { 268 if (this.tail == afternode) { 270 this.insertAtTail(newnode); } 272 else 273 { 274 Linkable nextnode = afternode.getNext(); 275 newnode.setNext(nextnode); newnode.setPrevious(afternode); afternode.setNext(newnode); nextnode.setPrevious(newnode); } 280 this.size++; 281 } 282 } 283 284 289 public synchronized String toString() 290 { 291 307 308 return null; 309 } 310 } 311 312 | Popular Tags |