循环队列(circular queue)

本文介绍了一种高效的数据结构——循环队列,并提供了一个C++实现的例子。通过使用循环队列,可以在单读单写场景中避免加锁操作,显著提高并发性能。文中还对比了Boost库中的circular buffer。

  循环队列是非常有用的一个数据结构,在单读、单写入的情况下可以不必对共享的队列数据进行加锁,非常有利于提升并发的性能。下面是一个实例程序:

#include <vector>
#include <stdio.h>
template<typename T>
class CircularQueue {
public:
  CircularQueue(const size_t& size) : size_(size + 1), read_index_(0), write_index_(0) {
    data_.reserve(size_);
  }
  bool Push(const T& element) {
    size_t next_index = (write_index_ + 1) % size_;
    if (next_index == read_index_) {
      return false;
    } else {
      data_[write_index_] = element;
      write_index_ = next_index;
    }
  }
  bool Pop(T* element) {
    if (read_index_ == write_index_) {
      return false;
    } else {
      *element = data_[read_index_];
      read_index_ = (read_index_ + 1) % size_;

    }
  }
private:
  size_t read_index_;
  size_t write_index_;
  size_t size_;
  std::vector<T> data_;
};

int main(int argc, char** argv) {
  CircularQueue<int> circular_queue(100);
  int index = 0;
  while (circular_queue.Push(index)) {
    printf("%d\n", index);
    index++;
  }
  int value;
  while (circular_queue.Pop(&value)) {
    printf("--%d--\n", value);
  }
}

值得一提的是boost有circular buffer的实现
参考文献:
http://en.wikipedia.org/wiki/Circular_buffer#Record_last_operation
http://www.boost.org/doc/libs/1_39_0/libs/circular_buffer/doc/circular_buffer.html
#include <stdio.h> #include <stdlib.h> #include <assert.h> typedef struct { int *data; // 存储队列元素的数组 int capacity; // 队列容量 int front; // 队头指针 int rear; // 队尾指针 } CircularQueue; // 初始化循环队列 CircularQueue* createQueue(int capacity) { CircularQueue *q = (CircularQueue*)malloc(sizeof(CircularQueue)); q->data = (int*)malloc(capacity * sizeof(int)); q->capacity = capacity; q->front = 0; q->rear = 0; return q; } // 判断队列是否为空 int isEmpty(CircularQueue *q) { assert(q != NULL); return (q->front == q->rear); // 队头等于队尾时队列为空 } // 判断队列是否已满 int isFull(CircularQueue *q) { assert(q != NULL); return ((q->rear + 1) % q->capacity == q->front); // 下一位置是队头则队列为满 } // 入队操作 void enqueue(CircularQueue *q, int value) { assert(q != NULL); if (isFull(q)) { printf("Queue is full. Cannot enqueue.\n"); return; } q->data[q->rear] = value; q->rear = (q->rear + 1) % q->capacity; // 使用模运算实现循环 } // 出队操作 int dequeue(CircularQueue *q) { assert(q != NULL); if (isEmpty(q)) { printf("Queue is empty. Cannot dequeue.\n"); return -1; // 返回错误码 } int value = q->data[q->front]; q->front = (q->front + 1) % q->capacity; // 移动队头指针 return value; } // 获取队头元素但不出队 int peek(CircularQueue *q) { assert(q != NULL); if (isEmpty(q)) { printf("Queue is empty. No element to peek.\n"); return -1; } return q->data[q->front]; } // 主函数测试 int main() { CircularQueue *queue = createQueue(5); // 创建容量为4的循环队列(实际有效容量为4) enqueue(queue, 10); enqueue(queue, 20); enqueue(queue, 30); enqueue(queue, 40); printf("Dequeued element: %d\n", dequeue(queue)); // 输出10 printf("Front element after dequeue: %d\n", peek(queue)); // 输出20 enqueue(queue, 50); // 此时队列将再次变为满状态 // 尝试入队 enqueue(queue, 60); // 应提示队列已满 free(queue->data); free(queue); return 0; } 在这段代码上加上一个输出队列元素的方法
最新发布
07-13
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值