如何实现一个循环队列

 下面是一个循环队列的完整实现,欢迎读者朋友参考和指正:

template
class CyclicQueue {
public:
    typedef T                    value_type;
    typedef size_t               size_type;
    typedef T&                   reference;
    typedef const T&             const_reference;

    CyclicQueue() : m_popPos(0), m_count(0) {
        assert(N > 0);
        m_beginPtr = (T*)(::operator new(sizeof(T) * N)); // 分配原始空间
        }

    ~CyclicQueue() {
        _Clear();                                 // this->_Clear();
        ::operator delete((void*)m_beginPtr);
        }

    CyclicQueue(const CyclicQueue& copy) : m_popPos(0), m_count(0) {
        assert(N > 0);
        m_beginPtr = (T*)(::operator new(sizeof(T) * N));  // 分配原始空间
        size_t copyPos = copy.m_popPos;
        for (size_type idx = 0; idx < copy.m_count; ++idx) {
            _Copy(idx, copy.m_beginPtr[copyPos]);   // this->_Copy();
            ++copyPos; copyPos %= N; ++m_count;
            }
        }

    CyclicQueue& operator=(const CyclicQueue& other) {
        if (this != &other) {                  // 检查自赋值
             CyclicQueue temp(other);    // 调用拷贝构造函数
             _Swap(temp);                      //  this->_Swap();
            }
        return (*this);
        }

    bool is_empty() const { return (m_count == 0); }
    bool is_full() const { return (m_count == N); }

    value_type front() {
        assert(m_count != 0);
        return (m_beginPtr[m_popPos]);
    }

    value_type front() const {
        assert(m_count != 0);
        return (m_beginPtr[m_popPos]);
    }

    value_type back() {
        return _Back();            // this->_Back();
    }
   
    value_type back() const {
        return _Back();           // this->_Back();
    }
 
   bool push(const_reference data = T()) {
        if (m_count < N) {                 // 不满!
            size_type pushPos = (m_popPos + m_count) % N;
            _Copy(pushPos, data);          // this->_Copy();
            ++m_count;
            return true;
            }
        return false;
    }

    bool pop(reference data) {
        if (m_count > 0) {                    // 不空!
            data = m_beginPtr[m_popPos];      // operator=
            _Destroy(m_popPos);               // this->_Destroy();
            --m_count; ++m_popPos; m_popPos %= N;   // 新的pop位置
            return true;
            }
        return false;
    }

    size_type size() const { return m_count; }
    size_type capacity() const { return N; }
    void clear() { _Clear(); }             // this->_Clear();
    void swap(CyclicQueue& other) {
        _Swap(other);                     // this->_Swap();
    }

private:
    void _Clear() {
        for (; m_count > 0; --m_count) {
            _Destroy(m_popPos);           // this->_Destroy();
            ++m_popPos; m_popPos %= N;
            }
        m_popPos = 0;
    }
 
    void _Destroy(size_type idx) {
        assert(idx < N);
        T *pTemp = (m_beginPtr + idx);
        pTemp->~T();                   // 调用析构函数销毁元素对象
    }

    void _Copy(size_type idx, const_reference data) {
        assert(idx < N);
        T *pTemp = (m_beginPtr + idx);
        new ((void*)pTemp) T(data);  // 调用placement new和拷贝构造函数复制对象
    }

    void _Swap(CyclicQueue& other) {
        std::swap(m_beginPtr, other.m_beginPtr);
        std::swap(m_popPos, other.m_popPos);
        std::swap(m_count, other.m_count);
    }

    value_type _Back() const {
        assert(m_count != 0);
        size_type pushPos = (m_popPos + m_count) % N;
        if (pushPos == 0)
            return (*(m_beginPtr + N - 1));
        return (m_beginPtr[pushPos - 1]);
    }

    value_type        *m_beginPtr;        // 队列存储空间起始位置
    size_type          m_popPos;          // 下次pop位置
    size_type          m_count;             // 有效元素个数
};




Objective-C语言中可以使用NSArray或NSMutableArray来实现队列,但是它们是动态数组,插入和删除操作效率较低。如果需要实现一个高效的队列,可以使用循环队列循环队列是一种特殊的队列,它的队尾指针可以指向队头位置,形成一个环形结构。这样可以充分利用数组空间,提高队列的效率。 下面是Objective-C实现一个循环队列的示例代码: ``` @interface CircularQueue : NSObject @property(nonatomic, assign) NSInteger head; // 队头指针 @property(nonatomic, assign) NSInteger tail; // 队尾指针 @property(nonatomic, assign) NSInteger size; // 队列大小 @property(nonatomic, strong) NSMutableArray *queueArray; // 队列数组 - (instancetype)initWithSize:(NSInteger)size; // 初始化方法 - (BOOL)enqueue:(id)obj; // 入队方法 - (id)dequeue; // 出队方法 - (BOOL)isEmpty; // 判断队列是否为空 - (BOOL)isFull; // 判断队列是否已满 @end @implementation CircularQueue - (instancetype)initWithSize:(NSInteger)size { if (self = [super init]) { self.head = 0; self.tail = 0; self.size = size; self.queueArray = [NSMutableArray arrayWithCapacity:size]; for (NSInteger i = 0; i < size; i++) { [self.queueArray addObject:[NSNull null]]; } } return self; } - (BOOL)enqueue:(id)obj { if ([self isFull]) { return NO; } self.queueArray[self.tail] = obj; self.tail = (self.tail + 1) % self.size; return YES; } - (id)dequeue { if ([self isEmpty]) { return nil; } id obj = self.queueArray[self.head]; self.queueArray[self.head] = [NSNull null]; self.head = (self.head + 1) % self.size; return obj; } - (BOOL)isEmpty { return self.head == self.tail && self.queueArray[self.head] == [NSNull null]; } - (BOOL)isFull { return self.head == self.tail && self.queueArray[self.head] != [NSNull null]; } @end ``` 在上面的代码中,我们使用一个NSMutableArray来保存队列元素,使用head和tail两个指针来指示队头和队尾位置。enqueue方法用于入队操作,dequeue方法用于出队操作,isEmpty方法和isFull方法分别用于判断队列是否为空和已满。注意,在enqueue和dequeue方法中,我们使用取模运算来实现循环指针的功能。 使用循环队列可以有效提高队列的效率,特别是在需要频繁插入和删除元素的场景下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值