用数组实现队列和栈

队列的特点是先入先出(FIFO),用数组实现的时候,必须要用两个指针来表示头head和尾tail,因为添加数据是在尾部,取出数据是在头部,如果要简单的实现队列满了就不允许在添加数据或者队列空了不允许再取数,相对简单,添加数据的时候tail指针往后移动,取数据的时候head往后移动,当head和tail相等的时候,就不太好确定队列中到底是最后一个数了还是tail已经超过head一圈了,也就不好确定队列中的数据元素个数,那么只需要用另一个变量来保存数据的个数即可,直接看代码:下面的代码是循环利用了数组来实现队列的功能

public class Queue {
    private int[] arr;
    private int head;
    private int tail;
    //表示队列中的元素个数
    private int size;

    public Queue(int size) {
        this.size = 0;
        this.head = 0;
        this.tail = 0;
        this.arr = new int[size];
    }

    public void add(int val) {
        if (size == arr.length) {
            System.err.println("队列满了.....");
            return;
        }
        if (tail == arr.length) {
            tail = 0;
        }
        arr[tail++] = val;
        size++;
    }

    public int get() {
        if (size == 0) {
            System.err.println("队列没有数据");
            return Integer.MIN_VALUE;
        }
        if (head == arr.length) {
            head = 0;
        }
        size--;
        int index = head++;
        int result = arr[index];
        //出列之后置为0
        arr[index] = 0;
        return result;
    }

    public static void main(String[] args) {
        Queue queue = new Queue(5);
        queue.add(5);
        queue.add(4);
        queue.add(2);
        queue.add(3);
        queue.add(2);
        System.err.println(queue.get());
        System.err.println(queue.get());
        System.err.println(queue.get());
        System.err.println(queue.get());
        queue.add(6);
        System.err.println(queue.get());
    }
}

用数组实现栈相对来说简单的多,只需要一个指针即可:

public class Stack {
    private int[] arr;
    //栈的容量
    private int capacity;
    //指针
    private int index;

    public Stack(int capacity) {
        this.capacity = capacity;
        arr = new int[capacity];
        this.index = 0;
    }

    public void push(int val) {
        if (index == capacity) {
            System.err.println("栈满了。。。");
            return;
        } else {
            arr[index++] = val;
        }
    }

    public int pop() {
        if (index == 0) {
            return arr[index];
        }
        if (index < 0) {
            System.err.println("没有数据了....");
            return Integer.MIN_VALUE;
        }
        int tmp = --index;
        int result = arr[tmp];
        //出栈之后置为0
        arr[tmp] = 0;
        return result;
    }

    public static void main(String[] args) {
        Stack stack = new Stack(5);
        stack.push(5);
        stack.push(4);
        stack.push(3);
        stack.push(1);
        stack.push(2);
        System.err.println(stack.pop());
        System.err.println(stack.pop());
        System.err.println(stack.pop());
        System.err.println(stack.pop());
        System.err.println(stack.pop());
        stack.push(2);
        System.err.println(stack.pop());
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值