C++实现环形队列

C++实现环形队列

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

class Customer
{
public:
    Customer(string name = "", int age = 0)
    {
        m_strName = name;
        m_iAge = age;
    }

    ~Customer()
    {

    }

    void printInfo() const
    {       
        cout << endl;
        cout << "xingming :" << m_strName << endl;
        cout << "nianling :" << m_iAge << endl;
    }

private:
    string m_strName;
    int m_iAge;
};

class MyQueue
{
public:
    MyQueue(int queueCapacity)
    {
        m_iQueueCapacity = queueCapacity;
        m_pQueue = new Customer[m_iQueueCapacity];
        ClearQueue();
    }

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

    void ClearQueue()
    {
        m_iHead = 0;
        m_iTail = 0;
        m_iQueuelen = 0;
    }

    bool QueueEmpty() const
    {
        if (m_iQueuelen == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
//      return m_iQueuelen == 0 ? true : false;
    }

    bool QueueFull() const
    {
        if (m_iQueuelen == m_iQueueCapacity)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    int QueueLenth() const
    {
        return m_iQueuelen;
    }

    bool EnQueue(Customer element)
    {
        if (QueueFull())
        {
            return false;
        }
        else
        {
            m_pQueue[m_iTail] = element;
            m_iTail++;
            m_iTail = m_iTail % m_iQueueCapacity;
            m_iQueuelen++;
            return true;
        }
    }

    bool DeQueue(Customer &element)
    {
        if (QueueEmpty())
        {
            return false;
        }
        else
        {
            element = m_pQueue[m_iHead];
            m_iHead++;
            m_iHead = m_iHead % m_iQueueCapacity;
            m_iQueuelen--;
            return true;
        }
    }

    void QueueTraverse()
    {
        for (size_t i = m_iHead; i < m_iQueuelen + m_iHead; i++)
        {
            m_pQueue[i%m_iQueueCapacity].printInfo();
            cout << "qian mian hai you " << (i - m_iHead) << "ren" << endl;
        }
    }

private:
    Customer *m_pQueue;
    int m_iQueuelen;
    int m_iQueueCapacity;
    int m_iHead;
    int m_iTail;
};


int main()
{
    MyQueue *p = new MyQueue(4);
    Customer c1("xiaoming", 12);
    Customer c2("xinasfmd", 32);
    Customer c3("xinfsawe", 23);

    p->EnQueue(c1);
    p->EnQueue(c2);
    p->EnQueue(c3);

    p->QueueTraverse();

    Customer c4("", 0);
    p->DeQueue(c4);
    c4.printInfo();

    p->QueueTraverse();

    delete[]p;
    p = NULL;

    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值