java版数据结构学习总结之栈

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/luffysk/article/details/82154215

栈的定义

  • 只允许线性表在固定的一端进行插入和删除元素操作即为栈
  • 是一种后进先出的数据结构
  • 每个节点有且只有一个前驱节点和一个后继节点

使用线性存储结构实现

//Stack接口中定义了栈的公共操作方法
public class OrderStack implements Stack{
    private DataType[] stack;
    private int bottom; //栈底指针
    private int top;    //栈顶指针
    private int length;
    //构造长度为length的栈,默认栈顶为-1
    public  OrderStack(int length) throws Exception {
        if(length<1){
            throw new Exception("参数length不正确");
        }
        this.stack = new DataType[length];
        this.bottom = 0;
        this.top = -1;
        this.length = 0;
    }

    //销毁栈
    public void destoryStack() {
        for(int i=0; i<stack.length; i++){
            stack[i] = null;
        }
        stack = null;
    }

    //清空栈
    public void clearStack() {
        for(int i=0; i<length; i++){
            stack[i] = null;
        }
        this.bottom = 0; 
        this.top = -1;
        this.length = 0;
    }

    //判断栈是否为空
    public boolean isEmpty() {
        return this.length==0? true : false;
    }

    //压栈
    public void push(DataType ele) throws Exception {
        if(this.length == stack.length){
            throw new Exception("栈已满,无法插入");
        }
        stack[++this.top] = ele;
        this.length++;
    }

    //出栈
    public DataType pop() throws Exception {
        if(this.length == 0){
            throw new Exception("栈已空,无法弹出");
        }
        DataType ret = stack[this.top--];
        this.length--;
        return ret;
    }

    //获取栈的实际大小
    public int size() {
        return this.length;
    }
}

链式存储结构实现栈

//Stack接口中定义了栈的公共方法
public class LinkedStack implements Stack{
    private Node top;    //栈顶指针
    private Node bottom; //栈底指针
    //初始化
    public  LinkedStack() {
        bottom = new Node(new DataType(-1),null);
        top = new Node(new DataType(-1),bottom);
    }

    //销毁栈
    public void destoryStack() {
        top = null;
        bottom = null;
    }

    //清空栈
    public void clearStack() {
        Node tmp = null;
        while(top.next != bottom){
            tmp = top.next;
            top.next = tmp.next;
            tmp = null;
        }
    }

    //判断栈是否为空
    public boolean isEmpty() {

        return top.next==bottom? true : false;
    }

    //压栈
    public void push(DataType ele) {
        Node node = new Node(ele,null);
        node.next = top.next;
        top.next = node;

    }

    //出栈,将弹出的元素返回
    public DataType pop() throws Exception {
        if(top.next == bottom){
            throw new Exception("栈已空,无法弹出");
        }
        Node ret = top.next;
        top.next = ret.next;
        ret.next = null;
        return ret.dt;
    }

    //获取栈的实际容量
    public int size() {
        int len = 0;
        Node cur = top;
        while(cur.next != bottom){
            cur = cur.next;
            len++;
        }
        return len;
    }

}
//栈的节点
class Node{
    public DataType dt;
    public Node next;
    public Node(DataType dt, Node next){
        this.dt = dt;
        this.next = next;
    }
}

猜你喜欢

转载自blog.csdn.net/luffysk/article/details/82154215