/**
*
* @ClassName: LinkedStack.java
* @Description: 栈的链接实现
* @Copyright: Copyright (c) 2017
* @author 周扬
* @date 2017年6月6日 下午4:57:19
* @version V1.0
*/
public class LinkedStack {
/**栈顶指针**/
private Node top;
/**
* @MethodName: push
* @Description: 入栈
* @param data
* @author 周扬
* @date 2017年6月6日 下午4:56:08
*/
public void push(Object data){
//新建节点
Node node=new Node();
node.setData(data);
//将栈顶节点放入到新节点的下一节点
node.setNextNode(top);
//设置新节点为栈顶节点
top=node;
}
/**
* @MethodName: pop
* @Description: 出栈
* @return
* @throws Exception
* @author 周扬
* @date 2017年6月6日 下午4:55:44
*/
public Object pop() throws Exception{
if(top==null){
throw new Exception("空栈");
}
Node node=top;
//获取栈顶节点存储的数据元素
Object data=node.getData();
//栈顶节点下移
top=node.getNextNode();
//释放原栈顶节点存储空间
node=null;
//返回元数据元素
return data;
}
/**
* @MethodName: getTop
* @Description: 获取栈顶元素
* @return
* @throws Exception
* @author 周扬
* @date 2017年6月6日 下午4:55:26
*/
public Object getTop() throws Exception{
if(top==null){
throw new Exception("空栈");
}
Node node=top;
//获取栈顶节点存储的数据元素
Object data=node.getData();
//返回元数据元素
return data;
}
/**
* @MethodName: empty
* @Description: 判断是否为空栈,是返回1,否返回0
* @return
* @author 周扬
* @date 2017年6月6日 下午4:55:15
*/
public int empty(){
if(top==null){
return 1;
}
return 0;
}
}
/**
*
* @ClassName: Node.java
* @Description: 节点类
* @Copyright: Copyright (c) 2017
* @author 周扬
* @date 2017年6月5日 上午10:53:54
* @version V1.0
*/
public class Node {
/**数据元素**/
private Object data;
/**下一个节点位置**/
private Node nextNode;
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNextNode() {
return nextNode;
}
public void setNextNode(Node nextNode) {
this.nextNode = nextNode;
}
}