Skip to content

linked the traincars #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__junit_junit_4_13_1.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

429 changes: 168 additions & 261 deletions .idea/workspace.xml

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
<groupId>com.zipcodewilmington</groupId>
<artifactId>singlylinkedlist</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,124 @@
/**
* Created by leon on 1/10/18.
*/
public class SinglyLinkedList {
}
public class SinglyLinkedList< T extends Comparable<T>> {

class Node<T> {
T data;
Node next;

public Node(T data){
this.data = data;
this.next = null;
}
}

public Node head = null;
public Node tail = null;


public void addNode(T data) {
Node newNode = new Node(data);

if (head == null) {
head = newNode;
} else {
tail.next = newNode;
}
tail = newNode;
}

public Integer size(){
Integer count = 0;
Node current = this.head;

while(current != null) {
count++;
current = current.next;
}
return count;
}

public Integer find(T valueToFind) {
Node current = this.head;
Integer index = 0;

if(current == null)
return -1;

while (current != null) {
if (current.data.equals(valueToFind))
return index;
index++;
current = current.next;
}
return -1;
}

public Boolean contains(T valueToFind) {
return this.find(valueToFind) >= 0; //if value is not -1 then the list contains the value
}

public void remove(T valueToFind){
Node current = head;
Node last = head;

while(current != null){
if(current.data.equals(valueToFind)) {
if (current.next == null) {
last.next = null;
} else if (current == last) {
head = current.next;
} else
last.next = current.next;
}
last = current;
current = current.next;
}
}

public T get(Integer index) {
Node current = head;

if(index >= size() || index < 0)
return null;

if (index == size() -1)
return (T)tail.data;

for (int i = 0; i < index; i++) {
current = current.next;
}
return (T)current.data;
}

public SinglyLinkedList<T> copy() {
SinglyLinkedList<T> copiedList = new SinglyLinkedList<T>();
Node current = head;

while(current != null) {
copiedList.addNode((T)current.data);
current = current.next;
}
return copiedList;
}
public void sort(){
Node current = head;
for(int i = 0; i < size(); i++){
current = head;
while(current.next != null){
if(compareTo((T)current.data, (T)current.next.data) > 0){
T nextVal = (T)current.next.data;
current.next.data = current.data;
current.data = nextVal;
}
current = current.next;
}
}
}

public int compareTo(T o, T o2) {
return o.compareTo(o2);
}
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,90 @@
package com.zipcodewilmington.singlylinkedlist;

import org.junit.Assert;
import org.junit.Test;


/**
* Created by leon on 1/10/18.
*/
public class SinglyLinkedListTest {

@Test
public void TestAddNode() {
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();

list.addNode(1);
list.addNode(2);
list.addNode(3);

Integer expected = 3;
Integer actual = list.size();

Assert.assertEquals(expected, actual);
}

@Test
public void TestFind() {
SinglyLinkedList<String> list = new SinglyLinkedList<String>();

list.addNode("Skyrim");
list.addNode("Fallout");
list.addNode("PipMAN");

Integer expected = 2;
Integer actual = list.find("PipMAN");

Assert.assertEquals(expected, actual);
}

@Test
public void TestRemove() {
SinglyLinkedList<String> list = new SinglyLinkedList<String>();

list.addNode("Skyrim");
list.addNode("Fallout");
list.addNode("PipMAN");
list.addNode("DankBrotherhood");
list.addNode("ArgonianMaid");

list.remove("DankBrotherhood");
Integer expected = 4;
Integer actual = list.size();

Assert.assertEquals(expected, actual);
}

@Test
public void TestCopy() {
SinglyLinkedList<Double> list = new SinglyLinkedList<Double>();

list.addNode(4.0);
list.addNode(8.0);
list.addNode(6.0);
list.addNode(9.0);
list.addNode(11.0);

SinglyLinkedList<Double> newList = list.copy();
Integer expected = 5;
Integer actual = newList.size();

Assert.assertEquals(expected, actual);
}

@Test
public void TestSort() {
SinglyLinkedList<Integer> list = new SinglyLinkedList<Integer>();

list.addNode(4);
list.addNode(8);
list.addNode(6);
list.addNode(9);
list.addNode(11);

list.sort();
Integer expected = 6;
Integer actual = list.get(1);

Assert.assertEquals(expected, actual);
}
}