|
| 1 | +/** |
| 2 | + * A class that implements doubly linked list. |
| 3 | + * |
| 4 | + * @param <E> The type of the elements stored in the list |
| 5 | + */ |
| 6 | +public class DoublyLinkedList<E> { |
| 7 | + private int size; |
| 8 | + private ListNode<E> head; |
| 9 | + private ListNode<E> tail; |
| 10 | + public DoublyLinkedList(){ |
| 11 | + this.size = 0; |
| 12 | + this.head = new ListNode<E>(null); |
| 13 | + this.tail = new ListNode<E>(null); |
| 14 | + this.head.setNext(this.tail); |
| 15 | + this.tail.setPrev(this.head); |
| 16 | + } |
| 17 | + |
| 18 | + /*Implementing some List interface methods to get some idea*/ |
| 19 | + |
| 20 | + /** |
| 21 | + * @return: returning DoublyLinkedList size |
| 22 | + */ |
| 23 | + public int size() { |
| 24 | + return this.size; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * adding data to the end of the list |
| 29 | + * |
| 30 | + * @param data: data to be added into the end of list |
| 31 | + */ |
| 32 | + public boolean add(E data){ |
| 33 | + ListNode<E> node = new ListNode<E>(data); |
| 34 | + // adding the node at the end |
| 35 | + node.setNext(this.tail); |
| 36 | + node.setPrev(this.tail.getPrev()); |
| 37 | + this.tail.getPrev().setNext(node); |
| 38 | + this.tail.setPrev(node); |
| 39 | + size++; |
| 40 | + return true; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * get data from a specified index |
| 45 | + * @param index |
| 46 | + */ |
| 47 | + public E get(int index) { |
| 48 | + int counter = 0; |
| 49 | + ListNode<E> nodeCounter = this.head; |
| 50 | + while(counter <= index){ |
| 51 | + nodeCounter = nodeCounter.getNext(); |
| 52 | + counter++; |
| 53 | + } |
| 54 | + return nodeCounter.getData(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * |
| 59 | + * @param index: index position where the node to add |
| 60 | + * @param data: Data contained in that node |
| 61 | + */ |
| 62 | + public boolean add(int index, E data){ |
| 63 | + ListNode<E> node = new ListNode<E>(data); |
| 64 | + ListNode<E> nodeCounter = this.head; |
| 65 | + int counter = 0; |
| 66 | + while (counter <= index){ |
| 67 | + nodeCounter = nodeCounter.getNext(); |
| 68 | + counter++; |
| 69 | + } |
| 70 | + node.setNext(nodeCounter); |
| 71 | + node.setPrev(nodeCounter.getPrev()); |
| 72 | + node.getPrev().setNext(node); |
| 73 | + nodeCounter.setPrev(node); |
| 74 | + return true; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * |
| 80 | + * @param <E> The type of element ListNode can have |
| 81 | + */ |
| 82 | +class ListNode<E>{ |
| 83 | + private ListNode<E> prev = null; |
| 84 | + private ListNode<E> next = null; |
| 85 | + private E data; |
| 86 | + public ListNode(E theData){ |
| 87 | + this.data = theData; |
| 88 | + } |
| 89 | + |
| 90 | + public E getData() { |
| 91 | + return data; |
| 92 | + } |
| 93 | + |
| 94 | + public ListNode<E> getNext() { |
| 95 | + return next; |
| 96 | + } |
| 97 | + |
| 98 | + public ListNode<E> getPrev() { |
| 99 | + return prev; |
| 100 | + } |
| 101 | + |
| 102 | + public void setData(E data) { |
| 103 | + this.data = data; |
| 104 | + } |
| 105 | + |
| 106 | + public void setNext(ListNode<E> next) { |
| 107 | + this.next = next; |
| 108 | + } |
| 109 | + |
| 110 | + public void setPrev(ListNode<E> prev) { |
| 111 | + this.prev = prev; |
| 112 | + } |
| 113 | +} |
0 commit comments