|
| 1 | +/*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. |
| 2 | + * E-Mail : info@careermonk.com |
| 3 | + * Creation Date : 2015-01-10 06:15:46 |
| 4 | + * Last modification : 2006-05-31 |
| 5 | + by : Narasimha Karumanchi |
| 6 | + * File Name : DoublyLinkedList.java |
| 7 | + * Book Title : Data Structures And Algorithms Made In Java |
| 8 | + * Warranty : This software is provided "as is" without any |
| 9 | + * warranty; without even the implied warranty of |
| 10 | + * merchantability or fitness for a particular purpose. |
| 11 | + * |
| 12 | + */ |
| 13 | + |
| 14 | +package chapter3linkedlists; |
| 15 | + |
| 16 | +public class DoublyLinkedList{ |
| 17 | + // properties |
| 18 | + private DLLNode head; |
| 19 | + private DLLNode tail; |
| 20 | + private int length; |
| 21 | + |
| 22 | + // Create a new empty list. |
| 23 | + public DoublyLinkedList() { |
| 24 | + head = new DLLNode(Integer.MIN_VALUE,null,null); |
| 25 | + tail = new DLLNode(Integer.MIN_VALUE, head, null); |
| 26 | + head.setNext(tail); |
| 27 | + length = 0; |
| 28 | + } |
| 29 | + |
| 30 | + // Get the value at a given position. |
| 31 | + public int get(int position) { |
| 32 | + return Integer.MIN_VALUE; |
| 33 | + } |
| 34 | + |
| 35 | + // Find the position of the head value that is equal to a given value. |
| 36 | + // The equals method us used to determine equality. |
| 37 | + public int getPosition(int data) { |
| 38 | + // go looking for the data |
| 39 | + DLLNode temp = head; |
| 40 | + int pos = 0; |
| 41 | + while (temp != null) { |
| 42 | + if (temp.getData() == data) { |
| 43 | + // return the position if found |
| 44 | + return pos; |
| 45 | + } |
| 46 | + pos += 1; |
| 47 | + temp = temp.getNext(); |
| 48 | + } |
| 49 | + // else return -1 |
| 50 | + return Integer.MIN_VALUE; |
| 51 | + } |
| 52 | + |
| 53 | + // Return the current length of the DLL. |
| 54 | + public int length() { |
| 55 | + return length; |
| 56 | + } |
| 57 | + |
| 58 | + // Add a new value to the front of the list. |
| 59 | + public void insert(int newValue) { |
| 60 | + DLLNode newNode = new DLLNode(newValue,head,head.getNext()); |
| 61 | + newNode.getNext().setPrev(newNode); |
| 62 | + head.setNext(newNode); |
| 63 | + length += 1; |
| 64 | + } |
| 65 | + |
| 66 | + // Add a new value to the list at a given position. |
| 67 | + // All values at that position to the end move over to make room. |
| 68 | + public void insert(int data, int position) { |
| 69 | + // fix the position |
| 70 | + if (position < 0) { |
| 71 | + position = 0; |
| 72 | + } |
| 73 | + if (position > length) { |
| 74 | + position = length; |
| 75 | + } |
| 76 | + |
| 77 | + // if the list is empty, make it be the only element |
| 78 | + if (head == null) { |
| 79 | + head = new DLLNode(data); |
| 80 | + tail = head; |
| 81 | + } |
| 82 | + // if adding at the front of the list... |
| 83 | + else if (position == 0) { |
| 84 | + DLLNode temp = new DLLNode(data); |
| 85 | + temp.next = head; |
| 86 | + head = temp; |
| 87 | + } |
| 88 | + // else find the correct position and insert |
| 89 | + else { |
| 90 | + DLLNode temp = head; |
| 91 | + for (int i=1; i<position; i+=1) { |
| 92 | + temp = temp.getNext(); |
| 93 | + } |
| 94 | + DLLNode newNode = new DLLNode(data); |
| 95 | + newNode.next = temp.next; |
| 96 | + newNode.prev = temp; |
| 97 | + newNode.next.prev = newNode; |
| 98 | + temp.next = newNode; |
| 99 | + } |
| 100 | + // the list is now one value longer |
| 101 | + length += 1; |
| 102 | + } |
| 103 | + |
| 104 | + // Add a new value to the rear of the list. |
| 105 | + public void insertTail(int newValue) { |
| 106 | + DLLNode newNode = new DLLNode(newValue,tail.getPrev(),tail); |
| 107 | + newNode.getPrev().setNext(newNode); |
| 108 | + tail.setPrev(newNode); |
| 109 | + length += 1; |
| 110 | + } |
| 111 | + |
| 112 | + // Remove the value at a given position. |
| 113 | + // If the position is less than 0, remove the value at position 0. |
| 114 | + // If the position is greater than 0, remove the value at the last position. |
| 115 | + public void remove(int position) { |
| 116 | + // fix position |
| 117 | + if (position < 0) { |
| 118 | + position = 0; |
| 119 | + } |
| 120 | + |
| 121 | + if (position >= length) { |
| 122 | + position = length-1; |
| 123 | + } |
| 124 | + |
| 125 | + // if nothing in the list, do nothing |
| 126 | + if (head == null) |
| 127 | + return; |
| 128 | + |
| 129 | + // if removing the head element... |
| 130 | + if (position == 0) { |
| 131 | + head = head.getNext(); |
| 132 | + if (head == null) |
| 133 | + tail = null; |
| 134 | + } |
| 135 | + // else advance to the correct position and remove |
| 136 | + else { |
| 137 | + DLLNode temp = head; |
| 138 | + for (int i=1; i<position; i+=1) { |
| 139 | + temp = temp.getNext(); |
| 140 | + } |
| 141 | + temp.getNext().setPrev(temp.getPrev()); |
| 142 | + temp.getPrev().setNext(temp.getNext()); |
| 143 | + } |
| 144 | + // reduce the length of the list |
| 145 | + length -= 1; |
| 146 | + } |
| 147 | + |
| 148 | + // Remove a node matching the specified node from the list. |
| 149 | + // Use equals() instead of == to test for a matched node. |
| 150 | + public synchronized void removeMatched(DLLNode node) { |
| 151 | + if (head == null) |
| 152 | + return; |
| 153 | + if (node.equals(head)) { |
| 154 | + head = head.getNext(); |
| 155 | + if (head == null) |
| 156 | + tail = null; |
| 157 | + return; |
| 158 | + } |
| 159 | + DLLNode p = head; |
| 160 | + while(p != null) { |
| 161 | + if (node.equals(p)) { |
| 162 | + p.prev.next = p.next; |
| 163 | + p.next.prev = p.prev; |
| 164 | + return; |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // Remove the head value from the list. If the list is empty, do nothing. |
| 170 | + public int removeHead() { |
| 171 | + if (length == 0) |
| 172 | + return Integer.MIN_VALUE; |
| 173 | + DLLNode save = head.getNext(); |
| 174 | + head.setNext(save.getNext()); |
| 175 | + save.getNext().setPrev(head); |
| 176 | + length -= 1; |
| 177 | + return save.getData(); |
| 178 | + } |
| 179 | + |
| 180 | + // Remove the tail value from the list. If the list is empty, do nothing. |
| 181 | + public int removeTail() { |
| 182 | + if (length == 0) |
| 183 | + return Integer.MIN_VALUE; |
| 184 | + DLLNode save = tail.getPrev(); |
| 185 | + tail.setPrev(save.getPrev()); |
| 186 | + save.getPrev().setNext(tail); |
| 187 | + length -= 1; |
| 188 | + return save.getData(); |
| 189 | + } |
| 190 | + |
| 191 | + // Return a string representation of this collection, in the form: ["str1","str2",...]. |
| 192 | + public String toString() { |
| 193 | + String result = "[]"; |
| 194 | + if (length == 0) |
| 195 | + return result; |
| 196 | + result = "[" + head.getNext().getData(); |
| 197 | + DLLNode temp = head.getNext().getNext(); |
| 198 | + while (temp != tail) { |
| 199 | + result += "," + temp.getData(); |
| 200 | + temp = temp.getNext(); |
| 201 | + } |
| 202 | + return result + "]"; |
| 203 | + } |
| 204 | + |
| 205 | + // Remove everything from the DLL. |
| 206 | + public void clearList(){ |
| 207 | + head = null; |
| 208 | + tail = null; |
| 209 | + length = 0; |
| 210 | + } |
| 211 | +} |
0 commit comments