栈—数组实现、链表实现

栈是很多高级数据结构和算法的基础,比如集合的Stack...

以下demo基本包含了栈的基本方法:

栈-数组实现 ArrayStack.java

/**
 * @Author: ltx
 * @Description: 栈-数组实现
 */
public class ArrayStack {
    private int[] arr;//数组
    private int maxSize;//栈大小
    private int size;//栈元素数量
    private int index;//栈顶位置

    /**
     * 构造方法-初始化
     *
     * @param maxSize
     */
    public ArrayStack(int maxSize) {
        this.arr = new int[maxSize];
        this.maxSize = maxSize;
        this.index = -1;
    }

    /**
     * 栈是否满了
     *
     * @return
     */
    private Boolean isFull() {
        return size == maxSize;
    }

    /**
     * 判空
     *
     * @return
     */
    private Boolean isEmpty() {
        return size == 0;
    }

    /**
     * 加元素
     *
     * @param value
     */
    private void push(int value) {
        if (isFull()) {
            System.out.println("栈满了...");
            return;
        }
        arr[++index] = value;
        size++;
    }

    /**
     * 取元素
     *
     * @return
     */
    private int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈空...");
        }
        size--;
        return arr[index--];
    }

    /**
     * 打印全部元素
     */
    private void show() {
        for (int i = 0; i < index + 1; i++) {
            System.out.print(arr[i] + ", ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(5);
        arrayStack.push(2);
        arrayStack.push(1);
        arrayStack.push(5);
        arrayStack.push(3);
        arrayStack.push(4);
        arrayStack.push(7);
        arrayStack.show();
        System.out.println("移除元素: " + arrayStack.pop());
        System.out.println("移除元素: " + arrayStack.pop());
        System.out.println("移除元素: " + arrayStack.pop());
        arrayStack.push(8);
        arrayStack.push(9);
        arrayStack.show();
    }
}

栈-链表实现 LinkedStack.java

/**
 * 节点
 */
class SignLinkedNode implements Serializable {
    public Integer value;
    //下一个节点
    public SignLinkedNode next;

    public SignLinkedNode() {
    }

    public SignLinkedNode(Integer value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return String.valueOf(value);
    }
}

/**
 * @Author: ltx
 * @Description: 栈-链表实现
 */
public class LinkedStack {
    //头指针
    SignLinkedNode head;
    //尾指针
    SignLinkedNode rear;
    //栈实际长度
    int size;
    //最大长度
    int maxSize;

    /**
     * 构造方法-初始化
     *
     * @param maxSize
     */
    public LinkedStack(int maxSize) {
        this.maxSize = maxSize;
    }

    /**
     * 栈是否满了
     *
     * @return
     */
    private Boolean isFull() {
        return size == maxSize;
    }

    /**
     * 空栈
     *
     * @return
     */
    private Boolean isEmpty() {
        return size == 0;
    }

    /**
     * 压入数据
     *
     * @param node
     */
    private void push(SignLinkedNode node) {
        if (isFull()) {
            System.out.println("栈满了...");
            return;
        }
        //第一个元素
        if (head == null) {
            head = node;
            rear = node;
        } else {
            rear.next = node;
            rear = node;
        }
        size++;
    }

    /**
     * 取出数据
     *
     * @return
     */
    private SignLinkedNode pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈空...");
        }
        SignLinkedNode temp = head;
        while (temp.next != rear) {
            temp = temp.next;
        }
        //尾指针往前走一下
        rear = temp;
        SignLinkedNode res = rear.next;
        //置空,方便gc回收后面的内存
        rear.next = null;
        size--;
        return res;
    }

    /**
     * 遍历栈元素
     */
    private void show() {
        SignLinkedNode temp = head;
        while (temp != null) {
            System.out.print(temp + ", ");
            temp = temp.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        LinkedStack arrayQueue = new LinkedStack(5);
        arrayQueue.push(new SignLinkedNode(2));
        arrayQueue.push(new SignLinkedNode(1));
        arrayQueue.push(new SignLinkedNode(5));
        arrayQueue.push(new SignLinkedNode(3));
        arrayQueue.push(new SignLinkedNode(4));
        arrayQueue.push(new SignLinkedNode(7));
        arrayQueue.show();
        System.out.println("移除元素: " + arrayQueue.pop());
        System.out.println("移除元素: " + arrayQueue.pop());
        System.out.println("移除元素: " + arrayQueue.pop());
        arrayQueue.push(new SignLinkedNode(8));
        arrayQueue.push(new SignLinkedNode(9));
        arrayQueue.show();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小绿豆

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值