Skip to content

finished #37

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

428 changes: 167 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>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
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 myList = new SinglyLinkedList();

myList.add(1);
myList.add(2);
myList.add(3);
myList.add(4);
myList.add(5);
myList.add(55);
myList.displayNodes();

System.out.println(myList.get(5).getData());




}

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

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

class Node {
T data;
Node next;

public Node(T data){
this.data = data;
this.next = null;
}

public T getData() {
return data;
}
}

public Node head;
public Node tail;

public SinglyLinkedList(){
this.head = null;
this.tail = null;
}
public void add(T data){
Node newNode = new Node(data);
if(head == null){
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
}

public void addHead(T data){
Node newNode = new Node(data);

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

public boolean contains(T data){
Node current = head;
while(current != null){
if(current.data.equals(data)){
return true;
}
current = current.next;
}
return false;
}

public void removeTail(){
Node current = head;
if(current == null){
return;
}
Node nextNode = current.next;
while(current.next != null){
if(nextNode.next == null){
tail = current;
current.next = null;
return;
}
current = nextNode;
nextNode = current.next;
}
}

public void removeHead(){
Node current = head;
if(current == null || current.next == null){
return;
}
if(head != null){
head = current.next;
}

}

public void remove(Integer index){
if(index < 0 || index > this.size()){
throw new UnsupportedOperationException("Index is out of bounds :(");
}
if(index == 0){
this.removeHead();
return;
}
if(index == (this.size() - 1)){
this.removeTail();
return;
} else {
Integer currentIndex = 0;
Node current = head;
while(currentIndex < (index - 1)){
current = current.next;
currentIndex++;
}
Node nextNode = current.next;
current.next = nextNode.next;
}
}

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

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

public Node get(Integer index){
if(index < 0 || index > (this.size() -1)){
throw new UnsupportedOperationException("Chosen index is outside the scope of this list! :(");
}
Node current = head;
Integer currentIndex = 0;
while(currentIndex != index){
current = current.next;
currentIndex++;
}
return current;
}

public void displayNodes() {
Node current = head;
if (head == null) {
System.out.println("List is empty");
return;
}
System.out.println("Nodes of singly linked list: ");
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}


public SinglyLinkedList<T> clone(){
SinglyLinkedList<T> cloned = new SinglyLinkedList<T>();
Node current = head;
while(current != null){
cloned.add(current.data);
current = current.next;
}
return cloned;
}

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

while(origin != null){
current = origin;
origin = origin.next;
current.next = prev;
prev = current;
head = current;
}
}

public void sort(){
Node current = head;
for (int i = 0; i < size(); i++) {
current = head;
while(current.next != null){
if(compareTo(current.getData(), current.next.getData()) > 0){
T nextValue = current.next.getData();
current.next.data = current.data;
current.data = nextValue;
}
current = current.next;
}
}

}

public Integer compareTo(T obj, T obj2){
return obj.compareTo(obj2);
}

public SinglyLinkedList<T> slice(Integer start, Integer end){
// SinglyLinkedList<T> cloned = this.clone();
// for (int i = 0; i < start; i++) {
// cloned.removeHead();
// }
// for (int i = this.size(); i > end; i--) {
// cloned.removeTail();
// }
SinglyLinkedList<T> cloned = new SinglyLinkedList<T>();
for (int i = 0; i < end; i++) {
if(i >= start){
cloned.add(get(i).getData());
}
}
return cloned;
}





}
Loading