DoublyLinkedListDummy Almost done

This commit is contained in:
RustyRaptor committed 2019-10-03 21:03:14 -06:00
1 parent 3380531e56
commit 51dbf2f5fa
2 files changed
+105 -24

No files matched your search

+74 -24
View File
@@ -4,19 +4,25 @@ public class DoublyLinkedListDummy {
public DoublyLinkedListDummy() {
this.head.setNext(tail);
this.tail.setPrev(head);
head = new DIntNode();
tail = new DIntNode();
head.setNext(tail);
tail.setPrev(head);
}
/**
* Adds a Node to the end of the list. (after the tail)
* @param element The data to add.
*
* Time Complexity: O(1)
* @param element The data to add.
* <p>
* Time Complexity: O(1)
*/
public void addEnd(int element) {
this.tail = new DIntNode(element, this.tail,
this.tail.getPrev());
DIntNode newNode = new DIntNode(element, null, null);
newNode.setPrev(this.getTail());
newNode.setNext(null);
this.getTail().setNext(newNode);
this.setTail(newNode);
}
/**
@@ -24,6 +30,12 @@ public class DoublyLinkedListDummy {
* Time Complexity: 0(1)
*/
public void removeFromHead() {
if (head.getNext() == null) {
return;
}
if (tail.getPrev() == null) {
return;
}
this.setHead(this.getHead().getNext());
this.getHead().setPrev(null);
}
@@ -31,27 +43,39 @@ public class DoublyLinkedListDummy {
@Override
public String toString() {
return "DoublyLinkedListDummy{" +
"head=" + head +
", tail=" + tail +
'}';
StringBuilder forward = new StringBuilder("");
StringBuilder backward = new StringBuilder("");
for (DIntNode cursor = this.getHead(); cursor != null; cursor
= cursor.getNext()) {
forward.append(cursor.getData());
forward.append("<->");
}
for (DIntNode cursor = this.getTail(); cursor != null; cursor
= cursor.getPrev()) {
backward.append(cursor.getData());
backward.append("<->");
}
return "DoublyLinkedListDummy: " +
"(forward)" + forward.toString() +
",(backward)" + backward.toString();
}
/**
* Returns the count of a given data
*
* @param e the data we want to count occurences of.
* @return the count of the data
*
* <p>
* Note: In this case the input itself is constant. The real input to
* base the time complexity on is the size of the list in the object
* it's being called from.
*
* <p>
* Time Complexity: O(n)
*/
public int countOccurrence(int e) {
int count = 0;
DIntNode cursor = this.head;
while (cursor.getNext() != null){
while (cursor != null) {
if (cursor.getData() == e) {
count++;
}
@@ -62,6 +86,7 @@ public class DoublyLinkedListDummy {
/**
* Removes all elements that match a given integer from the list.
*
* @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
@@ -70,7 +95,7 @@ public class DoublyLinkedListDummy {
public boolean removeAll(int e) {
DIntNode cursor = this.head;
boolean result = false;
while (cursor.getNext() != null){
while (cursor.getNext() != null) {
if (cursor.getData() == e) {
cursor.getPrev().setNext(cursor.getNext());
cursor.getNext().setPrev(cursor.getPrev());
@@ -83,19 +108,19 @@ public class DoublyLinkedListDummy {
/**
* returns a sublist of the current list
* @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.
*
* @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.
* <p>
* Time Complexity: O(n) where n is the number of elements from the
* start of the host list to endIdx
*
*/
public DoublyLinkedListDummy subList(int beginIdx, int endIdx){
public DoublyLinkedListDummy subList(int beginIdx, int endIdx) {
DoublyLinkedListDummy newList = new DoublyLinkedListDummy();
DIntNode cursor = this.head;
for (int i = 0; i <= endIdx; i++){
if(i == beginIdx){
for (int i = 0; i <= endIdx; i++) {
if (i == beginIdx) {
newList.setHead(cursor);
} else if (i > beginIdx) {
newList.addEnd(cursor.getData());
@@ -105,10 +130,35 @@ public class DoublyLinkedListDummy {
return newList;
}
public int listLength() {
if (this.head == null || this.head.getNext() == this.tail) {
return 2;
}
int cnt = 2;
for (DIntNode cursor = this.head; cursor != null; cursor
= cursor.getNext()) {
cnt++;
}
return cnt;
}
// public void printStatistics() {
//
// }
public void printStatistics() {
int size = this.listLength();
DoublyLinkedListDummy nums = new DoublyLinkedListDummy();
for (DIntNode cursor = this.head; cursor != null; cursor
= cursor.getNext()) {
if (nums.countOccurrence(cursor.getData())<1) {
nums.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()));
}
}
public DIntNode getHead() {
return head;
+31
View File
@@ -0,0 +1,31 @@
public class DoublyLinkedListDummyTest {
public static void main(String[] args) {
System.out.println("TESTING ADD END");
testAddEnd();
System.out.println("TESTING REMOVE FROM HEAD");
testRemoveFromHead();
}
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());
}
public static void testRemoveFromHead() {
DoublyLinkedListDummy test = new DoublyLinkedListDummy();
test.removeFromHead();
test.removeFromHead();
test.removeFromHead();
test.removeFromHead();
test.printStatistics();
System.out.println(test);
}
}