栈的链式实现

public class LinkStack<E> {
//内部类
private class Node {
public Node(E item) {
this.item = item;
}

private Node next;

private E item;
}

private Node node;
//判断栈是否为空
public boolean isEmpty() {
return node == null;
}

// 出栈操作,返回栈顶元素并删除
public E pop() {
if (node == null) {
return null;
} else {
E temp = node.item;
node = node.next;
return temp;
}
}

// 返回栈顶元素但并不删除
public E peek() {
if (node == null) {
return null;
} else {
return node.item;
}
}

// 入栈操作
public void push(E target) {
if (node == null) {
Node newnode = new Node(target);
node = newnode;
} else {
Node newnode = new Node(target);
newnode.next = node;
node = newnode;
}
}
}

你可能感兴趣的:(栈的链式实现)