栈是一种“后进先出”的数据结构,对栈的插入和删除操作都是在栈头位置进行的,这与在单链表的表头插入和删除元素的原理类似,因此可以用单向链表实现栈。
单链表实现栈的Java代码:
package parking;
import java.util.Stack;
class Node {
Object data;
Node next;
public Node(Object data) {
this.data = data;
}
}
class LinkNode {
private Node head;
private int size;
public LinkNode() {
this.head = null;
this.size = 0;
}
// 判断是否为空
public boolean isEmpty() {
return size == 0 ? true : false;
}
// 头插入法
public void addHNode(Node node) {
if (head == null) {
head = node;
} else {
node.next = head;
head = node;
}
size++;
}
// 输出头结点,不删除
public Object sysHNode() {
if (head == null) {
return null;
}
Object obj = head.data;
return obj;
}
// 输出头结点,并删除
public Object sysHnode() {
if (head == null) {
return null;
}
Object obj = head.data;
if (head.next == null) {
head = null;
} else {
head = head.next;
}
size--;
return obj;
}
public int getSize() {
// TODO Auto-generated method stub
return size;
}
}
public class LinkStack {
private LinkNode link;
LinkStack() {
link = new LinkNode();
}
// 判断是否为空
public boolean isEmpty() {
return link.isEmpty();
}
// 入栈
private void push(Object obj) {
Node node = new Node(obj);
link.addHNode(node);
}
// 出栈
private Object pop() {
return link.sysHnode();
}
// 出栈,但不删除头结点
private Object peek() {
return link.sysHNode();
}
// 获取栈大小
private int size() {
return link.getSize();
}
public static void main(String[] args) {
LinkStack stack = new LinkStack();
int i;
for (i = 0; i < 5; i++) {
stack.push(i);
}
System.out.println("栈的大小:" + stack.size());
System.out.println("栈顶元素:" + stack.peek());
while (!stack.isEmpty()) {
System.out.print(stack.pop() + "-->");
}
}
}