Skip to content

Hello #33

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 5 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_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.

685 changes: 228 additions & 457 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.12</version>
<scope>test</scope>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,24 @@
* Created by leon on 1/10/18.
*/
public class MainApplication {
public static void main(String[] args) {
SinglyLinkedList doesThisWork = new SinglyLinkedList();
doesThisWork.addNode(10);
doesThisWork.addNode(9);
doesThisWork.addNode(2);
doesThisWork.addNode(146);
doesThisWork.addNode(1245);
doesThisWork.addNode(56);
doesThisWork.addNode(4);
doesThisWork.addNode(5);
doesThisWork.addNode(90);
doesThisWork.addNode(1);

doesThisWork.display();
System.out.println("\n" + "\n");

// doesThisWork.sortLeastToGreatest();
// doesThisWork.reverse().display();
doesThisWork.sortGreatestToLeast();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,183 @@
package com.zipcodewilmington.singlylinkedlist;


import sun.jvm.hotspot.StackTrace;

/**
* 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 Node head = null;
public Node tail = null;

public void addNode(Object data) {
Node newNode = new Node(data);

if(head == null) {
head = newNode;
} else {
tail.next = newNode;
}
tail = newNode;
}

public int size() {
int count = 0;
Node current = head;

while(current != null) {
count++;
current = current.next;
}
return count;
}

public void display() {
Node current = head;
checkListEmpty("There's no list, idiot.");
System.out.println("Nodes of singly linked list:");
while(current != null) {
System.out.println(current.data + "");
current = current.next;
}
}

// returning new SinglyLinkedList - there is a way to stitch two nodes together
public SinglyLinkedList remove(Object data) {
SinglyLinkedList result = new SinglyLinkedList();
Node current = head;
checkListEmpty("There's no list, dummy");
while(current != null) {
if(current.data != data) {
result.addNode(current.data);
}
current = current.next;
}
return result;
}

private void checkListEmpty(String s) {
if (head == null) {
throw new UnsupportedOperationException(s);
}
}

public Boolean contains(Object data) {
Node current = head;
checkListEmpty("This list empty, doood");
while(current != null) {
if(current.data == data) {
return true;
}
current = current.next;
}
// if(find(data).equals(data)) {
// return true;
// }
return false;
}

public int find(Object data) {
Node current = head;
int theIndex = 0;
if (head == null) {
System.out.println("List is empty");
}
while(current != null) {
if(current.data == data) {
return theIndex;
}
theIndex++;
current = current.next;
}
return -1;
}

public Object get(double index) {
Node current = head;
int theIndex = 0;
if (head == null || index > size() || index < 0) {
throw new UnsupportedOperationException("You doin' something wrong, buckaroo");
}
while(current != null) {
if (theIndex == index) {
return current.data;
}
theIndex++;
current = current.next;
}
return -1;
}

public SinglyLinkedList copy() {
SinglyLinkedList newList = new SinglyLinkedList();

Node current = head;
checkListEmpty("List don't be there, my guy");
while (current != null) {
newList.addNode(current.data);
current = current.next;
}
return newList;
}


public void sortLeastToGreatest() {
Node current = head;
checkListEmpty("Where's the car fax?");
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;
}
}
}
public SinglyLinkedList reverse () {
SinglyLinkedList resultList = new SinglyLinkedList();
Integer i = size() - 1;
checkListEmpty("You ain't got one of them lists, buckaroo");
while(i >= 0) {
resultList.addNode(get(i));
i--;
}
return resultList;
}

public SinglyLinkedList splice (Integer beginningIndex, Integer endingIndex) {
Node current = head;
Integer index = 0;
SinglyLinkedList result = new SinglyLinkedList();
checkListEmpty("Where's the car fax?");
while(current != null) {
if (index >= beginningIndex && index <= endingIndex) {
result.addNode(current.data);
}
index++;
current = current.next;
}
return result;
}

public void sortGreatestToLeast () {
sortLeastToGreatest();
reverse().display();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,57 @@
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 addNodeTest () {
SinglyLinkedList single = new SinglyLinkedList();
Integer expected = 3;

single.addNode(3);
single.addNode(2);
single.addNode(5);
Integer actual = single.size();

Assert.assertEquals(expected, actual);
}

@Test
public void sizeTest () {
SinglyLinkedList single = new SinglyLinkedList();
Integer expected = 0;
Integer actual = single.size();

Assert.assertEquals(expected, actual);
}

@Test
public void displayTest () {
SinglyLinkedList single = new SinglyLinkedList();
single.addNode(3);
single.addNode(2);
single.addNode(5);

// Visual test - display returns void
single.display();
}

@Test
public void removeTest() {
SinglyLinkedList test = new SinglyLinkedList();
test.addNode(3);
test.addNode(6);
test.addNode(4);
test.addNode(1);
test.addNode(8);

SinglyLinkedList actual = test.remove(4);

Assert.assertEquals(test.size() - 1, actual.size());
}
}