Following the series about CS101 datastructures lets understand and implement a stack.
A Stack is a list on which items are added and remove in FIFO order (First In First Out).  Lets see a picture to better understand a Stack.

The most basic stack interface declares the following method:

  • Pop : Remove item from top of the stack and return.
  • Push : Add item to the top of the stack.

Here is my implementation of a stack which extends my linked list implementation

public class Stack<T> extends LinkedList<T>{

	/**
	 * returns the top object in the stack
	 * @return T
	 */
	public T peek() {
		return this.head.getData();
	}

	/**
	 * removes the top object from the stack
	 * @return T : the object removed
	 */
	public T pop() {
		return this.remove(this.getFirst());
	}

	/**
	 * add object to the top of the stack
	 * @param tData
	 */
	public void push(T tData) {
		this.addToFront(tData);
	}
}

Full Code: https://github.com/lineker/BackToBasics