diff --git a/src/DIntNode.java b/src/DIntNode.java index 0830b4a..282d38e 100644 --- a/src/DIntNode.java +++ b/src/DIntNode.java @@ -1,156 +1,156 @@ - - - -/****************************************************************************** -* DIntNode.java -* -* A DIntNode provides a node for a doubly linked list with -* integer data in each node. -* -* Lists of nodes can be made of any length, limited only by the amount of -* free memory in the heap. But beyond Integer.MAX_VALUE -* -* @author Huiping Cao -* -* -******************************************************************************/ -public class DIntNode -{ - private int data; //The integer value for the node - private DIntNode next = null; //The next node of the current one - //For the last node of a list, this is either null - //or is a reference to the dummy tail node - private DIntNode prev = null; //The previous node of the current one - //For the first node of a list, this is either null - //or is a reference to the dummy head node - - /** - * No argument constructor - */ - public DIntNode(){;} - - /** - * Initialize a node with a specified initial data - * @param initialData: the initial data of this new node - * @postcondition This node contains the specified data; - * The prev and next are all null references. - **/ - public DIntNode(int initialData) - { - data = initialData; - } - - /** - * Initialize a node with a specified initial data and link to the next and prev - * nodes. Note that the initialNext and initialPrev may be null, - * which indicates that the new node has nothing before and/or after it. - * @param initialData: the initial data of this new node - * @param initialPrev: the previous node reference of this node - * @param initialNext: the next node reference of this node - **/ - public DIntNode(int initialData,DIntNode initialPrev,DIntNode initialNext) - { - data = initialData; - prev = initialPrev; - next = initialNext; - } - - /** - * toString method convert this node's information to a String - */ - public String toString() - { - String str=""; - - //include previous node's information - if(prev==null) str+="(null)"; - else str += "("+prev.data+")"; - - //include the current data - str += data; - - //include the next node's information - if(next==null) str+="(null)"; - else str += "("+next.data+")"; - - return str; - } - - /** - * Accessor method to get data from this node. - * @param - none - * @return the data from this node - */ - public int getData( ) - { - return data; - } - - - /** - * Accessor method to get a reference to the next node after this node. - * @param - none - * @return a reference to the node after this node (or the null reference if there - * is nothing after this node) - **/ - public DIntNode getNext( ) - { - return next; - } - - /** - * Accessor method to get a reference to the previous node before this node. - * @param - none - * @return a reference to the node before this node (or the null reference if there - * is nothing before this node) - **/ - public DIntNode getPrev( ) - { - return prev; - } - - - /** - * Modification method to set the data in this node. - * @param newData: the new data to place in this node - * @postcondition The data of this node has been set to newData. - **/ - public void setData(int newData) - { - data = newData; - } - - - /** - * Modification method to set the next link to newnext. - * @param newnext: a reference to the node that should appear after this node in the linked - * list (or the null reference if there is no node after this node) - * @postcondition The link to the node after this node has been set to newnext. - **/ - public void setNext(DIntNode newnext) - { - next = newnext; - } - - /** - * Modification method to set the previous link to newprev. - * @param newprev a reference to the node that should appear before this node in the linked - * list (or the null reference if there is no node before this node) - * @postcondition The link to the node before this node has been set to newprev. - */ - public void setPrev(DIntNode newprev) - { - prev = newprev; - } - - /** - * This method increase the value of data by doubling its value - * This can be used for testing the clone method - */ - public void dataDouble() - { - data = 2*data; - } - -} - + + + +/****************************************************************************** +* DIntNode.java +* +* A DIntNode provides a node for a doubly linked list with +* integer data in each node. +* +* Lists of nodes can be made of any length, limited only by the amount of +* free memory in the heap. But beyond Integer.MAX_VALUE +* +* @author Huiping Cao +* +* +******************************************************************************/ +public class DIntNode +{ + private int data; //The integer value for the node + private DIntNode next = null; //The next node of the current one + //For the last node of a list, this is either null + //or is a reference to the dummy tail node + private DIntNode prev = null; //The previous node of the current one + //For the first node of a list, this is either null + //or is a reference to the dummy head node + + /** + * No argument constructor + */ + public DIntNode(){;} + + /** + * Initialize a node with a specified initial data + * @param initialData: the initial data of this new node + * @postcondition This node contains the specified data; + * The prev and next are all null references. + **/ + public DIntNode(int initialData) + { + data = initialData; + } + + /** + * Initialize a node with a specified initial data and link to the next and prev + * nodes. Note that the initialNext and initialPrev may be null, + * which indicates that the new node has nothing before and/or after it. + * @param initialData: the initial data of this new node + * @param initialPrev: the previous node reference of this node + * @param initialNext: the next node reference of this node + **/ + public DIntNode(int initialData,DIntNode initialPrev,DIntNode initialNext) + { + data = initialData; + prev = initialPrev; + next = initialNext; + } + + /** + * toString method convert this node's information to a String + */ + public String toString() + { + String str=""; + + //include previous node's information + if(prev==null) str+="(null)"; + else str += "("+prev.data+")"; + + //include the current data + str += data; + + //include the next node's information + if(next==null) str+="(null)"; + else str += "("+next.data+")"; + + return str; + } + + /** + * Accessor method to get data from this node. + * @param - none + * @return the data from this node + */ + public int getData( ) + { + return data; + } + + + /** + * Accessor method to get a reference to the next node after this node. + * @param - none + * @return a reference to the node after this node (or the null reference if there + * is nothing after this node) + **/ + public DIntNode getNext( ) + { + return next; + } + + /** + * Accessor method to get a reference to the previous node before this node. + * @param - none + * @return a reference to the node before this node (or the null reference if there + * is nothing before this node) + **/ + public DIntNode getPrev( ) + { + return prev; + } + + + /** + * Modification method to set the data in this node. + * @param newData: the new data to place in this node + * @postcondition The data of this node has been set to newData. + **/ + public void setData(int newData) + { + data = newData; + } + + + /** + * Modification method to set the next link to newnext. + * @param newnext: a reference to the node that should appear after this node in the linked + * list (or the null reference if there is no node after this node) + * @postcondition The link to the node after this node has been set to newnext. + **/ + public void setNext(DIntNode newnext) + { + next = newnext; + } + + /** + * Modification method to set the previous link to newprev. + * @param newprev a reference to the node that should appear before this node in the linked + * list (or the null reference if there is no node before this node) + * @postcondition The link to the node before this node has been set to newprev. + */ + public void setPrev(DIntNode newprev) + { + prev = newprev; + } + + /** + * This method increase the value of data by doubling its value + * This can be used for testing the clone method + */ + public void dataDouble() + { + data = 2*data; + } + +} + diff --git a/src/DoublyLinkedListDummy.java b/src/DoublyLinkedListDummy.java index 25f43e4..914b7f6 100644 --- a/src/DoublyLinkedListDummy.java +++ b/src/DoublyLinkedListDummy.java @@ -2,12 +2,22 @@ public class DoublyLinkedListDummy { private DIntNode head; private DIntNode tail; + /** + * this is used to keep track of the status of the list's emptiness. + */ + private boolean empty; + /** + * No-arg constructor + * sets the head's next to tail and the tail's prev to head + * Initializes empty as true. + */ public DoublyLinkedListDummy() { - head = new DIntNode(); - tail = new DIntNode(); - head.setNext(tail); - tail.setPrev(head); + this.head = new DIntNode(); + this.tail = new DIntNode(); + this.head.setNext(tail); + this.tail.setPrev(head); + this.empty = true; } /** @@ -15,49 +25,77 @@ public class DoublyLinkedListDummy { * * @param element The data to add. *

- * Time Complexity: O(1) + * Time Complexity(worst): O(1) */ public void addEnd(int element) { - DIntNode newNode = new DIntNode(element, null, null); - newNode.setPrev(this.getTail()); - newNode.setNext(null); - this.getTail().setNext(newNode); - this.setTail(newNode); + // new node with element and the previous is the last item + // the next is the tail + DIntNode newNode = new DIntNode(element, getTail().getPrev(), + getTail()); + + // set the tail's prev's next to the newNode + getTail().getPrev().setNext(newNode); + + // set the tail's prev to the newnode + getTail().setPrev(newNode); + + setNotEmpty(); } /** * Removes the first node in this list. (The head) - * Time Complexity: 0(1) + * Time Complexity(worst): 0(1) */ public void removeFromHead() { - if (head.getNext() == null) { + + // if it's empty return + if (checkEmpty()) { return; } - if (tail.getPrev() == null) { - return; - } - this.setHead(this.getHead().getNext()); - this.getHead().setPrev(null); + + // set the head's next to it's link's link. + getHead().setNext(getHead().getNext().getNext()); + + // set the head's next's prev to the head. + getHead().getNext().setPrev(getHead()); + + // check if empty now + checkEmpty(); } - + /** + * to string which accounts for empty lists. + * + * @return a string of the list. + * Time Complexity(worst): O(n) where n is the size of the list we + * are converting to a string. + */ @Override public String toString() { + if (checkEmpty()) { + return "EMPTY LIST"; + } StringBuilder forward = new StringBuilder(""); StringBuilder backward = new StringBuilder(""); - for (DIntNode cursor = this.getHead(); cursor != null; cursor + for (DIntNode cursor = getHead().getNext(); cursor != getTail(); cursor = cursor.getNext()) { forward.append(cursor.getData()); - forward.append("<->"); + if (cursor.getNext() != getTail()) { + forward.append("<->"); + } } - for (DIntNode cursor = this.getTail(); cursor != null; cursor + + + for (DIntNode cursor = getTail().getPrev(); cursor != getHead(); cursor = cursor.getPrev()) { backward.append(cursor.getData()); - backward.append("<->"); + if (cursor.getPrev() != getHead()) { + backward.append("<->"); + } } - return "DoublyLinkedListDummy: " + - "(forward)" + forward.toString() + - ",(backward)" + backward.toString(); + return "DoublyLinkedListDummy: " + "{" + "\n" + + "(forward) " + forward.toString() + "\n" + + "(backward)" + backward.toString() + "\n" + "}"; } /** @@ -70,12 +108,15 @@ public class DoublyLinkedListDummy { * base the time complexity on is the size of the list in the object * it's being called from. *

- * Time Complexity: O(n) + * Time Complexity(worst): O(n) where n is the size of the list. */ public int countOccurrence(int e) { + if (checkEmpty()) { + return 0; + } int count = 0; - DIntNode cursor = this.head; - while (cursor != null) { + DIntNode cursor = getHead().getNext(); + while (cursor != getTail()) { if (cursor.getData() == e) { count++; } @@ -89,13 +130,16 @@ public class DoublyLinkedListDummy { * * @param e an integer to remove from the list. * @return true or false if one was found and removed. - * Time Complexity: O(n) where n is the size of the array we are + * Time Complexity(worst): O(n) where n is the size of the list we are * searching in. */ public boolean removeAll(int e) { - DIntNode cursor = this.head; + if (checkEmpty()) { + return false; + } + DIntNode cursor = getHead().getNext(); boolean result = false; - while (cursor.getNext() != null) { + while (cursor.getNext() != getTail()) { if (cursor.getData() == e) { cursor.getPrev().setNext(cursor.getNext()); cursor.getNext().setPrev(cursor.getPrev()); @@ -103,7 +147,9 @@ public class DoublyLinkedListDummy { } cursor = cursor.getNext(); } + checkEmpty(); return result; + } /** @@ -111,18 +157,25 @@ public class DoublyLinkedListDummy { * * @param beginIdx The starting index of the sublist * @param endIdx The ending index of the sublist - * @return a new List being a sublist of this list. - *

- * Time Complexity: O(n) where n is the number of elements from the + * @return a new List being a sublist of this list + * Time Complexity(worst): O(n) where n is the number of elements in + * the list. * start of the host list to endIdx */ public DoublyLinkedListDummy subList(int beginIdx, int endIdx) { + if (checkEmpty()) { + return this; + } + if (beginIdx <= 0 && endIdx >= listLength()) { + return this; + } + if (beginIdx < 0) { + beginIdx = 0; + } DoublyLinkedListDummy newList = new DoublyLinkedListDummy(); - DIntNode cursor = this.head; - for (int i = 0; i <= endIdx; i++) { - if (i == beginIdx) { - newList.setHead(cursor); - } else if (i > beginIdx) { + DIntNode cursor = getHead().getNext(); + for (int i = 0; i < endIdx && cursor != getTail(); i++) { + if (i >= beginIdx) { newList.addEnd(cursor.getData()); } cursor = cursor.getNext(); @@ -130,12 +183,19 @@ public class DoublyLinkedListDummy { return newList; } + /** + * Gets the size of the list + * + * @return integer of the size + * Time Complexity(worst): O(n) where n is the size of the list we + * are checking the size of. + */ public int listLength() { - if (this.head == null || this.head.getNext() == this.tail) { - return 2; + if (checkEmpty()) { + return 0; } int cnt = 2; - for (DIntNode cursor = this.head; cursor != null; cursor + for (DIntNode cursor = getHead().getNext(); cursor != getTail(); cursor = cursor.getNext()) { cnt++; } @@ -143,23 +203,34 @@ public class DoublyLinkedListDummy { } + /** + * prints a list of all distinct items in the list and their count + * Time Complexity(worst): O(n) where n is the size of the list we + * are printing. + */ public void printStatistics() { - int size = this.listLength(); - DoublyLinkedListDummy nums = new DoublyLinkedListDummy(); - - for (DIntNode cursor = this.head; cursor != null; cursor + if (checkEmpty()) { + System.out.println("EMPTY LIST"); + return; + } + DoublyLinkedListDummy items = + new DoublyLinkedListDummy(); + for (DIntNode cursor = this.getHead().getNext(); cursor != getTail(); cursor = cursor.getNext()) { - if (nums.countOccurrence(cursor.getData())<1) { - nums.addEnd(cursor.getData()); + if (items.countOccurrence(cursor.getData()) == 0) { + items.addEnd(cursor.getData()); } } - System.out.println("number occurrence"); - for (DIntNode cursor = nums.getTail(); cursor.getPrev() != null; cursor - = cursor.getPrev()) { - System.out.println( cursor.getData() + " " + this.countOccurrence(cursor.getData())); + System.out.println("Number | Occurrences"); + for (DIntNode cursor = items.getHead().getNext(); cursor != items.getTail(); cursor + = cursor.getNext()) { + System.out.print(cursor.getData() + "\t \t"); + System.out.print(this.countOccurrence(cursor.getData()) + "\n"); + } } + // getters and setters. public DIntNode getHead() { return head; } @@ -175,4 +246,32 @@ public class DoublyLinkedListDummy { public void setTail(DIntNode tail) { this.tail = tail; } + + /** + * set the list to empty + */ + public void setEmpty() { + this.empty = true; + } + + /** + * set the list to NOT empty + */ + public void setNotEmpty() { + this.empty = false; + } + + /** + * this checks if the list is empty and sets the status accordingly. + * + * @return a boolean. True if it's empty False if it's not empty. + */ + public boolean checkEmpty() { + if (getHead().getNext() == getTail() && getTail().getPrev() == getHead()) { + setEmpty(); + return true; + } + setNotEmpty(); + return false; + } } diff --git a/src/DoublyLinkedListDummyTest.java b/src/DoublyLinkedListDummyTest.java index f7bf150..54fbdfd 100644 --- a/src/DoublyLinkedListDummyTest.java +++ b/src/DoublyLinkedListDummyTest.java @@ -1,31 +1,113 @@ public class DoublyLinkedListDummyTest { public static void main(String[] args) { - System.out.println("TESTING ADD END"); + announceTest("ADD END"); testAddEnd(); - System.out.println("TESTING REMOVE FROM HEAD"); + announceTest("REMOVE FROM HEAD"); testRemoveFromHead(); + announceTest("PRINT STATISTICS"); + testPrintStatistics(); + announceTest("SUBLIST"); + testSubList(); + announceTest("REMOVEALL"); + testRemoveAll(); } + public static void announceTest(String name) { + for (int i = 0; i < 50; i++) { + System.out.print("="); + } + System.out.println(); + System.out.println("TESTING " + name); + + System.out.println(); + } public static void testAddEnd() { DoublyLinkedListDummy test = new DoublyLinkedListDummy(); for (int i = 1; i <= 10; i++) { test.addEnd(i); } - test.printStatistics(); System.out.println(test); - System.out.println(test.getHead().getNext().getNext()); + test.addEnd(20); + System.out.println(test); } public static void testRemoveFromHead() { DoublyLinkedListDummy test = new DoublyLinkedListDummy(); test.removeFromHead(); + System.out.println(test); test.removeFromHead(); - test.removeFromHead(); - test.removeFromHead(); - test.printStatistics(); + System.out.println(test); + for (int i = 1; i <= 10; i++) { + test.addEnd(i); + } + System.out.println(test); + for (int i = 0; i < 5; i++) { + test.removeFromHead(); + } System.out.println(test); + + } + public static void testPrintStatistics() { + DoublyLinkedListDummy test = new DoublyLinkedListDummy(); + test.printStatistics(); + test.printStatistics(); + for (int i = 1; i <= 10; i++) { + test.addEnd(i); + } + test.printStatistics(); + for (int i = 1; i <= 10; i++) { + test.addEnd(i); + } + test.printStatistics(); + test.addEnd(10); + test.addEnd(5); + test.addEnd(1); + test.addEnd(10); + test.printStatistics(); + for (int i = 0; i < 5; i++) { + test.removeFromHead(); + } + test.printStatistics(); + for (int i = 0; i < 20; i++) { + test.removeFromHead(); + } + test.printStatistics(); + + } + + public static void testSubList() { + DoublyLinkedListDummy test = new DoublyLinkedListDummy(); + test.subList(0, 10); + test.subList(10, 20); + System.out.println(test.subList(0, 10)); + for (int i = 1; i <= 30; i++) { + test.addEnd(i); + } + System.out.println(test.subList(0, 10)); + System.out.println(test.subList(10, 20)); + System.out.println(test.subList(0, 100)); + System.out.println(test.subList(-1, -10)); + + } + + public static void testRemoveAll() { + DoublyLinkedListDummy test = new DoublyLinkedListDummy(); + test.removeAll(0); + System.out.println(test); + for (int i = 1; i <= 10; i++) { + test.addEnd(i); + } + System.out.println(test); + test.removeAll(5); + System.out.println(test); + for (int i = 1; i <= 5; i++) { + test.addEnd(5); + } + System.out.println(test); + test.removeAll(5); + System.out.println(test); } }