Last active
August 3, 2016 17:51
-
-
Save RonaldoMPF/bbfb89644cc9d082a7277b289e2c5582 to your computer and use it in GitHub Desktop.
SingleLinkedList (not tested)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Arrays; | |
| public class SingleLinkedList<T> { | |
| private SingleLinkedListNode<T> head; | |
| private int size; | |
| public SingleLinkedList(){ | |
| this.head = new SingleLinkedListNode<T>(); | |
| size = 0; | |
| } | |
| public boolean isEmpty(){ | |
| return this.head.isNIL(); | |
| } | |
| public void addLast(T element){ | |
| SingleLinkedListNode<T> newNode = new SingleLinkedListNode<T>(element, null); | |
| if(this.head.isNIL()){ | |
| newNode.setNext(head); | |
| this.head = newNode; | |
| }else{ | |
| SingleLinkedListNode<T> aux = head; | |
| while(!(aux.getNext().isNIL())){ | |
| aux = aux.getNext(); | |
| } | |
| newNode.setNext(aux.getNext()); | |
| aux.setNext(newNode); | |
| } | |
| this.size++; | |
| } | |
| public SingleLinkedListNode<T> search(T element){ | |
| SingleLinkedListNode<T> aux = head; | |
| while(!aux.isNIL() && aux.getData() != element){ | |
| aux = aux.getNext(); | |
| } | |
| return aux; | |
| } | |
| public boolean remove(T element){ | |
| if(isEmpty()){ | |
| return false; | |
| }else if(head.getData().equals(element)){ | |
| head = head.getNext(); | |
| this.size--; | |
| return true; | |
| }else{ | |
| SingleLinkedListNode<T> previous = null; | |
| SingleLinkedListNode<T> aux = head; | |
| while(!aux.isNIL() && !(aux.getData().equals(element))){ | |
| previous = aux; | |
| aux = aux.getNext(); | |
| } | |
| if(aux.isNIL()){ | |
| return false; | |
| }else{ | |
| previous.setNext(aux.getNext()); | |
| this.size--; | |
| return true; | |
| } | |
| } | |
| } | |
| public int size(){ | |
| return this.size; | |
| } | |
| public T[] toArray(){ | |
| @SuppressWarnings("unchecked") | |
| T[] array = (T[]) new Object[this.size]; | |
| SingleLinkedListNode<T> aux = head; | |
| int index = 0; | |
| while(!aux.isNIL()){ | |
| array[index] = aux.getData(); | |
| aux = aux.getNext(); | |
| index++; | |
| } | |
| return array; | |
| } | |
| } | |
| public class SingleLinkedListNode<T> { | |
| private T data; | |
| private SingleLinkedListNode<T> next; | |
| public SingleLinkedListNode(){ | |
| } | |
| public SingleLinkedListNode(T data, SingleLinkedListNode<T> next){ | |
| this.data = data; | |
| this.next = next; | |
| } | |
| public boolean isNIL(){ | |
| return (this.data == null); | |
| } | |
| public T getData() { | |
| return data; | |
| } | |
| public SingleLinkedListNode<T> getNext() { | |
| return next; | |
| } | |
| public void setNext(SingleLinkedListNode<T> next) { | |
| this.next = next; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment