环形队列C++实现

大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang

以下鄙人用C++实现了环形队列

/*************************************************************************************************************/

#include<iostream>
#include<stdlib.h>
#include"MyQueue.h"
#include<stddef.h>
using namespace std;
/******************/
/**实现环形队列***/
/******************/
int main(void)
{
    MyQueue *p = new MyQueue(4);
    Customer c1("zhangsan",20);
    Customer c2("lisi",20);
    Customer c3("wangwu",20);
 
    p->EnQueue(c1);
    p->EnQueue(c2);
    p->EnQueue(c3);
    p->QueTraverse();
 
    Customer c4("", 0);
    p->DeQueue(c4);
    c4.printInfo();
 
    p->QueTraverse();
 
    return 0;
}
 
MyQueue.h文件
/*****************************************************************************************************************************************************/

#ifndef MYQUEUE_H
#define MYQUEUE_H
 
/******************************************/
/*环形队列C++实现2016.2.3 by little duck*/
/***************************************/
#include"Customer.h"
 
class MyQueue
{
public:
    MyQueue(int queueCapacity);//创建队列
    virtual ~MyQueue();         //销毁队列
    void ClearQueue();          //清空
    bool QueueEmpty() const;    //判空
    bool QueueFull() const;     //判满
    int QueueLength() const;    //队列长度
    bool EnQueue(Customer element);  //新元素入队
    bool DeQueue(Customer &element);//首元素出兑
    void QueTraverse();         //遍历队列
private:
    Customer *m_pQueue;          //队列数组指针
    int m_iQueueLen;        //队列元素个数
    int m_iQueueCapacity;   //队列数组容量
    int m_iHead;
    int m_iTail;
};
#endif // MYQUEUE_H

 

 MyQueue.cpp文件
/*****************************************************************************************************************************************/ 

#include<stddef.h>
#include<iostream>
#include "MyQueue.h"
using namespace std;
 
MyQueue::MyQueue(int queueCapactiy)
{
    m_iQueueCapacity = queueCapactiy;
    m_pQueue = new Customer[m_iQueueCapacity];
    ClearQueue();
}
MyQueue::~MyQueue()
{
    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;
}
 
int MyQueue::QueueLength() const
{
    return m_iQueueLen;
}
 
bool MyQueue::QueueFull() const
{
    return m_iQueueLen == m_iQueueCapacity;
}
 
bool MyQueue::EnQueue(Customer element)
{
    if(QueueFull())
    {
        return false;
    }
    else
    {
        m_pQueue[m_iTail] = element;
        ++ m_iTail;
        m_iTail %= m_iQueueCapacity;
        ++ m_iQueueLen;
        return true;
    }
}
 
bool MyQueue::DeQueue(Customer &element)
{
    if(QueueEmpty())
    {
        return false;
    }
    else
    {
        element = m_pQueue[m_iHead];
        ++ m_iHead;
        m_iHead %= m_iQueueCapacity;
        -- m_iQueueLen;
        return true;
    }
}
 
void MyQueue::QueTraverse()
{
    for(int i = m_iHead; i < m_iHead + m_iQueueLen; ++i)
    {
        m_pQueue[i%m_iQueueCapacity].printInfo();
        cout << "前面还有" << (i - m_iHead) << "人" << endl << endl << endl;
    }
    cout << endl ;
}

Customer.h文件
/*****************************************************************************************************************************************************/

#ifndef CUSTOMER_H
#define  CUSTOMER_H
 
#include<string>
using namespace std;
 
class Customer
{
public:
    Customer(string name = "", int age = 0);
    void printInfo() const;
private:
    string m_strName;
    int m_iAge;
};
 
 
#endif // CUSTOMER_H
 

 Customer.cpp文件

/*****************************************************************************************************************************************************/ 

#include<iostream>
#include"Customer.h"
using namespace std;
 
Customer::Customer(string name, int age)
{
    m_strName = name;
    m_iAge = age;
}
void Customer::printInfo() const
{
    cout << "姓名" << m_strName << endl;
    cout << "年龄" << m_iAge << endl;
    cout << endl;
}

原文链接: https://www.cnblogs.com/xiaoyajiang/p/5950333.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    环形队列C++实现

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/241905

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午9:57
下一篇 2023年2月13日 下午9:58

相关推荐