public class LinkedList<T> {
//testing
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> lifo = null;
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) {
lifo = new LinkedList<>(data, lifo);
}
// peek method, return upper
public T peek() {
if (lifo == null)
return null;
else
return lifo.getData();
}
// pop method, return upper and remove
public T pop() {
T data = null; // empty condition
if (lifo != null) {
data = lifo.getData();
lifo = lifo.getPrevious(); // stack is overwritten with next item
}
return data; // pop always returns data of element popped
}
// 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() {
StringBuilder stackToString = new StringBuilder("[");
LinkedList<T> node = lifo; // start from the back
while (node != null)
{
stackToString.append(node.getData()); // append the database to output string
node = node.getPrevious(); // go to previous node
if (node != null)
stackToString.append(", ");
} // loop 'till you reach the beginning
stackToString.append("]");
return stackToString.toString();
}
}
public class Reverse {
public static void main(String[] args) {
// test stack with Integer wrapper class
Stack<Integer> stacks = new Stack<Integer>();
stacks.push(1); //adds a new node after the rear and moves the rear to the next node.
stacks.push(2);
stacks.push(3);
stacks.push(4);
stacks.push(5);
System.out.println(stacks.toString());
System.out.println(stacks.peek());
}
}
Reverse.main(null);