堆栈的链式存储实现:
直接上代码:
栈类:
//仅仅是为了复习用,为了省时间,我把判断是否出界等功能省了
public class Stack{
int Len;
Node head;
public Stack() {
this.head=null;
this.Len=0;
}
public void Push(int data) {
Node node=new Node(data);
Node flag=null;
flag=this.head;
this.head=node;
node=flag;
this.head.next=node;
this.Len++;
}//压栈操作
public void Pull() {
System.out.println(this.head.data+"出栈");
this.head=this.head.next;
this.Len--;
}//出栈操作
public void Print() {
Node a;
a=this.head;
for(int i=this.Len;i>0;i--) {
System.out.println(a.data+" ");
a=a.next;
}
}//打印操作
}
节点类:
public class Node {
int data;
Node next;
public Node(int Data) {
this.data=Data;
this.next=null;
}
}