From 2248633d30c56bd23aa6dd45e8d52cd90bea2ab9 Mon Sep 17 00:00:00 2001 From: Marisa Pacitti Date: Thu, 2 Apr 2015 15:03:12 -0400 Subject: [PATCH] Linked List Lab --- LinkedListQuestions.txt | 30 +++++++++++++ src/SortedLinkedList.java | 75 ++++++++++++++++++++++++++++--- src/SortedLinkedListTest.java | 84 +++++++++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+), 6 deletions(-) create mode 100644 LinkedListQuestions.txt mode change 100755 => 100644 src/SortedLinkedList.java create mode 100644 src/SortedLinkedListTest.java diff --git a/LinkedListQuestions.txt b/LinkedListQuestions.txt new file mode 100644 index 0000000..8d17453 --- /dev/null +++ b/LinkedListQuestions.txt @@ -0,0 +1,30 @@ +**Question 1** +Inorder to test that the methods work on an empty list you have to make an new list with +nothing in it. Then you need to go to the methods and correctly create the code for them +that would return the correct answer. + +**Question 2** +To test that the methods work on a single node list, we need to again create a new list. +then you again go to the methods and create and insertion method as well as fixing the +'getName" and 'getQuantity' methods so that they work with more nodes. + +**Question 4** +Your code will still work to add nodes that belong at the head because you insert the new +node to the head every time you add a new node. + +**Question 5** +prev would hav to be curr inorder to add second node to the end of the list. + +**Question 6** +prev would have to be prev.setNext because curr is null so the prev.setNext will place a +new node in between prev and curr. + +**Question 7** +I this that this is evidence that the class is working because we tested adding a new node +to the begging, end, and middle. + +**Question 8** +Implementing the methods "from scratch" was very challenging, however it helped to +understand the importance of manipulating the methods inorder to get to the finished +linke list. + \ No newline at end of file diff --git a/src/SortedLinkedList.java b/src/SortedLinkedList.java old mode 100755 new mode 100644 index f75caa0..b7b93b9 --- a/src/SortedLinkedList.java +++ b/src/SortedLinkedList.java @@ -1,6 +1,7 @@ public class SortedLinkedList { private Node head; + private int length; /** * Create an empty list. @@ -8,6 +9,7 @@ public class SortedLinkedList public SortedLinkedList() { head = null; + length = 0; } /** @@ -34,7 +36,15 @@ public void print() */ public String getName(int index) { - + if (head == null || index < 0 || index >= length) + return null; + else { + Node temp = head; + for(int i = 0; i < index; i++) { + temp = temp.getNext(); + } + return temp.getName(); + } } /** @@ -44,15 +54,31 @@ public String getName(int index) */ public int getQuantity(int index) { + Node temp = head; + if (temp ==null) { + return -1; + } + + for (int i = 0; i < index; i++) { + temp = temp.getNext(); + } + + if (temp == null) { + return -1; + } + else { + return temp.getQuantity(); + } } + /** * This method will return the number of elements currently held in the list. */ public int length() - { - + { + return length; } /** @@ -61,7 +87,13 @@ public int length() */ public boolean isMember(String name) { - + Node temp = head; + if (head == null) { + return false; + } + else { + return true; + } } /** @@ -69,7 +101,38 @@ public boolean isMember(String name) * order. This order is specified by the quantity from low to high. */ public void insert(String name, int quantity) - { + { + Node newNode = new Node(name, quantity); + + if (head == null) { + head = newNode; + length += 1; + return; + } + + if (newNode.getQuantity() < head.getQuantity()) { + newNode.setNext(head); + head = newNode; + length += 1; + return; + } - } + Node curr = head.getNext(); + Node prev = head; + while (curr != null) { + + if (newNode.getQuantity() >= prev.getQuantity() && newNode.getQuantity() < curr.getQuantity()) { + prev.setNext(newNode); + newNode.setNext(curr); + length += 1; + return; + } + + prev = curr; + curr = curr.getNext(); + } + + prev.setNext(newNode); + length += 1; + } } diff --git a/src/SortedLinkedListTest.java b/src/SortedLinkedListTest.java new file mode 100644 index 0000000..57b99df --- /dev/null +++ b/src/SortedLinkedListTest.java @@ -0,0 +1,84 @@ +import junit.framework.TestCase; + +public class SortedLinkedListTest extends TestCase { + public void testEmptyList() { + SortedLinkedList ll = new SortedLinkedList(); + assertEquals(null, ll.getName(2)); + assertEquals(-1, ll.getQuantity(0)); + assertEquals(0, ll.length()); + assertFalse(ll.isMember("member")); + } + + public void testSingleNode() { + SortedLinkedList ll = new SortedLinkedList(); + ll.insert("Dan", 1); + assertEquals("Dan", ll.getName(0)); + assertEquals(1, ll.getQuantity(0)); + assertEquals(1, ll.length()); + assertTrue(ll.isMember("Dan")); + } + + public void testSecondNodeNewHead() { + SortedLinkedList ll = new SortedLinkedList(); + ll.insert("Banana", 2); + ll.insert("Apple", 1); + assertEquals("Banana", ll.getName(1)); + assertEquals("Apple", ll.getName(0)); + assertEquals(1, ll.getQuantity(0)); + assertEquals(2, ll.getQuantity(1)); + assertEquals(2, ll.length()); + assertTrue(ll.isMember("Banana")); + assertTrue(ll.isMember("Apple")); + } + + public void testRepeatedHeadInsert() { + SortedLinkedList ll = new SortedLinkedList(); + ll.insert("Rich", 3); + ll.insert("Richy", 2); + ll.insert("Dollar", 1); + assertEquals("Rich", ll.getName(2)); + assertEquals("Richy", ll.getName(1)); + assertEquals("Dollar", ll.getName(0)); + assertEquals(3, ll.getQuantity(2)); + assertEquals(2, ll.getQuantity(1)); + assertEquals(1, ll.getQuantity(0)); + assertEquals(3, ll.length()); + assertTrue(ll.isMember("Rich")); + assertTrue(ll.isMember("Richy")); + assertTrue(ll.isMember("Dollar")); + } + + public void testSecondTail() { + SortedLinkedList ll = new SortedLinkedList(); + ll.insert("Todd", 3); + ll.insert("Millie", 2); + ll.insert("Brad", 1); + assertEquals("Todd", ll.getName(2)); + assertEquals("Millie", ll.getName(1)); + assertEquals("Brad", ll.getName(0)); + assertEquals(3, ll.getQuantity(2)); + assertEquals(2, ll.getQuantity(1)); + assertEquals(1, ll.getQuantity(0)); + assertEquals(3, ll.length()); + assertTrue(ll.isMember("Todd")); + assertTrue(ll.isMember("Millie")); + assertTrue(ll.isMember("Brad")); + } + + public void testMiddleThird() { + SortedLinkedList ll = new SortedLinkedList(); + ll.insert("Bubbles", 3); + ll.insert("Blossom", 2); + ll.insert("Buttercup", 1); + assertEquals("Bubbles", ll.getName(2)); + assertEquals("Blossom", ll.getName(1)); + assertEquals("Buttercup", ll.getName(0)); + assertEquals(3, ll.getQuantity(2)); + assertEquals(2, ll.getQuantity(1)); + assertEquals(1, ll.getQuantity(0)); + assertEquals(3, ll.length()); + assertTrue(ll.isMember("Bubbles")); + assertTrue(ll.isMember("Blossom")); + assertTrue(ll.isMember("Buttercup")); + } +}