手写 环形队列各操作

MyQueue.h:

/

#pragma once

//环形队列 C++实现

class MyQueue
{
public:
    MyQueue(void);
    MyQueue(int QueueCapacity);
    virtual ~MyQueue(void);

    void ClearQueue();
    bool QueueEmpty() const;//清空队列 回到初始状态
    bool QueueFull() const;//判断队列是不是满了
    int  QueueLength() const;
    bool EnQueue(int element);//加入元素
    bool DeQueue(int& element);//删除首元素
    void QueueTraverse();//遍历队列

private:
    int  m_iQueueLen; //队列元素个数
    int* m_pQueue;    //队列指针
    int  m_iQueueCapacity; //队列容量 队列最大长度

    int m_iHead;//表示队列头的位置
    int m_iTail;//表示队列尾的位置

};
 

MyQueue.cpp:

///

#include "stdafx.h"
#include "MyQueue.h"

#include <iostream>

using namespace std;

MyQueue::MyQueue(void)
{
}


MyQueue::MyQueue(int QueueCapacity)
{
    m_iQueueCapacity = QueueCapacity;
    m_iHead = 0;
    m_iTail = 0;
    m_pQueue = new int[QueueCapacity];
    m_iQueueLen = 0;
}

MyQueue::~MyQueue(void)
{
    delete[] m_pQueue;
    m_pQueue = NULL;
}

void MyQueue::ClearQueue()
{
    m_iHead = 0;
    m_iTail = 0;
    m_iQueueLen = 0;
}

bool MyQueue::QueueEmpty() const
{        
    return m_iQueueLen==0? true:false;
}

bool MyQueue::QueueFull() const
{
    return m_iQueueLen == m_iQueueCapacity?true:false;
}

int MyQueue::QueueLength() const
{
    return m_iQueueLen;
}

bool MyQueue::EnQueue(int element)
{
    if (QueueFull())
    {
        return false;
    }
    else
    {
        m_pQueue[m_iTail] = element;
        m_iTail++;
        m_iTail = m_iTail%m_iQueueCapacity;
        m_iQueueLen++;
        return true;
    }
}

bool MyQueue::DeQueue(int& element)
{
    if (QueueEmpty())
    {
        return false;
    }
    else
    {
        element = m_pQueue[m_iHead];
        m_iHead++;
        m_iHead = m_iHead%m_iQueueCapacity;
        m_iQueueLen--;

        return true;
    }
}

void MyQueue::QueueTraverse()
{
    cout<<endl;
    for (int i = m_iHead ; i< m_iQueueLen+m_iHead; i++)
    {
        cout<< m_pQueue[i%m_iQueueCapacity]<<endl;
    }
    cout<<endl;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值