数组实现环形队列

数组实现环形队列,第1张

实现思路:
  1. front 表示指向队列第一个元素,初始化为0。

  2. rear 表示指向队列最后一个元素的后一个位置,初始化为0。

  3. 数组实现环形队列需要预留一个空位置(不放元素)。

  4. 当队列已满时,满足:(rear + 1) % capacity == front

  5. 当队列为空时,满足:(rear + capacity - front) % capacity == 0

  6. 当出队列时,front变化满足front = (front + 1) % capacity

  7. 队列元素个数等于(rear + capacity - front) % capacity

完整代码
public class CircleArray {
    private int front;
    private int rear;
    private int capacity;
    int []queue;

    //构造器
    public CircleArray(){
        front = 0;
        rear = 0;
        capacity = 10;
        queue = new int[capacity];
    }
    public CircleArray(int capacity){
        front  = 0;
        rear = 0;
        this.capacity = capacity;
        queue = new int[capacity];
    }
    //判断队列是否已满
    public boolean isFull(){
        return (rear + 1) % capacity == front;
    }

    //判空
    public boolean isEmpty(){
        return (rear + capacity - front) % capacity == 0;
    }

    //入队列
    public void queueAdd(int x){
        if(isFull()){
            throw new RuntimeException("队列已满,不能入队列!");
        }
        queue[rear] = x;
        //会空一个位置
        rear = (rear + 1) % capacity;
    }

    //取数据
    public int queueFront(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,无法取数据!");
        }
        return queue[front];
    }
    //出队列
    public int queuePop(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,不能出队列!");
        }
        int value = queue[front];
        front = (front + 1) % capacity;
        return value;
    }
    //获取队列有效数据的个数
    public int queueSize(){
        return (rear + capacity - front) % capacity;
    }

    //显示队列数据
    public void showQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,无法显示数据!");
        }
        int tmp = front;
        for(int i = 0; i < queueSize(); i++){
            System.out.print(queue[tmp] + " ");
            tmp = (tmp + 1) % capacity;
        }
    }
}
代码测试

测试代码:

class Test01{
    public static void main(String[] args) {
        CircleArray c = new CircleArray(5);
        System.out.println("a:isFull");
        System.out.println("b:isEmpty");
        System.out.println("c:queueAdd");
        System.out.println("d:queueFront");
        System.out.println("e:queuePop");
        System.out.println("f:queueSize");
        System.out.println("g:showQueue");
        Scanner scanner  = new Scanner(System.in);
        while(true){
            char k = scanner.next().charAt(0);
            switch(k){
                case 'a':
                    System.out.println(c.isFull());
                    break;
                case 'b':
                    System.out.println(c.isEmpty());
                    break;
                case 'c':
                    try{
                        c.queueAdd(scanner.nextInt());
                    }catch(RuntimeException e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'd':
                    try{
                        System.out.println("对头数据为:" + c.queueFront());
                    }catch(RuntimeException e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    try{
                        System.out.println(c.queuePop() + "已被出队列!");
                    }catch(RuntimeException e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'f':
                    System.out.println(c.queueSize());
                    break;
                case 'g':
                        try{
                            c.showQueue();
                        }catch(RuntimeException e){
                            System.out.println(e.getMessage());
                        }
                        break;
                default:
                        break;


            }
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/904559.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-15
下一篇 2022-05-15

发表评论

登录后才能评论

评论列表(0条)

保存