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
25 changes: 25 additions & 0 deletions linkedlistanswers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**Martin Nesbitt**

***Linked List Lab***

**Question 1:**
The getName() method can be tested by asserting null. the getQuantity() method can be tested by asserting -1 as the return value for an invalid index. The length() method may be tested by asserting 0. The isMember() method can be tested by asserting false and using anything except for a valid value as the parameter.

**Question 2:**
The getName() method will be tested with an assertion of b. The getQuantity method can be tested by asserting 1. The length() method can be tested by asserting 1. The isMember() method can be tested by asserting true when calling isMember(b).

**Question 4:**
The code at this point will not be sufficient because it does not take into account the node that is currently in the list.

**Question 5:**
Prev should be null and head should be curr so it can set its next as the new node.

**Question 6:**
Prev should be the first node so it can set its next as the new node and the new node can set its next as curr.

**Question 7:**
These tests are sufficient in that the test all possible scenarios when inserting an element, however in the grand scheme of linked lists, this class itself would be insufficient seeing as there is no remove function.




135 changes: 106 additions & 29 deletions src/SortedLinkedList.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
public class SortedLinkedList
{
public class SortedLinkedList {
private Node head;

private int elements;
private int i;
/**
* Create an empty list.
*/
public SortedLinkedList()
{
public SortedLinkedList() {
head = null;
}

Expand All @@ -15,15 +14,12 @@ public SortedLinkedList()
* the form "name:quantity" and the entries will be separated by a single
* space.
*/
public void print()
{
public void print() {
Node temp = head;
while(temp != null)
{
while(temp != null) {
System.out.print(temp.getName() + ":" + temp.getQuantity() + " ");
temp = temp.getNext();
}

System.out.println();
}

Expand All @@ -32,44 +28,125 @@ public void print()
* array, the index of the first entry is zero. If the index is invalid,
* this method will return null.
*/
public String getName(int index)
{

public String getName(int index) {
Node n = head;
int c = 0;
if (head == null) {
return null;
}
while (c != index) {
n = n.getNext();
c++;
}
return n.getName();
}

/**
* This method will return the quantity at the specified index. Similar to
* an array, the index of the first entry is zero. If the index is invalid,
* this method will return -1.
*/
public int getQuantity(int index)
{

}
public int getQuantity(int index) {
Node curr = head;
int c = 0;
if (head == null) {
return -1;
}
while (c != index) {
curr = curr.getNext();
c++;
}
return curr.getQuantity();
}


/**
* This method will return the number of elements currently held in the list.
*/
public int length()
{

public int length() {
if (head != null) {
return elements;
}
else {
return 0;
}
}

/**
* This method will return whether or not the specified name is contained in
* the list.
*/
public boolean isMember(String name)
{

public boolean isMember(String name) {
Node curr = head;
while(curr != null) {
if(curr.getName() == name) {
return true;
}
else {
curr = curr.getNext();
}
}
return false;
}

/**
* 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)
{

}
public void insert(String name, int quantity) {
Node temp = new Node(name, quantity);
Node prev = null;
Node curr = head;
if(elements == 0) {
temp.setNext(head);
head = temp;
elements ++;
}
else {
while (curr != null) {
if (temp.getQuantity() > curr.getQuantity()) {
elements ++;
if(prev == null) {
temp.setNext(head);
head = temp;
break;
}
else {
prev.setNext(temp);
temp.setNext(curr);
break;
}
}
if(temp.getQuantity() == curr.getQuantity()) {
elements ++;
if (prev == null) {
temp.setNext(head);
head = temp;
break;
}
else {
prev.setNext(temp);
temp.setNext(curr);
}
}
prev = curr;
curr = curr.getNext();
}
if(curr == null) {
prev.setNext(temp);
elements++;
}
}
}
}













89 changes: 89 additions & 0 deletions src/SortedLinkedListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import junit.framework.TestCase;

public class SortedLinkedListTest extends TestCase {
public void testEmptyList() {
SortedLinkedList a = new SortedLinkedList();
assertEquals(null, a.getName(0));
assertEquals(-1, a.getQuantity(0));
assertEquals(0, a.length());
assertEquals(false, a.isMember("Frank"));
}
public void testSingleNode() {
SortedLinkedList a = new SortedLinkedList();
a.insert("b", 1);
assertEquals("b", a.getName(0));
assertEquals(1, a.getQuantity(0));
assertEquals(1, a.length());
assertEquals(true, a.isMember("b"));
}
public void testSecondNodeNewHead() {
SortedLinkedList l = new SortedLinkedList();
l.insert("a", 1);
l.insert("b", 2);
assertEquals("b", l.getName(0));
assertEquals("a", l.getName(1));
assertEquals(2, l.getQuantity(0));
assertEquals(1, l.getQuantity(1));
assertEquals(2, l.length());
assertEquals(true, l.isMember("a"));
assertEquals(true, l.isMember("b"));
}

public void testRepeatedHeadInsert() {
SortedLinkedList l = new SortedLinkedList();
l.insert("a", 1);
l.insert("b", 2);
l.insert("c", 3);
assertEquals("c", l.getName(0));
assertEquals("b", l.getName(1));
assertEquals("a", l.getName(2));
assertEquals(3, l.getQuantity(0));
assertEquals(2, l.getQuantity(1));
assertEquals(1, l.getQuantity(2));
assertEquals(3, l.length());
assertEquals(true, l.isMember("a"));
assertEquals(true, l.isMember("b"));
assertEquals(true, l.isMember("c"));

}


public void testSecondTail() {
SortedLinkedList l = new SortedLinkedList();
l.insert("a", 1);
l.insert("b", 0);
assertEquals("a", l.getName(0));
assertEquals("b", l.getName(1));
assertEquals(1, l.getQuantity(0));
assertEquals(0, l.getQuantity(1));
assertEquals(2, l.length());
assertEquals(true, l.isMember("a"));
assertEquals(true, l.isMember("b"));
}

public void testMiddleThird() {
SortedLinkedList l = new SortedLinkedList();
l.insert("a", 3);
l.insert("c", 1);
l.insert("b", 2);
assertEquals("b", l.getName(1));
assertEquals("a", l.getName(0));
assertEquals("c", l.getName(2));
assertEquals(3, l.getQuantity(0));
assertEquals(2, l.getQuantity(1));
assertEquals(1, l.getQuantity(2));
assertEquals(3, l.length());
assertEquals(true, l.isMember("a"));
assertEquals(true, l.isMember("b"));
assertEquals(true, l.isMember("c"));
}
}