栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

数据结构-循环队列-简单范例(java)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构-循环队列-简单范例(java)

public class ArrayQueue {
    //队列容量
    public int size;
    //队列头部下标
    public int front;
    //队列尾部下标
    public int rear;
    //数组
    public int arr[];

    public ArrayQueue(int size){
        this.size = size + 1; //默认留一个空地址
        this.front = 0;
        this.rear = 0;
        this.arr = new int[size+1];
    }

    
    public boolean isFull(){
        return (rear+1) % size == front;
    }

    
    public boolean isEmpty(){
        return rear == front;
    }

    
    public void push(int item){
        if(isFull()){
            throw new RuntimeException("队列已满,入队失败");
        }
        arr[rear] = item;
        rear = (rear + 1) % size;
    }

    
    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        int item = arr[front];
        front = (front + 1) % size;
        return item;
    }

    
    public int length(){
        return (rear + size - front) % size;
    }

    
    public void show(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        for (int i = front; i < front+length(); i++) {
            System.out.print(arr[i%size]+"t");
        }
        System.out.printf("n");
    }

    public int getHeader(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return arr[front];
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/781490.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号