为了账号安全,请及时绑定邮箱和手机立即绑定

在老师的基础上改的,C++环形队列类模板

标签:
C++

感谢老师,讲的很认真非常好

1 - 环形队列类模板

#ifndef MYQUEUE_H
#define MYQUEUE_H

#include "Customer.h"

template <typename T>
class MyQueue
{
public:
    MyQueue(int queueCapacity);   //创建队列
    virtual ~MyQueue();           //销毁队列
    void ClearQueue();            //清空队列
    bool QueueEmpty()const;       //判空队列
    bool QueueFull()const;        //判满队列
    int QueueLength()const;       //队列长度
    bool EnQueue(T element);      //新元素入队
    bool DeQueue(T &element);   //首元素出队
    void QueueTraverse();         //遍历队列
private:
    T *m_pQueue;                //队列数组指针
    int m_iQueueLen;              //队列元素个数
    int m_iQueueCapacity;         //队列数组容量
    int m_iHead;                  //队列头
    int m_iTail;                  //队列尾
};

template <typename T>
MyQueue<T>::MyQueue(int queueCapacity)
{
    m_iQueueCapacity = queueCapacity;
    ClearQueue();
    m_pQueue = new T[m_iQueueCapacity];
}

template <typename T>
MyQueue<T>::~MyQueue()
{
    delete[]m_pQueue;
    m_pQueue = NULL;
}

template <typename T>
void MyQueue<T>::ClearQueue()
{
    m_iQueueLen = 0;
    m_iHead = 0;
    m_iTail = 0;
}

template <typename T>
bool MyQueue<T>::QueueEmpty() const
{
    if (m_iQueueLen == 0)
        return true;
    else
        return false;
}

template <typename T>
bool MyQueue<T>::QueueFull() const
{
    if (m_iQueueLen == m_iQueueCapacity)
        return true;
    else
        return false;
}

template <typename T>
int MyQueue<T>::QueueLength()const
{
    return m_iQueueLen;
}

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

template <typename T>
bool MyQueue<T>::DeQueue(T &element)
{
    if (QueueEmpty())
    {
        return false;
    }
    else
    {
        element = m_pQueue[m_iHead];
        m_iHead++;
        m_iHead = m_iHead % m_iQueueCapacity;
        m_iQueueLen--;
        return true;
    }
}

template <typename T>
void MyQueue<T>::QueueTraverse()
{
    cout << "" << endl;

    /*for (int i = 0; i < m_iQueueLen; i++)
    {
    cout << m_pQueue[(i+m_iHead) % m_iQueueCapacity] << endl;
    }*/

    for (int i = m_iHead; i < (m_iQueueLen + m_iHead); i++)
    {
        //cout << m_pQueue[i % m_iQueueCapacity] << endl;
        cout << i << endl;
        m_pQueue[i % m_iQueueCapacity].printInfo();
    }

    cout << "" << endl;
}

#endif

2 - 测试类

#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <iostream>
#include <string>
using namespace std;

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

Customer::Customer(string name, int age)
{
    m_strName = name;
    m_iAge = age;
}

void Customer::printInfo() const
{
    cout << "" << endl;
    cout << "姓名: " << m_strName << endl;
    cout << "年龄: " << m_iAge << endl;
    cout << "" << endl;
}

#endif

3- 测试

#include <iostream>
using namespace std;
#include <string>

#include "MyQueue.h"

int main()
{
    /*测试4*/
    MyQueue<Customer> *p = new MyQueue<Customer>(4);

    Customer c1("张三", 20);
    Customer c2("李四", 25);
    Customer c3("小华", 36);
    Customer c4("王二", 28);
    Customer c5("小明", 18);

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

    //cout << p->QueueLength() << endl;
    p->QueueTraverse();

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

    //cout << p->QueueLength() << endl;
    p->QueueTraverse();

    delete p;
    p = NULL;

    /*测试3*/
    /*MyQueue<string> *p = new MyQueue<string>(4);
    p->EnQueue("张");
    p->EnQueue("王");
    p->EnQueue("李");
    p->EnQueue("赵");
    p->EnQueue("tom");

    p->QueueTraverse();
    string s;
    p->DeQueue(s);
    cout << s << endl;

    p->EnQueue("tom");
    p->QueueTraverse();
    delete p;
    p = NULL;*/

    /*测试2*/
    /*MyQueue<int> p(4);
    p.EnQueue(10);
    p.EnQueue(20);
    p.EnQueue(30);
    p.QueueTraverse();*/

    /*测试1*/
    /*
    MyQueue<int> *p = new MyQueue<int>(4);

    p->EnQueue(10);
    p->EnQueue(20);
    p->EnQueue(30);
    p->EnQueue(40);
    p->EnQueue(50);
    cout << p->EnQueue(60) << endl;;

    //cout << p->QueueLength() << endl;

    p->QueueTraverse();

    int e;
    p->DeQueue(e);
    cout << e << endl;

    cout << p->QueueLength() << endl;

    p->QueueTraverse();
    p->EnQueue(50);
    p->DeQueue(e);
    cout << e << endl;

    cout << p->QueueLength() << endl;

    p->QueueTraverse();

    //p->ClearQueue();
    //cout<<p->QueueLength()<<endl;

    delete p;
    p = NULL;
    */
    system("pause");
    return 0;
}

5- 运行结果
图片描述

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消