Driver - /class "Node" defines a node of the DLL...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
//class "Node" defines a node of the DLL class Node<E> { E data; Node<E> next = null; Node<E> prev = null; public Node(E dataItem) { data = dataItem; } } // the dll class public class MyDLL<E> { // the properties private Node<E> head = null; private Node<E> tail = null; private int size = 0; public int size() { return size; } public E get(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException("Invalid index " + index); Node<E> item = head; for (int i = 0; i < index; i++) item = item.next; return item.data; } public void add(int index, E obj) { if (index < 0 || index > size) throw new IndexOutOfBoundsException("Invalid index " + index); Node<E> newNode = new Node < E > (obj); if (head == null) { // Add to an empty list. head = newNode; tail = head; } else if (index == 0) { // Insert at head. newNode.next = head; /*Step 1*/ head.prev = newNode; /*Step 2*/head = newNode; // Step 3 } else if (index == size) { // Insert at tail. tail.next = newNode; /* Step 1*/ newNode.prev = tail; /* Step 2*/ tail = newNode; // Step 3 } else { // Insert into the middle. Node<E> nextItem = head; for (int i = 0; i < index; i++) nextItem = nextItem.next; newNode.prev = nextItem.prev; /* Step 1*/ newNode.next = nextItem; /* Step 3*/ nextItem.prev = newNode; /* Step 4*/ } size++; } public E remove(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException("Invalid index " + index); E retval = null; if(index == 0) { retval = head.data; head = head.next; head.prev = null; size--; return retval; } if(index == size - 1) { retval = tail.data; tail = tail.prev; tail.next = null; size--; return retval; } Node <E> item = head; for (int i = 0; i < index; i++) item = item.next; item.prev.next = item.next; item.next.prev = item.prev; size--; return item.data; } public void addFirst(E item) { add(0, item); } public void addLast(E item) { add(size, item); } public E removeFirst() { return remove(0); } public E removeLast() { return remove(size - 1); }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
public void clear() { head= null; tail= null; size = 0; } public int indexOf(Object obj) { Node<E> item = head; int index = 0; while (item !=null) { if (!item.data.equals((E)obj)){ item = item.next; index++; } else break; } if(index == size) { return -1; } return index; } public boolean remove (Object obj) { int index = this.indexOf(obj); if (index == -1){ return false; } else this.remove(index);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

Driver - /class &quot;Node&quot; defines a node of the DLL...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online