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
133 changes: 121 additions & 12 deletions src/SortedLinkedList.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
public class SortedLinkedList
{

private Node head;
private int theLength;

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

/**
Expand All @@ -20,7 +23,7 @@ public void print()
Node temp = head;
while(temp != null)
{
System.out.print(temp.getName() + ":" + temp.getQuantity() + " ");
System.out.print(temp.getName() + " " + temp.getQuantity() + " ");
temp = temp.getNext();
}

Expand All @@ -32,9 +35,24 @@ 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 temp = head;
if (temp == null) {
return null;
}
if (index == 0) {
return temp.getName();
}
for (int i = 0; i<index; i++){
temp = temp.getNext();
}
if (temp == null) {
return null;
}
else {
return temp.getName();
}
}

/**
Expand All @@ -44,15 +62,38 @@ 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()
{

public int length() {
Node temp = head;
int i = 0;
if (head == null) {
return 0;
}
else{
while (temp != null) {
i += 1;
temp = temp.getNext();
}
return i;
}

}

/**
Expand All @@ -61,7 +102,24 @@ public int length()
*/
public boolean isMember(String name)
{

Node temp = head;
if (head == null) {
if (name == null){
return true;
}
else{
return false;
}
}
while(temp.getName() != name){
temp = temp.getNext();
}
if (temp.getName() == name){
return true;
}
else{
return false;
}
}

/**
Expand All @@ -70,6 +128,57 @@ public boolean isMember(String name)
*/
public void insert(String name, int quantity)
{

}
}
Node newNode = new Node(name, quantity);
Node temp = head;
Node prev = head;

// for an empty list
if (head == null) {
head = newNode;
}

else {

//if it's larger than the head
if (quantity >= head.getQuantity()){
newNode.setNext(head);
head = newNode;
}

//otherwise
else{
temp = temp.getNext();

//if the head is the only item
if (temp == null){
prev.setNext(newNode);
}

//adding in the middle or end with more than one item
else {
//adding in the middle
while (temp.getNext() != null){
if (quantity >= temp.getQuantity() && quantity <= prev.getQuantity()){
newNode.setNext(temp);
prev.setNext(newNode);
break;
}
prev = prev.getNext();
temp = temp.getNext();
}
//adding at the end or just before
if (temp.getNext() == null){
if (quantity >= temp.getQuantity() && quantity <= prev.getQuantity()){
newNode.setNext(temp);
prev.setNext(newNode);
}
//adds to the end
else {
temp.setNext(newNode);
}
}
}
}
}
}
}
104 changes: 104 additions & 0 deletions src/testLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
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 testLinkedList 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 s = new SortedLinkedList();
assertEquals(s.getName(0), null);
assertEquals(s.getQuantity(0), -1);
assertEquals(s.length(), 0);
assertTrue(s.isMember(null));
}

public void testOneNode() {
SortedLinkedList s = new SortedLinkedList();
s.insert("A", 5);

assertEquals(s.getName(0), "A");
assertEquals(s.getQuantity(0), 5);
assertEquals(s.length(), 1);
assertTrue(s.isMember("A"));

}

public void testTwoNodes() {
SortedLinkedList s = new SortedLinkedList();
s.insert("A", 5);
s.insert("B", 10);

assertEquals(s.getName(0), "B");
assertEquals(s.getName(1), "A");
assertEquals(s.getQuantity(0), 10);
assertEquals(s.getQuantity(1), 5);
assertEquals(s.length(), 2);
assertTrue(s.isMember("A"));
assertTrue(s.isMember("B"));

}

public void testThreeNode(){
SortedLinkedList s = new SortedLinkedList();
s.insert("A", 5);
s.insert("B", 10);
s.insert("C", 15);
s.insert("D", 7);
s.insert("E", 4);
s.insert("F",12);

assertEquals(s.getName(0), "C");
assertEquals(s.getName(1), "F");
assertEquals(s.getName(2), "B");
assertEquals(s.getName(3), "D");
assertEquals(s.getName(4), "A");
assertEquals(s.getName(5), "E");
assertEquals(s.getQuantity(0), 15);
assertEquals(s.getQuantity(1), 12);
assertEquals(s.getQuantity(2), 10);
assertEquals(s.getQuantity(3), 7);
assertEquals(s.getQuantity(4), 5);
assertEquals(s.getQuantity(5), 4);
assertEquals(s.length(), 6);
assertTrue(s.isMember("C"));
assertTrue(s.isMember("A"));
assertTrue(s.isMember("B"));
assertTrue(s.isMember("D"));
assertTrue(s.isMember("E"));
assertTrue(s.isMember("F"));

}

public void testDoubleTailInsert(){
SortedLinkedList s = new SortedLinkedList();
s.insert("A", 15);
s.insert("B", 10);
s.insert("C", 5);
s.insert("D", 3);

assertEquals(s.getName(0), "A");
assertEquals(s.getName(1), "B");
assertEquals(s.getName(2), "C");
assertEquals(s.getName(3), "D");
assertEquals(s.getQuantity(0), 15);
assertEquals(s.getQuantity(1), 10);
assertEquals(s.getQuantity(2), 5);
assertEquals(s.getQuantity(3), 3);
assertEquals(s.length(), 4);
assertTrue(s.isMember("C"));
assertTrue(s.isMember("A"));
assertTrue(s.isMember("B"));
assertTrue(s.isMember("D"));
}

}