栈作为被广泛使用的数据结构,是在一个特定范围的存储单元中存储的数据,这些数据可以重新被取出使用,与线性表相比,它们的插入和删除受到更多的约束和限定,所以又称为限定性的线性表结构。不同的是,栈是一个FILO结构,
下面是采用java链表的方式时实现栈

/**
* 定义一个链表的节点
*
* @author VicterTian
* @version V1.0
* @Date 2019/1/21
*/
class NodeForStack<E> {
NodeForStack<E> next = null;
E data;

NodeForStack(E data) {
this.data = data;
}
}
/**
* 通过链表实现栈
*
* @author VicterTian
* @version V1.0
* @Date 2019/1/21
*/
public class MyStackByLinkedList<E> {
private NodeForStack<E> top = null;

Boolean isEmpty() {
return top == null;
}

void push(E data) {
NodeForStack<E> newNode = new NodeForStack<>(data);
newNode.next = top;
top = newNode;
}

E pop() {
if (this.isEmpty()) {
return null;
}
E data = top.data;
top = top.next;
return data;
}
E peek(){
if (this.isEmpty()){
return null;
}
return top.data;
}
/**
* 测试
*/
public static void main(String[] args) {
MyStackByLinkedList<Integer> myStack = new MyStackByLinkedList<>();
myStack.push(1);
myStack.push(2);
System.out.println("myStack.peek() = " + myStack.peek());
System.out.println("myStack.pop() = " + myStack.pop());
System.out.println("myStack.pop() = " + myStack.pop());
}
}