|
| 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 : CircularLinkedList.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 | + |
| 15 | +package chapter3linkedlists; |
| 16 | + |
| 17 | +public class CircularLinkedList{ |
| 18 | + protected CLLNode tail; |
| 19 | + protected int length; |
| 20 | + |
| 21 | + // Constructs a new circular list |
| 22 | + public CircularLinkedList(){ |
| 23 | + tail = null; |
| 24 | + length = 0; |
| 25 | + } |
| 26 | + |
| 27 | + // Adds data to beginning of list. |
| 28 | + public void add(int data){ |
| 29 | + addToHead(data); |
| 30 | + } |
| 31 | + |
| 32 | + // Adds element to head of list |
| 33 | + public void addToHead(int data){ |
| 34 | + CLLNode temp = new CLLNode(data); |
| 35 | + if (tail == null) { // first data added |
| 36 | + tail = temp; |
| 37 | + tail.setNext(tail); |
| 38 | + } else { // element exists in list |
| 39 | + temp.setNext(tail.getNext()); |
| 40 | + tail.setNext(temp); |
| 41 | + } |
| 42 | + length++; |
| 43 | + } |
| 44 | + |
| 45 | + // Adds element to tail of list |
| 46 | + public void addToTail(int data){ |
| 47 | + // new entry: |
| 48 | + addToHead(data); |
| 49 | + tail = tail.getNext(); |
| 50 | + } |
| 51 | + |
| 52 | + // Returns data at head of list |
| 53 | + public int peek(){ |
| 54 | + return tail.getNext().getData(); |
| 55 | + } |
| 56 | + |
| 57 | + // Returns data at tail of list |
| 58 | + public int tailPeek(){ |
| 59 | + return tail.getData(); |
| 60 | + } |
| 61 | + |
| 62 | + // Returns and removes data from head of list |
| 63 | + public int removeFromHead(){ |
| 64 | + CLLNode temp = tail.getNext(); // ie. the head of the list |
| 65 | + if (tail == tail.getNext()) { |
| 66 | + tail = null; |
| 67 | + } else { |
| 68 | + tail.setNext(temp.getNext()); |
| 69 | + temp.setNext(null); // helps clean things up; temp is free |
| 70 | + } |
| 71 | + length--; |
| 72 | + return temp.getData(); |
| 73 | + } |
| 74 | + |
| 75 | + // Returns and removes data from tail of list |
| 76 | + public int removeFromTail(){ |
| 77 | + if (isEmpty()){ |
| 78 | + return Integer.MIN_VALUE; |
| 79 | + } |
| 80 | + CLLNode finger = tail; |
| 81 | + while (finger.getNext() != tail) { |
| 82 | + finger = finger.getNext(); |
| 83 | + } |
| 84 | + // finger now points to second-to-last data |
| 85 | + CLLNode temp = tail; |
| 86 | + if (finger == tail) { |
| 87 | + tail = null; |
| 88 | + } else { |
| 89 | + finger.setNext(tail.getNext()); |
| 90 | + tail = finger; |
| 91 | + } |
| 92 | + length--; |
| 93 | + return temp.getData(); |
| 94 | + } |
| 95 | + |
| 96 | + // Returns true if list contains data, else false |
| 97 | + public boolean contains(int data){ |
| 98 | + if (tail == null) return false; |
| 99 | + CLLNode finger; |
| 100 | + finger = tail.getNext(); |
| 101 | + while (finger != tail && (!(finger.getData() == data))){ |
| 102 | + finger = finger.getNext(); |
| 103 | + } |
| 104 | + return finger.getData() == data; |
| 105 | + } |
| 106 | + |
| 107 | + // Removes and returns element equal to data, or null |
| 108 | + public int remove(int data){ |
| 109 | + if (tail == null) return Integer.MIN_VALUE; |
| 110 | + CLLNode finger = tail.getNext(); |
| 111 | + CLLNode previous = tail; |
| 112 | + int compares; |
| 113 | + for (compares = 0; compares < length && (!(finger.getData() == data)); compares++) { |
| 114 | + previous = finger; |
| 115 | + finger = finger.getNext(); |
| 116 | + } |
| 117 | + if (finger.getData() == data) { |
| 118 | + // an example of the pigeon-hole principle |
| 119 | + if (tail == tail.getNext()) { |
| 120 | + tail = null; } |
| 121 | + else { |
| 122 | + if (finger == tail) |
| 123 | + tail = previous; |
| 124 | + previous.setNext(previous.getNext().getNext()); |
| 125 | + } |
| 126 | + // finger data free |
| 127 | + finger.setNext(null); // to keep things disconnected |
| 128 | + length--; // fewer elements |
| 129 | + return finger.getData(); |
| 130 | + } |
| 131 | + else return Integer.MIN_VALUE; |
| 132 | + } |
| 133 | + |
| 134 | + // Return the current length of the CLL. |
| 135 | + public int size(){ |
| 136 | + return length; |
| 137 | + } |
| 138 | + |
| 139 | + // Return the current length of the CLL. |
| 140 | + public int length() { |
| 141 | + return length; |
| 142 | + } |
| 143 | + |
| 144 | + // Returns true if no elements in list |
| 145 | + public boolean isEmpty(){ |
| 146 | + return tail == null; |
| 147 | + } |
| 148 | + |
| 149 | + // Remove everything from the CLL. |
| 150 | + public void clear(){ |
| 151 | + length = 0; |
| 152 | + tail = null; |
| 153 | + } |
| 154 | + |
| 155 | + // Return a string representation of this collection, in the form: ["str1","str2",...]. |
| 156 | + public String toString(){ |
| 157 | + String result = "["; |
| 158 | + if (tail == null) { |
| 159 | + return result+"]"; |
| 160 | + } |
| 161 | + result = result + tail.getData(); |
| 162 | + CLLNode temp = tail.getNext(); |
| 163 | + while (temp != tail) { |
| 164 | + result = result + "," + temp.getData(); |
| 165 | + temp = temp.getNext(); |
| 166 | + } |
| 167 | + return result + "]"; |
| 168 | + } |
| 169 | +} |
0 commit comments