Java基础:数据结构,实现简单的循环队列

本文介绍了一个简单的循环队列实现,包括队列的基本操作如入队和出队,并讨论了如何判断队列为空或已满的状态。

/**
 * 
 * 循环队列  FIFO  first in first out
 * @author liuhaibing
 *
 */
public class Queue {
    
    // 
    private int head;
    private int end;
    private int size;
    private int[] queue;
    /**
     * 初始化一个size大小的空队列
     * @param size
     */
    public Queue(int size) {
        this.size = size;
        queue = new int[size + 1]; // 必须定义size + 1个空间
        head = end = 0; // head == end 队列为空
    }
    
    public boolean isEmpty() {
        if(head == end) {
            return true;
        }
        return false;
    }
    
    public boolean isFull() {
        int next = 0;  // 如果end= size-1
        // 特殊情况,end在数组的最后一个元素
        if(end == size) {
            next = 0;
        }else {
            next = end + 1;
        }
        
        if(next == head) { // 如果尾的下一个是head,说明队列满了
            return true;
        }else {
            return false;
        }
    }

    public void offer(int data) {
        int next = 0;
        if(!isFull()) {
            if(end == size) {
                next = 0;
            }else {
                next = end + 1;
            }
            queue[end] = data;
            end = next;
        }else {
            throw new IllegalStateException("队列已满");
        }
    }
    
    public int poll() {
        if(!isEmpty()) {
            // 取数据
            int data = queue[head];
            if(head == size) {
                head = 0;
            }else {
                head ++;
            }
            
            return data;
            
        }else {
            throw new IllegalStateException("队列是空!");   // Alt + /
        }
    }
    
    

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Queue queue = new Queue(5);
        queue.offer(1); // 入队
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);
        queue.offer(5);
         // queue.offer(6);
        
//        System.out.println(queue.poll());  // poll出队
//        queue.offer(10);
//        // queue.offer(11);
//        System.out.println(queue.poll()); // 2 
//        queue.offer(11);
//        System.out.println(queue.poll()); // 3
//        queue.offer(12);
//        System.out.println(queue.poll()); // 4
//        queue.offer(13);
//        System.out.println(queue.poll()); // 5
//        queue.offer(14);
//        System.out.println(queue.poll()); // 10
//        System.out.println(queue.poll()); // 11
//        System.out.println(queue.poll()); // 12
//        System.out.println(queue.poll()); // 13
//        System.out.println(queue.poll()); // 14
//        
//        queue.offer(15);
//        System.out.println(queue.poll()); // 14
//        
//        queue.offer(16);
//        System.out.println(queue.poll()); // 14
//        
//        queue.offer(17);
//        System.out.println(queue.poll()); // 14
//        
//        queue.offer(18);
//        System.out.println(queue.poll()); // 14
//        
//        
//        queue.offer(19);
//        System.out.println(queue.poll()); // 14
//        
//        queue.offer(20);
//        System.out.println(queue.poll()); // 14
//        
//        
//        queue.offer(21);
//        System.out.println(queue.poll()); // 14
//        
//        System.out.println(queue.poll()); 
        
    }

}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值