public class LinkedList { public Node head; public LinkedList(int length) { head = null; for (int i = 0; i < length; i ++) insert(i); } public LinkedList() { head = null; } public void clear() { head = null; } public void insert(int n) { Node current = new Node(n); current.next = head; head = current; } public void insert(Node n, int index) { Node previous, current; Node nnode = new Node(-10); nnode.next = head; previous = nnode; current = head; while (current != null && index > 0) { current = current.next; previous = previous.next; index --; } if (previous == nnode) { n.next = head; head = n; } else { previous.next = n; n.next = current; } } public void delete(int index) { int current; Node currentnode = head; for (current = index; current > 1; current --) currentnode = currentnode.next; if (currentnode == head) head = head.next; else currentnode.next = currentnode.next.next; } public Node getNode(int index) { Node nnode = new Node(-10); nnode.next = head; Node current = head; while(current.next != null && index > 0) { current = current.next; index --; } return current; } public int getVal(int index) { int current; Node currentnode = head; for (current = index; current > 0; current --) currentnode = currentnode.next; return currentnode.getVal(); } public void print() { System.out.println(); head.print(); } private void swap(Node pa, Node a, Node pb, Node b) { Node temp = b.next; pa.next = b; if (a.next != b) { b.next = a.next; pb.next = a; } else b.next = a; a.next = temp; } public void selectionSort() { Node current, a, previous, position; position = new Node(0); position.next = head; head = position; while (position.next != null) { current = previous = position.next; a = position.next; while(a != null) { if (a.getVal() < current.getVal()) { current = a; while(previous.next != current) previous = previous.next; } a = a.next; } if (current != previous) { Node t = position.next; swap(position, t, previous, current); } position = position.next; } head = head.next; } public void bubbleSort() { Node current, a, previous, position; position = new Node(0); position.next = head; head = position; while (position.next != null) { current = position.next; previous = position; a = current.next; while(a != null) { if (a.getVal() < current.getVal()) { Node temp = a.next; a.next = previous.next; previous.next = current.next; current.next = temp; previous = a; a = temp; } else { a = a.next; current = current.next; previous = previous.next; } } position = position.next; } Node head = head.next; }