Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions LinkedListQuestions.txt
Original file line number Diff line number Diff line change
@@ -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.

75 changes: 69 additions & 6 deletions src/SortedLinkedList.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
public class SortedLinkedList
{
private Node head;
private int length;

/**
* Create an empty list.
*/
public SortedLinkedList()
{
head = null;
length = 0;
}

/**
Expand All @@ -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();
}
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -61,15 +87,52 @@ public int length()
*/
public boolean isMember(String name)
{

Node temp = head;
if (head == null) {
return false;
}
else {
return true;
}
}

/**
* This method will add the specified name/quantity to the list in sorted
* 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;
}
}
84 changes: 84 additions & 0 deletions src/SortedLinkedListTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}