从零开始学数据结构与算法--3.栈和队列


模仿栈结构写一个数组

主要模仿栈的先进后出和后进先出



public class MyStack {
    //低层实现是一个数组
    private long[] arr;
    private int top;

    /**
     * 默认的构造方法
     */

    public MyStack() {
        this.arr = new long[10];
        this.top = -1;
    }

    /**
     * 带参数的构造方法,参数为数组初始化大小
     */
    public MyStack(int maxsize) {
        this.arr = new long[maxsize];
        this.top = -1;
    }

    /**
     * 添加数据
     */
    public void push(int value) {
        arr[++top] = value;
    }

    /**
     * 移除数据
     */
    public long pop() {
        return arr[top--];
    }

    /**
     * 查看数据
     */
    public long peek() {
        return arr[top];
    }

    /**
     * 判断是否为空
     */
    public boolean isEmpty() {
        return top == -1;
    }

    /**
     * 判断是否满了
     */

    public boolean isFull() {
        return top == arr.length -1;
    }
}
 
 
    public static void main(String[] args) {
        MyStack myStack = new MyStack(4);
        myStack.push(23);
        myStack.push(12);
        myStack.push(1);
        myStack.push(90);
        System.out.println(myStack.isEmpty());
        System.out.println(myStack.isFull());
        System.out.println(myStack.peek());

        while (!myStack.isEmpty()) {
            System.out.println(myStack.pop() + "");
        }

        System.out.println(myStack.isEmpty());
        System.out.println(myStack.isFull());
    }
}

//显示结果
false
true
90
90
1
12
23
true
false

模仿队列写一个数组类

/**
 * 列队类
 */
public class MyCycleQueue {

    //底层使用数组
    private long[] arr;
    //有效数据的大小
    private int elements;
    //队头
    private int front;
    //队尾
    private int end;

    /**
     * 默认构造方法
     */
    public MyCycleQueue() {
        arr = new long[10];
        elements = 0;
        front = 0;
        end = -1;
    }

    /**
     * 带参数的构造方法,参数为数组的大小
     */
    public MyCycleQueue(int maxsize) {
        arr = new long[maxsize];
        elements = 0;
        front = 0;
        end = -1;
    }

    /**
     * 添加数据,从队尾插入
     */
    public void insert(long value) {
        if (end == arr.length - 1) {
            end = -1;
        }
        arr[++end] = value;
        elements++;
    }

    /**
     * 删除数据,从队头删除
     */
    public long remove() {
        if (front == arr.length) {
            front = 0;
        }
        long value = arr[front++];
        elements--;
        return value;
    }

    /**
     * 查看数据,从队头查看
     */
    public long peek() {
        return arr[front];
    }

    /**
     * 判断是否为空
     */
    public boolean isEmpty() {
        return elements == 0;
    }

    /**
     * 判断是否满了
     */
    public boolean isFull() {
        return elements == arr.length;
    }
}
public static void main(String[] args) {
//        MyQueue mq = new MyQueue(4);
        MyCycleQueue mq = new MyCycleQueue(4);
        mq.insert(23);
        mq.insert(45);
        mq.insert(13);
        mq.insert(1);

        System.out.println(mq.isFull());
        System.out.println(mq.isEmpty());
        //队头数据
        System.out.println(mq.peek());

        while (!mq.isEmpty()) {
            System.out.print(mq.remove() + " ");
        }

        mq.insert(23);
        mq.insert(13);
        System.out.println();

        while (!mq.isEmpty()) {
            System.out.print(mq.remove() + " ");
        }
    }

得出结果

true
false
23
23 45 13 1 
23 13

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值