diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 4be381c..a07d5fa 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -6,11 +6,13 @@ + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_12.xml b/.idea/libraries/Maven__junit_junit_4_12.xml new file mode 100644 index 0000000..d411041 --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_12.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000..f58bbc1 --- /dev/null +++ b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index ad4fefc..8b9083f 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,6 +3,7 @@ + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index b6d36bd..fe7265d 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,86 +1,15 @@ + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -212,60 +142,23 @@ + + - + + + + + + + - - - - - - - - - - + - - - - - + @@ -459,6 +346,8 @@ @@ -494,12 +383,20 @@ - - \ No newline at end of file diff --git a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java index 2fb3165..09d1e7f 100644 --- a/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java +++ b/src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java @@ -3,5 +3,108 @@ /** * Created by leon on 1/10/18. */ -public class SinglyLinkedList { +public class SinglyLinkedList< T extends Comparable> { + + 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 copy(){ + SinglyLinkedList copy = new SinglyLinkedList(); + + 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; + } + } + } + } } + + + diff --git a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java index 5cc057e..b2ff29b 100644 --- a/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java +++ b/src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java @@ -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 listina = new SinglyLinkedList(); + + //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 listy = new SinglyLinkedList();; + 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 listina = new SinglyLinkedList(); + + //WHEN + listina.add("Butter"); + listina.add("lgo"); + listina.add("ptd"); + + Assert.assertTrue(listina.contains("Butter")); +} + +@Test + public void findTest(){ + SinglyLinkedList listy = new SinglyLinkedList();; + 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 listina = new SinglyLinkedList(); + 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 listina = new SinglyLinkedList(); + String expected = "ptd"; + //WHEN + listina.add("Butter"); + listina.add("lgo"); + listina.add("ptd"); + + SinglyLinkedList actual = listina.copy(); + String actualPrimera = actual.get(0); + String actualDespues = actual.get(1); + + Assert.assertEquals((String) "Butter", actualPrimera); + + +} + +@Test + public void removeTest(){ + SinglyLinkedList listy = new SinglyLinkedList();; + Integer expected = 2; + + //WHEN + listy.add(7); + listy.add(13); + listy.add(20); + + + listy.remove(1); + Integer actual = listy.size(); + + Assert.assertEquals(expected, actual); +} + + + }