Skip to content

SinglyLinkedList lab complete #36

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

414 changes: 153 additions & 261 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,148 @@
/**
* Created by leon on 1/10/18.
*/
public class SinglyLinkedList {
public class SinglyLinkedList<T extends Comparable<T>> {


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

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

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

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

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


public SinglyLinkedList<T> copy() {
SinglyLinkedList<T> newList = new SinglyLinkedList<T>();
Node temp = head;
while(temp != null) {
newList.add(temp.data);
temp = temp.next;
}
return newList;
}

public void sort() {
Node current = head;

for(int i = 0; i < size(); i++) {
current = head;
while(current.next != null) {
if(current.data.compareTo(current.next.data) > 0) {
T temp = current.next.data;
current.next.data = current.data;
current.data = temp;
}
current = current.next;
}
}
}

public void reverse() {
Node original = head;
Node previous = null;
Node current = null;

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

current.next = previous;
previous = current;
head = current;
}
}

public SinglyLinkedList<T> slice(Integer startIndex, Integer endIndex) {
SinglyLinkedList<T> slicedList = new SinglyLinkedList<T>();

for(int i = 0; i <= endIndex; i++) {
if(i >= startIndex) {
slicedList.add(get(i));
}
}
return slicedList;
}

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