Skip to content

Single list completed #46

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.

348 changes: 87 additions & 261 deletions .idea/workspace.xml

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
<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>
</dependencies>


</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
* Created by leon on 1/10/18.
*/
public class MainApplication {
public static void main(String[] args){
SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,113 @@
package com.zipcodewilmington.singlylinkedlist;



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

class Node{
Object data;
Node next;

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

}
}

public SinglyLinkedList(){

}

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

public void addNode(Object data){
Node node = new Node(data);
if(head == null){
head = node;
}else {
tail.next = node;
}
tail = node;
}

public Integer size(){
Integer count = 0;
Node current = head;
while(current!= null){
count++;
current = current.next;
}
return count;
}

public Object find(Object data){
Integer index = 0;
Node current = head;
while(current!= null){
if(current.data == data){
return index;
}
index++;
current = current.next;
}
return -1;
}

public Boolean contains(Object data){
Node current = head;
while(current!= null){
if(current.data == data){
return true;
}
current = current.next;
}
return false;
}

public SinglyLinkedList remove(Integer index){
SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
Integer indexOfNode = 0;
Node current = head;
while(current!=null){
if(index != indexOfNode){
singlyLinkedList.addNode(current.data);
}
indexOfNode++;
current = current.next;

}
return singlyLinkedList;
}

public SinglyLinkedList copy(){
SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
Node current = head;
while(current!=null){
singlyLinkedList.addNode(current.data);
current = current.next;
}
return singlyLinkedList;
}

public void sort(){
Node current = head;
for(int i = 0; i < size();i++){
current = head;
while(current.next!= null){
Node next = current.next;
if((Integer)current.data>(Integer)next.data){
Object temp = current.data;
current.data = next.data;
next.data = temp;
}
current = current.next;
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,99 @@
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 TestSize(){
//given
SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
Integer expected = 0;
//when
Integer actual = singlyLinkedList.size();
//then
Assert.assertEquals(expected,actual);
}

@Test
public void testAdd(){
//given
SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
singlyLinkedList.addNode(1);
singlyLinkedList.addNode(2);
Integer expected = 2;
//when
Integer actual = singlyLinkedList.size();
//then
Assert.assertEquals(expected, actual);

}

@Test
public void testFind(){
SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
Integer expectedInd = 2;
singlyLinkedList.addNode("Hello");
singlyLinkedList.addNode("World");
singlyLinkedList.addNode("Zipcode");

//When
Object actual = singlyLinkedList.find("Zipcode");

//then
Assert.assertEquals(expectedInd,actual);

}

@Test
public void testContains(){
SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
Boolean expected = true;
singlyLinkedList.addNode("Hi");
singlyLinkedList.addNode("Hello");

//when
Boolean actual = singlyLinkedList.contains("Hello");

//Then
Assert.assertTrue(actual);
}

@Test
public void testCopy(){
SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
singlyLinkedList.addNode(2);
singlyLinkedList.addNode(3);
Integer expected = 2;

//When
Integer actual = singlyLinkedList.copy().size();

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void sort(){
SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
singlyLinkedList.addNode(5);
singlyLinkedList.addNode(4);
singlyLinkedList.addNode(3);
singlyLinkedList.addNode(2);
Integer expected = 4;

//When
Integer actual = singlyLinkedList.size();

//then
Assert.assertEquals(expected,actual);



}

}