思路:(压栈时候采用头插法)
代码演示:
public class LinkedListStack {
private Person top = new Person(-1);
// 判断栈是否为空
public boolean isEmpty(){
return top.getNo() == -1;
}
// 入栈
public void push(Person person){
Person temp = top;
if(top.getNext() == null){// 第一个节点的插入
top.setNext(person);
return;
}
Person cur = top.getNext();
top.setNext(person);
person.setNext(cur);
}
// 出栈
public void pop(){
System.out.println("节点为:"+top.getNext().getNo());
top = top.getNext();
}
// 遍历
public void show(){
if(top.getNext() == null){
System.out.println("栈空。。。");
return;
}
Person temp = top;
while(temp.getNext() != null){
System.out.println("节点为:"+temp.getNext().getNo());
temp = temp.getNext();
}
}
}
定义节点:
public class Person {
private int no;
private Person next;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public Person getNext() {
return next;
}
public void setNext(Person next) {
this.next = next;
}
public Person(int no) {
this.no = no;
}
@Override
public String toString() {
return "Person{" +
"no=" + no +
'}';
}
}
测试类:
public static void main(String[] args) {
Person p1 = new Person(3);
Person p2 = new Person(1);
Person p3 = new Person(5);
LinkedListStack stack = new LinkedListStack();
stack.push(p1);
stack.push(p2);
stack.push(p3);
stack.show();
// stack.pop();
// stack.pop();
}