Skip to content

completed all tests, including optional features. My God, just use an… #34

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

432 changes: 173 additions & 259 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
Expand Up @@ -3,5 +3,172 @@
/**
* Created by leon on 1/10/18.
*/
public class SinglyLinkedList {
public class SinglyLinkedList<T extends Comparable<T>> {



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 addNode(T data){
Node newNode = new Node(data);

if(head == null) {
head = newNode;
} else {
tail.next = newNode;
}
tail = newNode; //on the first iteration, head and tail mean the same thing
}


public Integer size(){
Integer count = 0;
Node current = this.head;
count = current.next == null ? 0 : 1;
while(current.next != null){
count++;
current = current.next;
}
return count;
}

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

public Boolean contains(T valueToFind){
return this.find(valueToFind) >= 0;
}


public void remove(T valueToFind){
Node current = head;
Node last = head;
while(current != null){
if(current.data.equals(valueToFind))
if(current.next == null) {
last.next = null;
} else if(current == last){
head = current.next;
}
else {
last.next = current.next;
}
last = current;
current = current.next;

}
}

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;
}
}

public T get(Integer index){
Node current = head;
Integer currentIndex = 0;
if(index >= size())
return null;
if(index == size() - 1)
return (T)tail.data;
for(int i = 0; i < index; i++){
current = current.next;
}
return (T)current.data;
}

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

while(current != null){
copiedList.addNode((T)current.data);
current = current.next;
}
return copiedList;
}

public void set(Integer index, T valueToSet){
Node current = head;

for(int i = 0; i < index; i++){
current = current.next;
}
current.data = valueToSet;
}
//10 1
//15
//2
//1
public void sort(){
Node current = head;
for(int i = 0; i < size(); i++){
current = head;
while(current.next != null){
if(compareTo((T)current.data, (T)current.next.data) > 0){
T nextVal = (T)current.next.data;
current.next.data = current.data;
current.data = nextVal;
}
current = current.next;
}
}
}

public SinglyLinkedList<T> slice(int start, int end){
SinglyLinkedList<T> subList = new SinglyLinkedList<T>();

for(int i = 0; i < end; i++){
if(i >= start)
subList.addNode(get(i));
}
return subList;
}

public void reverse(){
Node orig = head;
Node prev = null;
Node current = null;

while(orig != null) {
current = orig;
orig = orig.next;

current.next = prev;
prev = current;
head = current;
//this.display();
}
}

public int compareTo(T o, T o2) {
return o.compareTo(o2);
}
}
Loading