Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package linkedlist;
- import java.util.Comparator;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- import java.util.Spliterator;
- import java.util.function.Consumer;
- public class SimpleLinkedList<T extends Comparable<T>> implements ISimpleList<T>, IStack<T>, ISortable<T>, Iterable<T> {
- private Element<T> tail = new Element<>(null, null);
- private Element<T> head = new Element<>(tail, null);
- private int listCount = 0;
- @Override
- public void add(T toAdd) {
- Element lastInList = head.getLastValidElement();
- Element<T> newElement = new Element<T>(tail, toAdd);
- if(!lastInList.equals(tail) || !lastInList.equals(head)) {
- lastInList.setNext(newElement);
- } else {
- head.setNext(newElement);
- }
- listCount++;
- }
- @Override
- public void add(int index, T toAdd) throws IndexOutOfBoundsException {
- if(index < 0 || index > size()) throw new IndexOutOfBoundsException();
- // CurrentElement is our start and will get increased each loop iteration.
- Element currentElement = head;
- int counter = 0;
- while(currentElement.hasNext()){
- // Next Element is the ancessor of the current element
- // It replaces the current element at the end of the iteration.
- Element nextElement = currentElement.getNext();
- // Checks if the element is valid, only head and tail doesnt have data sets.
- if(counter == index){
- Element<T> newElement = new Element<T>(nextElement, toAdd);
- currentElement.setNext(newElement);
- listCount++;
- }
- currentElement = nextElement;
- counter++;
- }
- }
- @Override
- public void set(int index, T toSet) throws IndexOutOfBoundsException {
- remove(index);
- add(index, toSet);
- }
- @Override
- public void addAll(Iterable<T> collection) throws NullPointerException {
- for(T t : collection)
- add(t);
- }
- @Override
- public T remove(T toSearch) {
- int indexOf = indexOf(toSearch);
- if(indexOf <= 0) return null;
- return remove(indexOf);
- }
- @Override
- public T remove(int index) throws IndexOutOfBoundsException {
- if(index >= size() || index < 0) throw new IndexOutOfBoundsException();
- // CurrentElement is our start and will get increased each loop iteration.
- Element currentElement = head;
- int counter = 0;
- while(currentElement.hasNext()){
- // Next Element is the ancessor of the current element
- // It replaces the current element at the end of the iteration.
- Element nextElement = currentElement.getNext();
- // Checks if the element is valid, only head and tail doesnt have data sets.
- if(!nextElement.hasData()){ return null; }
- if(counter == index){
- currentElement.setNext(nextElement.getNext());
- listCount--;
- return (T)nextElement.getData();
- }
- currentElement = nextElement;
- counter++;
- }
- return null;
- }
- @Override
- public void clear() {
- this.head.setNext(tail);
- listCount = 0;
- }
- @Override
- public boolean contains(T toSearch) {
- // CurrentElement is our start and will get increased each loop iteration.
- Element currentElement = head;
- int counter = 0;
- while(currentElement.hasNext()){
- // Next Element is the ancessor of the current element
- // It replaces the current element at the end of the iteration.
- Element nextElement = currentElement.getNext();
- // Checks if the element is valid, only head and tail doesnt have data sets.
- if(!nextElement.hasData()){ return false; }
- if(nextElement.getData().equals(toSearch)){
- return true;
- }
- currentElement = nextElement;
- counter++;
- }
- return false;
- }
- @Override
- public int indexOf(T toFind) {
- // CurrentElement is our start and will get increased each loop iteration.
- Element currentElement = head;
- int counter = 0;
- while(currentElement.hasNext()){
- // Next Element is the ancessor of the current element
- // It replaces the current element at the end of the iteration.
- Element nextElement = currentElement.getNext();
- // Checks if the element is valid, only head and tail doesnt have data sets.
- if(!nextElement.hasData()){ return -1; }
- if(nextElement.getData().equals(toFind)){
- return counter;
- }
- currentElement = nextElement;
- counter++;
- }
- return -1;
- }
- @Override
- public T get(int index) throws IndexOutOfBoundsException, NoSuchElementException {
- if(index == size() && index <= 0) throw new NoSuchElementException();
- if(index < 0 || index >= size()) throw new IndexOutOfBoundsException();
- Element currentElement = head;
- int counter = 0;
- while(currentElement.hasNext()){
- Element nextElement = currentElement.getNext();
- if(nextElement.equals(tail)){ return null; }
- if(counter == index){ return (T)nextElement.getData(); }
- currentElement = nextElement;
- counter++;
- }
- return null;
- }
- @Override
- public T getFirst() {
- if(size() <= 0) throw new NoSuchElementException();
- return (T)head.getNext().getData();
- }
- @Override
- public T getLast() {
- if(size() <= 0) throw new NoSuchElementException();
- return (T)head.getLastValidElement().getData();
- }
- @Override
- public ISimpleList<T> subList(int fromIndex, int toIndex) throws IndexOutOfBoundsException, IllegalArgumentException {
- if(fromIndex > toIndex) throw new IllegalArgumentException();
- ISimpleList<T> list = new SimpleLinkedList<>();
- for(int index = fromIndex; index <= toIndex; index++) {
- T entry = get(index);
- list.add(entry);
- System.out.println(entry);
- }
- return list;
- }
- @Override
- public boolean isEmpty() {
- return size() <= 0;
- }
- @Override
- public int size() {
- return this.listCount;
- }
- //////////////////////////////////
- // Stack & Sort methods
- /////////////////////////////////
- @Override
- public void push(T toAdd) {
- add(size(), toAdd);
- }
- @Override
- public T pop() throws NoSuchElementException {
- if(size() <= 0) throw new NoSuchElementException();
- return remove(size()-1);
- }
- private Element<T> getElementAt(int index){
- Element currentElement = head;
- int counter = 0;
- while(currentElement.hasNext()){
- Element nextElement = currentElement.getNext();
- if(counter == index){ return nextElement; }
- currentElement = nextElement;
- counter++;
- }
- return null;
- }
- public void switchElements(int indexOne, int indexTwo){
- Element elementOne = getElementAt(indexOne);
- Element elementTwo = getElementAt(indexTwo);
- Element elementBeforeOne = getElementAt(indexOne-1);
- Element elementBeforeTwo = getElementAt(indexTwo-1);
- if(elementBeforeOne == null)
- elementBeforeOne = head;
- if(elementBeforeTwo != null){
- elementBeforeOne.setNext(elementTwo);
- elementBeforeTwo.setNext(elementOne);
- Element elementAfterOne = elementOne.getNext();
- elementOne.setNext(elementTwo.getNext());
- elementTwo.setNext(elementAfterOne);
- }
- }
- @Override
- public void sort() {
- sort(new Comparator<T>() {
- @Override
- public int compare(T o1, T o2) {
- return o1.compareTo(o2);
- }
- });
- }
- @Override
- public void sort(Comparator<? super T> c) {
- for(int index = 0; index < listCount; index++){
- Element<T> currentElement = getElementAt(index);
- for(int sindex = index; sindex < listCount; sindex++){
- Element<T> innerElement = getElementAt(sindex);
- if(currentElement.getData() == null || innerElement.getData() == null) continue;
- if( c.compare(currentElement.getData(), innerElement.getData()) > 0){
- switchElements(index, sindex);
- currentElement = getElementAt(index);
- }
- }
- }
- }
- //////////////////////////////////
- // Iterating methods
- /////////////////////////////////
- @Override
- public Iterator<T> iterator() {
- T[] array = (T[])new Comparable[size()];
- for (int index = 0; index < size(); index++)
- array[index] = get(index);
- return new ListIterator<T>(array);
- }
- @Override
- public void forEach(Consumer<? super T> action) {
- }
- @Override
- public Spliterator<T> spliterator() {
- return null;
- }
- }
- package linkedlist;
- public class Element<T> {
- private T data;
- private Element next = null;
- public Element(Element next, T data){
- this.data = data;
- this.next = next;
- }
- public T getData() {
- return data;
- }
- public boolean hasData(){ return getData() != null; }
- public void setData(T data) {
- this.data = data;
- }
- public boolean hasNext(){ return getNext() != null; }
- public Element getNext() {
- return next;
- }
- public void setNext(Element next) {
- this.next = next;
- }
- public Element getLastValidElement(){
- if(!next.hasNext()) {
- return this;
- }
- else return next.getLastValidElement();
- }
- }
- package linkedlist;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- import java.util.function.Consumer;
- public class ListIterator<T> implements Iterator<T> {
- private T[] array;
- private int counter = 0;
- public ListIterator(T[] array){
- this.array = array;
- }
- @Override
- public boolean hasNext() {
- return counter < array.length;
- }
- @Override
- public T next() {
- if(!hasNext()) throw new NoSuchElementException();
- T nextElement = array[counter];
- counter++;
- return nextElement;
- }
- @Override
- public void forEachRemaining(Consumer<? super T> action) {
- if(action == null) return;
- while (hasNext())
- action.accept(next());
- }
- @Override
- public void remove() {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement