Skip to content

Completed lab late #47

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 2 commits 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_2.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.

419 changes: 158 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>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
package com.zipcodewilmington.singlylinkedlist;

import java.util.LinkedList;

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

public static void main(String[] args) {
SinglyLinkedList<String> singlyLinkedList = new SinglyLinkedList();
singlyLinkedList.add("1");
singlyLinkedList.add("2");
singlyLinkedList.add("3");
singlyLinkedList.add("4");
// singlyLinkedList.remove(3);


SinglyLinkedList<String> singlyLinkedList1 = singlyLinkedList.copy();
singlyLinkedList.add("6");

Boolean result = singlyLinkedList.contains("3");

System.out.println(result);
System.out.println(singlyLinkedList.find("1"));
singlyLinkedList.display();
singlyLinkedList1.display();


LinkedList<String> list = new LinkedList<String>();
list.clone();
list.add("1");
list.add("2");
list.add("3");

// System.out.println(list);
}

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


import jdk.nashorn.internal.ir.WhileNode;

/**
* Created by leon on 1/10/18.
*/
public class SinglyLinkedList {
public class SinglyLinkedList<T extends Comparable<T>> {

public SinglyLinkedList() {
}

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 add(T data) {
Node node = new Node(data);
if (head == null) {
head = node;
} else {
tail.next = node;
}
tail = node;
}


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

}
return counter;
}

public Integer find(T data) {
Node current = head;
Integer index = 0;

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

public Node<T> get(Integer index){

Node current = head;
Integer currentIndex = 0;

while(currentIndex != index){
current = current.next;
currentIndex++;
}
return current;
}

public Boolean contains(T data) {
Node current = head;

while (current != null) {
if (current.data == data) {
return true;
}
current = current.next;
}
return false;
}



public void remove(Integer 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;
break;
} else {
previous = current;
current = current.next;
counter++;
}
}
}

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

while(current != null) {
newList.add((T) current.data);
current = current.next;
}
return newList;
}






public void display() {

Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
while(current != null) {

System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,121 @@
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 addTest(){
//given
SinglyLinkedList<String> list = new SinglyLinkedList<String>();
Integer expectedSize = 3;
//when
list.add("a");
list.add("b");
list.add("c");
Integer actual = list.size();
//then
Assert.assertEquals(expectedSize, actual);
}


@Test
public void removeTest(){
//given
SinglyLinkedList<String> list = new SinglyLinkedList<String>();
Integer expectedSize = 2;
//when
list.add("a");
list.add("b");
list.add("c");
list.remove(2);
Integer actual = list.size();
//then
Assert.assertEquals(expectedSize, actual);

}
@Test
public void sizeTest(){
//given
SinglyLinkedList<String> list = new SinglyLinkedList<String>();
Integer expectedSize = 6;
//when
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
Integer actual = list.size();
//then
Assert.assertEquals(expectedSize, actual);

}

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

//when
list.add("a");
list.add("b");
list.add("c");
Boolean actual = list.contains("b");
//then
Assert.assertTrue(actual);

}

@Test
public void findTest(){
//given
SinglyLinkedList<String> list = new SinglyLinkedList<String>();
Integer expectedIndex = 2;
//when
list.add("a");
list.add("b");
list.add("c");
Integer actual = list.find("c");
//then
Assert.assertEquals(expectedIndex, actual);

}

@Test
public void getTest(){
//given
SinglyLinkedList<String> list = new SinglyLinkedList<String>();
String expected = "c";
//when
list.add("a");
list.add("b");
list.add("c");

String actual = list.get(2).data;
//then
Assert.assertEquals(expected, actual);
}

@Test
public void copyTest(){
//given
SinglyLinkedList<String> list = new SinglyLinkedList<String>();
Integer expectedSize = 3;
//when
list.add("a");
list.add("b");
list.add("c");
SinglyLinkedList<String> list2 = list.copy();
list.add("d"); //added after copy to test if it would be added to copy
Integer actual = list2.size();
//then
Assert.assertEquals(expectedSize, actual);

}

}