Skip to content

done #41

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

done #41

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.

420 changes: 159 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
@@ -1,7 +1,127 @@
package com.zipcodewilmington.singlylinkedlist;

import com.sun.org.apache.xpath.internal.operations.Bool;

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

class Node {
Object data;
Node next;

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

public Node head;
public Node tail;

public void add(Object data){
Node newNode = new Node(data);
if(this.head == null){
this.head = newNode;
} else {
tail.next = newNode;
}
tail = newNode;
}

public void remove(int index){
Node current = this.head;
Node previous = null;

if (index == 0 && current != null){
this.head = current.next;
}
int counter = 0;
while (current != null) {
if(counter == index){
previous.next = current.next; //[previous] -> [current.next]
break;
} else {
previous = current;
current = current.next;
counter++;
}
}
if (current == null){
System.out.println("not found");
}
}

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

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

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

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

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

public void sort(){
Node current = this.head;
for(int i =0; i < size(); i++){
while(current.next != null){
Node next = current.next;
if((Integer)current.data > (Integer) next.data){ // [head] -> [current] -> current.next] -> [tail] -> [null]
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,167 @@
package com.zipcodewilmington.singlylinkedlist;

import com.sun.org.apache.xpath.internal.operations.Bool;
import org.junit.Assert;
import org.junit.Test;

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

@Test
public void addTest(){
// Given
SinglyLinkedList list = new SinglyLinkedList();

// When
list.add(1);
list.add(2);
list.add(3);
list.add(4);

int actual = list.size();
int expected = 4;


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

@Test
public void removeTest(){
// Given
SinglyLinkedList list = new SinglyLinkedList();

// When
list.add(1);
list.add(2);
list.add(3);
list.add(4);

list.remove(3);
int expected = 3;
int actual = list.size();

Assert.assertEquals(expected, actual);

}

@Test
public void containsTest(){
// Given
SinglyLinkedList list = new SinglyLinkedList();

// When
list.add(1);
list.add(2);
list.add(3);
list.add(4);

Boolean actual = list.contains(3);

Assert.assertTrue(actual);
Assert.assertFalse(list.contains(6));
}

@Test
public void sizeTest(){
// Given
SinglyLinkedList list = new SinglyLinkedList();

// When
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);

int expected = 6;
int actual =list.size();

Assert.assertEquals(expected,actual);
}

@Test
public void findTest(){
// Given
SinglyLinkedList list = new SinglyLinkedList();

// When
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);

Integer actual = list.find(4);
Integer expected = 3;

Assert.assertEquals(expected, actual);
}

@Test
public void getTest(){
// Given
SinglyLinkedList list = new SinglyLinkedList();

// When
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);

Integer actual = (Integer) list.get(4);
Integer expected = 5;

Assert.assertEquals(expected, actual);
}

@Test
public void copyTest(){
// Given
SinglyLinkedList list = new SinglyLinkedList();

// When
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);

SinglyLinkedList newList = list.copy();
Integer expected = list.size();
Integer actual = newList.size();

Assert.assertEquals(expected, actual);

}

@Test
public void sortTest(){
// Given
SinglyLinkedList list = new SinglyLinkedList();

// When
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(9);
list.add(7);
list.add(6);

list.sort();

Assert.assertTrue(list.find(1) == 0);
Assert.assertTrue(list.find(9) == 7);

}

}