堆栈

帅比萌擦擦* 提交于 2020-01-26 17:16:42

堆栈的链式存储实现:

直接上代码:

栈类:

//仅仅是为了复习用,为了省时间,我把判断是否出界等功能省了
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;
 	}
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!