A linked list implementation in Java. Acts as a stack, allows to push and pop elements, as well reversing the list and searching and deleting an element. package ch.ethz.da.serie05; /** * A simple stack based on a single linked list. * * @author Moritz Hoffmann * * @param * The list data type. */ public class MyList { private static class Element { private D data; private Element next; } Element head; public MyList() { head = null; } public boolean contains(D data) { Element current = head; while (current != null) { if (current.data.equals(data)) { return true; } current = current.next; } return false; } public void delete(D data) { if (head != null) { if (head.data.equals(data)) { // Kopf loeschen head = head.next; } else { // Element nach Kopf loeschen Element current = head; while (current.next != null) { if (current.next.data.equals(data)) { current.next = current.next.next; break; } current = current.next; } } } } public void insert(D data) { Element e = new Element(); e.data = data; e.next = head; head = e; } public D pop() { D data = null; if (head != null) { data = head.data; head = head.next; } return data; } public void reverse() { Element current = head; Element temporary = null; Element last = null; while (current != null) { temporary = current.next; current.next = last; last = current; current = temporary; } head = last; } @Override public String toString() { StringBuilder sb = new StringBuilder("["); Element current = head; while (current != null) { sb.append(current.data); current = current.next; if (current != null) { sb.append(", "); } } return sb.append("]").toString(); } }