Sorted Linked Lists
Sorted Linked Lists
public class LinkedList<T> {
private T data;
private LinkedList<T> prevNode, nextNode;
/*
* Constructs a new element
* @param data, data of object
* @param node, previous node
*/
public LinkedList(T data, LinkedList<T> node) {
this.setData(data);
this.setPrevNode(node);
this.setNextNode(null);
}
/*
* Clone an object,
* @param node object to clone
*/
public LinkedList(LinkedList<T> node) {
this.setData(node.data);
this.setPrevNode(node.prevNode);
this.setNextNode(node.nextNode);
}
/*
* Setter for T data in DoubleLinkedNode object
* @param data, update data of object
*/
public void setData(T data) {
this.data = data;
}
/*
* Returns T data for this element
* @return data associated with object
*/
public T getData() {
return this.data;
}
/*
* Setter for prevNode in DoubleLinkedNode object
* @param node, prevNode to current Object
*/
public void setPrevNode(LinkedList<T> node) {
this.prevNode = node;
}
/*
* Setter for nextNode in DoubleLinkedNode object
* @param node, nextNode to current Object
*/
public void setNextNode(LinkedList<T> node) {
this.nextNode = node;
}
/*
* Returns reference to previous object in list
* @return the previous object in the list
*/
public LinkedList<T> getPrevious() {
return this.prevNode;
}
/*
* Returns reference to next object in list
* @return the next object in the list
*/
public LinkedList<T> getNext() {
return this.nextNode;
}
}
public class Stack<T> {
private LinkedList<T> upper;
private int size;
// constructor initiates null LinkedList<T> object + set size to 0
public Stack() {
this.upper = null;
this.size = 0;
}
// push method for a new element to the upper value
public void push(T data) {
LinkedList<T> newNode = new LinkedList<T>(data, this.upper);
this.upper = newNode;
this.size++;
}
// peek method, return upper
public T peek() {
// try/catch to either return upper or print message if upper doesn't exist
try {
return this.upper.getData();
} catch (NullPointerException e) {
System.out.println("No upper element, empty stack!");
return null;
}
}
// pop method, return upper and remove
public T pop() {
// try/catch to either return + pop upper or print message if upper doesn't exist
try {
T data = this.upper.getData();
this.upper = this.upper.getPrevious();
this.size--;
return data;
} catch (NullPointerException e) {
System.out.println("No upper element, empty stack!");
return null;
}
}
// get size method
public int size() {
return this.size;
}
// isEmpty method, compare size to 0
public boolean isEmpty() {
return this.size == 0;
}
// toString method, from top to bottom
public String toString() {
String x = "[ ";
LinkedList<T> currentNode = upper;
// gets upper node, then keeps going down to previous until previous is null
while (currentNode != null) {
x += currentNode.getData();
currentNode = currentNode.getPrevious();
if (currentNode != null) {
x += ", ";
}
}
x += " ]";
return x;
}
public void bubbleSort() {
// if size is 0 or 1, don't sort
if (this.size <= 1) {
return;
}
// create a new stack to hold sorted values
Stack<T> sorted = new Stack<T>();
while (!this.isEmpty()) {
// empty stack by popping
T temp = this.pop();
// checks if temp is smaller than the top of sorted
while (!sorted.isEmpty() && ((Comparable<T>) sorted.peek()).compareTo(temp) > 0) {
// pop from sorted and push
this.push(sorted.pop());
}
// push temp into sorted
sorted.push(temp);
}
// if sorted still has elements, pop and push to this
while (!sorted.isEmpty()) {
this.push(sorted.pop());
}
}
}
public class Tester {
public static void main(String[] args) {
Stack<Integer> x1 = new Stack<Integer>();
// add objects to queue and print both
x1.push(15);
x1.push(5);
x1.push(20);
x1.push(55);
x1.push(10);
System.out.println("\033[1m"+"String - Unsorted: "+"\033[0m" + x1.toString());
x1.bubbleSort();
System.out.println("\033[1m"+"String - Sorted: "+"\033[0m" + x1.toString());
}
}
Tester.main(null);