第一天:队列

下面又三种:一种是含有统计元素个数的循环队列,第二种是不含有统计个数的循环队列;至于不循环的队列在第三种中

 

第一种:

复制代码
//有数据项计数字段的队列实现
class Queue{
    private int size;
    private Object[] data = null;
    private int head;
    private int tail;
    private int count;
    
    public Queue(int s){
        size = s;
        data = new Object[size];
        head = 0;
        tail = -1;
        count = 0;
    }
    
    public void insert(Object ele) throws Exception{
        //首先要判断元素是否已满,可否再添加
        if(isFull()){
            throw new Exception("空间以满,无法再添加了");
        }else{
            //添加元素,但添加元素前为了,让其形成循环队列,以免浪费空间,所以首先判断,tail是否到达了尾端
            if(tail == size -1){
                tail = -1;
            }
            data[++tail] = ele;
            count++;
        }
    }
    
    public boolean isFull(){
        return count == size;
    }
    
    public Object remove() throws Exception{
        //删除前判断是否为空
        if(isEmpty()){
            throw new Exception("元素已经空了,没有可以删除的元素了");
        }else{
            Object temp = data[head++];
            
            //判断删除的head的指标是否已经到达了顶部,让其循环
            if(head == size){
                head = 0;
            }
            count--;
            return temp;
        }    
    }
    
    public boolean isEmpty(){
        return count == 0;
    }
    
    public int getSize(){
        return count;
    }
    
    public Object peekHead(){
        return data[head];
    }
}
复制代码

 

第二种:

 

复制代码
//没有数据项计数字段队列操作
class Queue2{
    private int size;
    private Object[] data = null;
    private int head;
    private int tail;
    
    public Queue2(int s){
        size = s+1;
        data = new Object[size];
        head = 0;
        tail = -1;
    }
    
    public void insert(Object ele){
        //判读是否已满
        if(isFull()){
            System.out.println("元素已满,不能再添加");
            return;
        }else{
            //添加元素,但添加元素前为了,让其形成循环队列,以免浪费空间,所以首先判断,tail是否到达了尾端
            if(tail == size -1){
                tail = -1;
            }
            data[++tail] = ele;
        }
    }
    
    public boolean isFull(){
        return ( tail+2 == head || (head+size-2 == tail));
    }
    
    public Object remove(){
        //删除前判断是否为空
        if(isEmpty()){
            System.out.print("元素已经空了,没有可以删除的元素了");
            return null;
        }else{
            Object temp = data[head++];
            //判断删除的head的指标是否已经到达了顶部,让其循环
            if(head == size){
                head = 0;
            }
            return temp;
        }    
    }
    
    public boolean isEmpty(){
        return ( tail+1 == head || (head+size-1 == tail));
    }
    
    public int getSize(){
        if(tail >= head){
            return tail-head+1;
        }else{
            return (size-head)+(tail+1);
        }
    }
    
    public Object peekHead(){
        return data[head];
    }
}
复制代码

 

测试代码:

复制代码
public class TestDemo01 {
    public static void main(String args[]) throws Exception{
        Queue2 q = new Queue2(3);
        q.insert(1);
        q.insert(2);
        q.insert(3);
        //q.insert(4);
        
        q.remove();
        q.remove();
        
        
        //q.insert(4);
        q.insert(4);
        q.insert(5);
        //q.insert(6);
        //System.out.println(q.isEmpty());
        while(!q.isEmpty()){
            Object temp = q.remove();
            System.out.print(temp);
            System.out.print(" ");
        }
        
    }
}
复制代码

 

第三种:

复制代码
class Queue{
    private int size;
    private Object[] data;
    private int head;
    private int tail;
    
    public Queue(int initSize){
        size = initSize;
        data = new Object[size];
        head = 0;
        tail = 0;
    }
    
    public void insert(Object ele) throws Exception{
        //首先判断元素是否满了,不采用循环的方式
        if(tail == size){
            throw new Exception("元素添加已经满了,没法再添加");
        }
        data[tail++] = ele;
    }
    
    public Object remove() throws Exception{
        //判读元素是否已空,//没有循环
        if(tail == head){
            throw new Exception("元素已经空了");
        }
        Object temp = data[head++];
        if(head == size){
            throw new Exception("元素已经到底了");
            
        }
        return temp;
    }
}

public class TestDemo05 {
    public static void main(String args[]) throws Exception{
        Queue queue = new Queue(3);
        queue.insert(1);
        queue.insert(2);
        queue.insert(3);
        queue.remove();
        queue.insert(4);
        
    }
}
复制代码

posted on   pony1223  阅读(184)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示