 Representation  Space Analysis  Creation and Insertion  Traversal  Deletion

} // .} public Element getNext(){return next. Element next.} public Element getPrevious(){return previous. tail. } } list head tail . Element previous){ data = obj. this.next = next. previous.Representation public class DoublyLinkedList{ protected Element head. . public class Element { Object data. . } public Object getData(){return data.previous = previous. . this. //. Element next. . Element(Object obj.

. k++) list. b) tail head  Like singly link list.List Creation and Insertion  An empty doubly linked list is created as follows: DoublyLinkedList list = new DoublyLinkedList().append(new Int(k)). we can use insertAfter or InsertBefore of the Element class. once Created. elements can be inserted into the list using either the append or prepend methods for (int k = 0. k < 10.  Also if we have reference to a node (an element)..

null. tail). tail = element.Insertion at the end (append) public void append(Object obj){ Element element = new Element(obj. else { tail. Complexity is O(1) } } .next = element. if(head == null) head = tail = element.

head = element. null). head.previous = element. else { Complexity is O(1) head.Insertion at the beginning (prepend) public void prepend(Object obj){ Element element = new Element(obj. } } . if(head == null) head = tail = element.

Complexity is O(1) . this.previous.previous).previous = element. this.Insertion before an element  Inserting before the current node (this) that is neither the first nor the last node: Element element = new Element(obj. this.next = element. this.

while (e != null) { //do something e = e. } Element e = tail. } Example: Count the number of nodes in a linked list. } . } return count. starting from head.next. public int countNodes(){ int count = 0. while(e != null){ count++. traversal can be done in either direction.next.Traversal For DoublyLinked list.previous. Element e = head. Element e = head. or backward starting from tail. while (e != null) { //do something e = e. Forward. Complexity is O(n) e = e.

sum = 0.intValue(). Element e = tail. int count = 0. of nodes < "+n). } . Complexity is O(n) while(e != null && count < n){ sum += ((Integer)e. } if(count < n) throw new IllegalArgumentException(³No. return sum.data). count++. e = e. if(head == null) throw new ListEmptyException().Traversal Example: The following computes the sum of the last n nodes: public int sumLastNnodes(int n){ if(n <= 0) throw new IllegalArgumentException("Wrong: " + n).previous.

next.previous.previous.next.equals(obj))) element = element. } if(element == tail) tail = element.next.previous = element. Complexity is O(n) if(element == null) throw new IllegalArgumentException("item not found").next != null) element. while((element != null) && (!element.next.next != null) element.previous = null.previous. if(element. public void extract(Object obj){ Element element = head.next = element. if(element.Deletion  To delete an element. if(element == head) { head = element. } . }else{ element.next.data. we use either the extract method of DoublyLinkedList or that of the Element inner class.

Implement each of the following methods and state its complexity.Element inner class.Exercises  For the DoublyLinkedList class. implement each of the following methods and state its complexity. ± void insertBefore() ± void insertAfter() ± void extract()  What are the methods of DoublyLinkedList and its Element inner class are more efficient than those of MyLinkedList class? . ± ± ± ± ± String toString() Element find(Object obj) void ExtractLast() void ExtractFirst() void ExtractLastN(int n)  For the DoublyLinkedList.