Skip to content

some #43

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 3 commits into
base: master
Choose a base branch
from
Open

some #43

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_12.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.

421 changes: 159 additions & 262 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.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>


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

Node head = null;
Node tail = null;


class Node {
T data;
Node next;

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


public void add(T data) {
Node elNubo = new Node(data);
if (head == null) {
head = elNubo;

} else {// SI NO ESTA VASIO AGRAGARLO AL FINAL..

tail.next = elNubo;
}
tail = elNubo; // AHORA EL EL ES TAIL.
}


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

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

}
return false;
}

public Integer find(T element) {
Node enElQueEsta = head;
for (int i = 0; i < size(); i++) {
if (!enElQueEsta.data.equals(element)) {
enElQueEsta = enElQueEsta.next;
} else return i;
}
return -1;
}


public T get(int index) {
Node box = head;
for (int i = 0; i < size(); i++) {
if (i == index ){
return box.data;
}else box = box.next;


}
return null;
}


public SinglyLinkedList<T> copy(){
SinglyLinkedList<T> copy = new SinglyLinkedList<T>();

Node nodito = head;
for (int i = 0; i <= size(); i++) {
copy.add(nodito.data);
nodito = head.next;
}

return copy;
}
public void remove(Integer index) {
Node current = head;
Node last = head;
for (int i = 0; i < size(); i++) {
if(i != index) {
last = current;
current = current.next;
} else {
while(i < size() - 1) {
last.next = current.next;
}
}
}
}
}



Original file line number Diff line number Diff line change
@@ -1,7 +1,130 @@
package com.zipcodewilmington.singlylinkedlist;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.Assert;



import javax.xml.soap.Node;

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



@Test
public void addTest(){
//GIVEN
int expected = 3;
SinglyLinkedList<String> listina = new SinglyLinkedList<String>();

//WHEN
listina.add("Butter");
listina.add("lgo");
listina.add("ptd");

int actual = listina.size();

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

@Test
public void sizeTest(){
//GIVEN
SinglyLinkedList<Integer> listy = new SinglyLinkedList<Integer>();;
int expected = 2;

//WHEN
listy.add(7);
listy.add(13);
int actual = listy.size();

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

}

@Test
public void containsTest(){
//GIVEN
SinglyLinkedList<String> listina = new SinglyLinkedList<String>();

//WHEN
listina.add("Butter");
listina.add("lgo");
listina.add("ptd");

Assert.assertTrue(listina.contains("Butter"));
}

@Test
public void findTest(){
SinglyLinkedList<Integer> listy = new SinglyLinkedList<Integer>();;
Integer expected = 0;

//WHEN
listy.add(7);
listy.add(13);
listy.add(20);
Integer actual = listy.find(7);

Assert.assertEquals(expected, actual);
}

@Test
public void getTest(){
//GIVEN
SinglyLinkedList<String> listina = new SinglyLinkedList<String>();
String expected = "ptd";
//WHEN
listina.add("Butter");
listina.add("lgo");
listina.add("ptd");

String actual = listina.get(2);
//THEN
Assert.assertEquals(expected, actual);

}

@Test
public void copyTest(){
SinglyLinkedList<String> listina = new SinglyLinkedList<String>();
String expected = "ptd";
//WHEN
listina.add("Butter");
listina.add("lgo");
listina.add("ptd");

SinglyLinkedList<String> actual = listina.copy();
String actualPrimera = actual.get(0);
String actualDespues = actual.get(1);

Assert.assertEquals((String) "Butter", actualPrimera);


}

@Test
public void removeTest(){
SinglyLinkedList<Integer> listy = new SinglyLinkedList<Integer>();;
Integer expected = 2;

//WHEN
listy.add(7);
listy.add(13);
listy.add(20);


listy.remove(1);
Integer actual = listy.size();

Assert.assertEquals(expected, actual);
}



}