Java Reference
In-Depth Information
Display 15.25
A Doubly Linked List with an Iterator (part 2 of 3)
42 public void insertHere(String newData)
43 {
44 if (position == null && head != null )
45 {
46 // Add to end. First move a temp
47 // pointer to the end of the list
48 TwoWayNode temp = head;
49 while (temp.next != null )
50 temp = temp.next;
51 temp.next = new TwoWayNode(newData, temp, null );
52 }
53 else if (head == null || position.previous == null )
54 // at head of list
55 DoublyLinkedList.this.addToStart (newData);
56 else
57 {
58 // Insert before the current position
59 TwoWayNode temp = new TwoWayNode(newData,
position. previous, position);
60 position.previous.next = temp;
61 position.previous = temp;
62 }
63 }
64 public void delete( )
65 {
66 if (position == null )
67 throw new IllegalStateException( );
68 else if (position.previous == null )
69 { // Deleting first node
70 head = head.next;
71 position = head;
72 }
73 else if (position.next == null )
74 { // Deleting last node
75 position.previous.next = null ;
76 position = null ;
77 }
78 else
79 {
80 position.previous.next = position.next;
81 position.next.previous = position.previous;
82 position = position.next;
83 }
84 }
85 }
// DoublyLinkedIterator
Search WWH ::




Custom Search