From a2fafcef8c17670425fb0d1abf9a885dac2c2867 Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Tue, 3 Mar 2015 13:59:23 -0500 Subject: [PATCH 1/8] Completed Question 1. --- src/SortedLinkedList.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) mode change 100755 => 100644 src/SortedLinkedList.java diff --git a/src/SortedLinkedList.java b/src/SortedLinkedList.java old mode 100755 new mode 100644 index f75caa0..d424f99 --- a/src/SortedLinkedList.java +++ b/src/SortedLinkedList.java @@ -34,7 +34,7 @@ public void print() */ public String getName(int index) { - + return null; } /** @@ -44,7 +44,7 @@ public String getName(int index) */ public int getQuantity(int index) { - + return -1; } /** @@ -52,7 +52,7 @@ public int getQuantity(int index) */ public int length() { - + return 0; } /** @@ -61,7 +61,7 @@ public int length() */ public boolean isMember(String name) { - + return false; } /** From cbb4f0163eaa123f573f46dfadab3aa5eec65723 Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Tue, 3 Mar 2015 14:00:39 -0500 Subject: [PATCH 2/8] Completed Question 1, re-commit. --- src/SortedLinkedListTest.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/SortedLinkedListTest.java diff --git a/src/SortedLinkedListTest.java b/src/SortedLinkedListTest.java new file mode 100644 index 0000000..ec2430f --- /dev/null +++ b/src/SortedLinkedListTest.java @@ -0,0 +1,25 @@ +import junit.framework.TestCase; + +/** + * A JUnit test case class. + * Every method starting with the word "test" will be called when running + * the test with JUnit. + */ +public class SortedLinkedListTest extends TestCase { + + /** + * A test method. + * (Replace "X" with a name describing the test. You may write as + * many "testSomething" methods in this class as you wish, and each + * one will be called when running JUnit over this class.) + */ + public void testEmptyList() { + SortedLinkedList l = new SortedLinkedList(); + + assertEquals(null, l.getName(0)); + assertEquals(-1, l.getQuantity(0)); + assertEquals(0, l.length()); + assertFalse(l.isMember("Fred")); + } + +} From 2ff8b58a8b22791a3a8dbf9badc5912b934e9f66 Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Tue, 3 Mar 2015 14:34:36 -0500 Subject: [PATCH 3/8] Completed Question 3. --- src/SortedLinkedList.java | 22 +++++++++++++++++++++- src/SortedLinkedListTest.java | 11 +++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/SortedLinkedList.java b/src/SortedLinkedList.java index d424f99..f4905d0 100644 --- a/src/SortedLinkedList.java +++ b/src/SortedLinkedList.java @@ -34,6 +34,9 @@ public void print() */ public String getName(int index) { + if (head != null) + return head.getName(); + return null; } @@ -44,6 +47,9 @@ public String getName(int index) */ public int getQuantity(int index) { + if (head != null) + return head.getQuantity(); + return -1; } @@ -52,7 +58,14 @@ public int getQuantity(int index) */ public int length() { - return 0; + Node temp = head; + int count = 0; + + while(temp != null) { + count += 1; + temp = temp.getNext(); + } + return count; } /** @@ -61,6 +74,9 @@ public int length() */ public boolean isMember(String name) { + if ( head != null) + return true; + return false; } @@ -70,6 +86,10 @@ public boolean isMember(String name) */ public void insert(String name, int quantity) { + String nodeName = name; + int nodeQuantity = quantity; + head = new Node(nodeName, nodeQuantity); + } } diff --git a/src/SortedLinkedListTest.java b/src/SortedLinkedListTest.java index ec2430f..f27a887 100644 --- a/src/SortedLinkedListTest.java +++ b/src/SortedLinkedListTest.java @@ -22,4 +22,15 @@ public void testEmptyList() { assertFalse(l.isMember("Fred")); } + public void testSingleNode() { + SortedLinkedList l = new SortedLinkedList(); + + l.insert("Fred", 1); + + + assertEquals("Fred", l.getName(0)); + assertEquals(1, l.getQuantity(0)); + assertEquals(1, l.length()); + assertTrue(l.isMember("Fred")); + } } From c609c522f77fdf9e3b01012bd5a5d24dec5c6375 Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Tue, 3 Mar 2015 15:15:48 -0500 Subject: [PATCH 4/8] Finished Question 4. --- src/SortedLinkedList.java | 54 ++++++++++++++++++++++++++++------- src/SortedLinkedListTest.java | 22 ++++++++++++++ 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/src/SortedLinkedList.java b/src/SortedLinkedList.java index f4905d0..664597b 100644 --- a/src/SortedLinkedList.java +++ b/src/SortedLinkedList.java @@ -34,10 +34,17 @@ public void print() */ public String getName(int index) { - if (head != null) - return head.getName(); - - return null; + //Make sure there is something in the list + if (head == null) + return null; + + int i = 0; + Node temp = head; + while(i < index) { + temp = temp.getNext(); + i++; + } + return temp.getName(); } /** @@ -47,10 +54,17 @@ public String getName(int index) */ public int getQuantity(int index) { - if (head != null) - return head.getQuantity(); + //Make sure there is something in the list + if (head == null) + return -1; - return -1; + int i = 0; + Node temp = head; + while(i < index) { + temp = temp.getNext(); + i++; + } + return temp.getQuantity(); } /** @@ -74,9 +88,16 @@ public int length() */ public boolean isMember(String name) { - if ( head != null) - return true; + Node temp = head; + while(temp != null) { + if (temp.getName() == name) + return true; + + temp = temp.getNext(); + } + + //All-else, return false return false; } @@ -89,7 +110,18 @@ public void insert(String name, int quantity) String nodeName = name; int nodeQuantity = quantity; - head = new Node(nodeName, nodeQuantity); - + if ( head == null) + head = new Node(nodeName, nodeQuantity); + + else { + //Create a new node that will be the head + Node newHead = new Node(nodeName, nodeQuantity); + + //link the newHead to the old head... + newHead.setNext(head); + + //Change head to reference the newHead + head = newHead; + } } } diff --git a/src/SortedLinkedListTest.java b/src/SortedLinkedListTest.java index f27a887..2796610 100644 --- a/src/SortedLinkedListTest.java +++ b/src/SortedLinkedListTest.java @@ -33,4 +33,26 @@ public void testSingleNode() { assertEquals(1, l.length()); assertTrue(l.isMember("Fred")); } + + + public void testSecondNodeNewHead() { + SortedLinkedList l = new SortedLinkedList(); + + l.insert("Fred", 1); + l.insert("Cats", 5); + + //Index 0, the cats. + assertEquals("Cats", l.getName(0)); + assertEquals(5, l.getQuantity(0)); + assertTrue(l.isMember("Cats")); + + //Index 1, Fred + assertEquals("Fred", l.getName(1)); + assertEquals(1, l.getQuantity(1)); + assertTrue(l.isMember("Fred")); + + //Length == 2 + assertEquals(2, l.length()); + + } } From 713c613c00d4a280fedfed2915b8650a727e2bca Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Tue, 3 Mar 2015 15:41:46 -0500 Subject: [PATCH 5/8] End of Lab on 3 March 2015. --- src/SortedLinkedListTest.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/SortedLinkedListTest.java b/src/SortedLinkedListTest.java index 2796610..8d978d8 100644 --- a/src/SortedLinkedListTest.java +++ b/src/SortedLinkedListTest.java @@ -55,4 +55,30 @@ public void testSecondNodeNewHead() { assertEquals(2, l.length()); } + + public void testRepeatedHeadInsert() { + SortedLinkedList l = new SortedLinkedList(); + + l.insert("Fred", 1); + l.insert("Cats", 5); + l.insert("Spaghetti", 58); + + //Index 0, the spaghetti + assertEquals("Spaghetti", l.getName(0)); + assertEquals(58, l.getQuantity(0)); + assertTrue(l.isMember("Spaghetti")); + + //Index 1, the cats. + assertEquals("Cats", l.getName(1)); + assertEquals(5, l.getQuantity(1)); + assertTrue(l.isMember("Cats")); + + //Index 2, Fred + assertEquals("Fred", l.getName(2)); + assertEquals(1, l.getQuantity(2)); + assertTrue(l.isMember("Fred")); + + //Length == 3 + assertEquals(3, l.length()); + } } From 8dc5d1cc6fca4e602bd94b11c70f41b957a0025b Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Tue, 17 Mar 2015 14:22:08 -0400 Subject: [PATCH 6/8] Finished Question 5. --- src/SortedLinkedList.java | 41 +++++++++++++++++++++++++++++------ src/SortedLinkedListTest.java | 20 +++++++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/SortedLinkedList.java b/src/SortedLinkedList.java index 664597b..da8c43a 100644 --- a/src/SortedLinkedList.java +++ b/src/SortedLinkedList.java @@ -107,21 +107,48 @@ public boolean isMember(String name) */ public void insert(String name, int quantity) { + //Localizations of the parameters to make the new node later in the method String nodeName = name; int nodeQuantity = quantity; + //Check if a headnode exists, if not, make it. if ( head == null) head = new Node(nodeName, nodeQuantity); + //Otherwise, iterate through the list to find the appropriate position for insertion. else { - //Create a new node that will be the head - Node newHead = new Node(nodeName, nodeQuantity); + Node curr = head; + Node prev = null; + Boolean newNodeMade = false; + + while(curr != null && newNodeMade != true) { + //Check if the new node quantity is bigger than the current position + if ( nodeQuantity >= curr.getQuantity() ) { + Node newNode = new Node(nodeName, nodeQuantity); + newNodeMade = true; + + // If curr is the head node. + if ( prev == null) { + newNode.setNext(curr); + head = newNode; + } + // Otherwise, if curr is not the head node + else { + newNode.setNext(curr); + prev.setNext(newNode); + } + } - //link the newHead to the old head... - newHead.setNext(head); - - //Change head to reference the newHead - head = newHead; + // Go to the next node + prev = curr; + curr = curr.getNext(); + } + + //A catch, when we reach the end of the list and the new node was not inserted in the list. + if (curr == null && newNodeMade == false ) { + Node newNode = new Node(nodeName, nodeQuantity); + prev.setNext(newNode); + } } } } diff --git a/src/SortedLinkedListTest.java b/src/SortedLinkedListTest.java index 8d978d8..acf6ac3 100644 --- a/src/SortedLinkedListTest.java +++ b/src/SortedLinkedListTest.java @@ -81,4 +81,24 @@ public void testRepeatedHeadInsert() { //Length == 3 assertEquals(3, l.length()); } + + public void testSecondTail() { + SortedLinkedList l = new SortedLinkedList(); + + l.insert("Cats", 5); + l.insert("Fred", 1); + + //Index 0, the cats. + assertEquals("Cats", l.getName(0)); + assertEquals(5, l.getQuantity(0)); + assertTrue(l.isMember("Cats")); + + //Index 1, Fred + assertEquals("Fred", l.getName(1)); + assertEquals(1, l.getQuantity(1)); + assertTrue(l.isMember("Fred")); + + //Length == 2 + assertEquals(2, l.length()); + } } From 8b537884fff457c6e38aa65b9ea4b50e7ef7536d Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Tue, 17 Mar 2015 14:29:42 -0400 Subject: [PATCH 7/8] Finished Question 6. --- src/SortedLinkedListTest.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/SortedLinkedListTest.java b/src/SortedLinkedListTest.java index acf6ac3..173f597 100644 --- a/src/SortedLinkedListTest.java +++ b/src/SortedLinkedListTest.java @@ -101,4 +101,30 @@ public void testSecondTail() { //Length == 2 assertEquals(2, l.length()); } + + public void testMiddleThird() { + SortedLinkedList l = new SortedLinkedList(); + + l.insert("Spaghetti", 58); + l.insert("Fred", 1); + l.insert("Cats", 5); + + //Index 0, the spaghetti + assertEquals("Spaghetti", l.getName(0)); + assertEquals(58, l.getQuantity(0)); + assertTrue(l.isMember("Spaghetti")); + + //Index 1, the cats. + assertEquals("Cats", l.getName(1)); + assertEquals(5, l.getQuantity(1)); + assertTrue(l.isMember("Cats")); + + //Index 2, Fred + assertEquals("Fred", l.getName(2)); + assertEquals(1, l.getQuantity(2)); + assertTrue(l.isMember("Fred")); + + //Length == 3 + assertEquals(3, l.length()); + } } From 5866d69961e3c810207624896f54f7967c42b391 Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Tue, 17 Mar 2015 14:42:44 -0400 Subject: [PATCH 8/8] Finished Question 7. --- src/SortedLinkedListTest.java | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/SortedLinkedListTest.java b/src/SortedLinkedListTest.java index 173f597..cf483b1 100644 --- a/src/SortedLinkedListTest.java +++ b/src/SortedLinkedListTest.java @@ -127,4 +127,48 @@ public void testMiddleThird() { //Length == 3 assertEquals(3, l.length()); } + + public void testFinaltest() { + SortedLinkedList l = new SortedLinkedList(); + + l.insert("Dogs", 10); + l.insert("Lucky Charms", 777); + l.insert("Cats", 5); + l.insert("Spaghetti", 58); + l.insert("Pineapples", 50); + l.insert("Fred", 1); + + //Index 0, the Lucky Charms + assertEquals("Lucky Charms", l.getName(0)); + assertEquals(777, l.getQuantity(0)); + assertTrue(l.isMember("Lucky Charms")); + + //Index 1, the Spaghetti + assertEquals("Spaghetti", l.getName(1)); + assertEquals(58, l.getQuantity(1)); + assertTrue(l.isMember("Spaghetti")); + + //Index 2, the Pineapples + assertEquals("Pineapples", l.getName(2)); + assertEquals(50, l.getQuantity(2)); + assertTrue(l.isMember("Pineapples")); + + //Index 3, the Dogs + assertEquals("Dogs", l.getName(3)); + assertEquals(10, l.getQuantity(3)); + assertTrue(l.isMember("Dogs")); + + //Index 4, the Cats + assertEquals("Cats", l.getName(4)); + assertEquals(5, l.getQuantity(4)); + assertTrue(l.isMember("Cats")); + + //Index 5, Fred + assertEquals("Fred", l.getName(5)); + assertEquals(1, l.getQuantity(5)); + assertTrue(l.isMember("Fred")); + + //Length == 6 + assertEquals(6, l.length()); + } }