自定义循环队列

我们有时还会使用一种队列叫循环队列。如操作系统课程讲解生产者消费者模型时可以就会使用循环队列。环形队列通常使用数组实现。

public class CircleQueue<E> {
    private E[] elements= (E[]) new Object[10];
    private int size=0;
    private int front=0;
    private int tail=0;

    public void add(E element){
        if(size==elements.length){
            throw new  IndexOutOfBoundsException("队列已满");
        }
        elements[tail]=element;
        tail=(tail+1)%elements.length;
        size++;
    }
    public E poll(){
        if(size==0){
            throw new RuntimeException("队列为空");
        }
        E element=elements[front];
        front=(front+1)%elements.length;
        size--;
        return element;
    }
    public boolean isEmpty(){
        return front==tail;
    }

    public int size(){
        return size;
    }

    public void clear(){
        front=tail=0;
    }

    public  E peek(){
        if(front==tail){
            return null;
        }
        return elements[front];
    }
    public String toString(){
        StringBuilder sb=new StringBuilder();
        sb.append("[ ");
        if(front!=tail){
            for(int i=front;i!=tail;i++){
                sb.append(elements[i]+" ");
            }
            sb.append("]");
        }else {
            sb.append("null ]");
        }
        return sb.toString();
    }
    public static void main(String[] args) {
        CircleQueue queue=new CircleQueue();
       for(String s:"1 2 3 4".split(" ")){
           queue.add(s);
       }
       System.out.println("队列中的元素:"+queue);
       queue.poll();
       System.out.println("队首元素:"+queue.peek());
       queue.poll();
       queue.poll();
       queue.poll();
       System.out.println(queue);
    }

}

试试效果:

队列中的元素:[ 1 2 3 4 ]
队首元素:2
[ null ]

Process finished with exit code 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++标准库中没有直接提供循环队列的实现。但是你可以通过使用数组或者自定义数据结构来实现循环队列循环队列是一种环形的数据结构,可以在固定大小的数组中实现队列的功能。 以下是一个简单的循环队列的实现示例: ```cpp #include <iostream> const int MAX_SIZE = 10; class CircularQueue { private: int front; // 队头指针 int rear; // 队尾指针 int data[MAX_SIZE]; // 存储数据的数组 public: CircularQueue() { front = rear = -1; } bool isEmpty() { return front == -1 && rear == -1; } bool isFull() { return (rear + 1) % MAX_SIZE == front; } void enqueue(int value) { if (isFull()) { std::cout << "Queue is full. Cannot enqueue." << std::endl; return; } if (isEmpty()) { front = rear = 0; } else { rear = (rear + 1) % MAX_SIZE; } data[rear] = value; } void dequeue() { if (isEmpty()) { std::cout << "Queue is empty. Cannot dequeue." << std::endl; return; } if (front == rear) { front = rear = -1; } else { front = (front + 1) % MAX_SIZE; } } int getFront() { if (isEmpty()) { std::cout << "Queue is empty." << std::endl; return -1; } return data[front]; } }; int main() { CircularQueue queue; queue.enqueue(1); queue.enqueue(2); queue.enqueue(3); std::cout << "Front element: " << queue.getFront() << std::endl; queue.dequeue(); std::cout << "Front element after dequeue: " << queue.getFront() << std::endl; return 0; } ``` 这个示例中,我们使用数组来存储数据,并使用两个指针`front`和`rear`来表示队头和队尾的位置。通过取模运算来实现循环的效果。`enqueue`函数用于入队操作,`dequeue`函数用于出队操作,`getFront`函数用于获取队头元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值