大家好,我是小鸭酱,博客地址为: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;
}

环形队列C++实现的更多相关文章

  1. 【转】C#环形队列

    概述 看了一个数据结构的教程,是用C++写的,可自己C#还是一个菜鸟,更别说C++了,但还是大胆尝试用C#将其中的环形队列的实现写出来,先上代码: 1 public class MyQueue< ...

  2. Atitit.提升软件稳定性---基于数据库实现的持久化 循环队列 环形队列

    Atitit.提升软件稳定性---基于数据库实现的持久化  循环队列 环形队列 1. 前言::选型(马) 1 2. 实现java.util.queue接口 1 3. 当前指针的2个实现方式 1 1.1 ...

  3. 队列(Queue)--环形队列、优先队列和双向队列

    1. 队列概述 队列和堆栈都是有序列表,属于抽象型数据类型(ADT),所有加入和删除的动作都发生在不同的两端,并符合First In, First Out(先进先出)的特性. 特性: ·FIFO ·拥 ...

  4. C#实现环形队列

    概述 看了一个数据结构的教程,是用C++写的,可自己C#还是一个菜鸟,更别说C++了,但还是大胆尝试用C#将其中的环形队列的实现写出来,先上代码: public class MyQueue<T& ...

  5. 数据结构-环形队列 C和C++的实现

    队列: 含义:是一种先入先出(FIFO)的数据结构. 当我们把数据一个一个放入队列中.当我们需要用到这些数据时,每次都从队列的头部取出第一个数据进行处理.就像排队进场一样,先排队的人先进场. 结构如下 ...

  6. [LeetCode] Design Circular Queue 设计环形队列

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  7. ucos-iii串口用信号量及环形队列中断发送,用内建消息队列中断接收

    串口发送部分代码: //通过信号量的方法发送数据 void usart1SendData(CPU_INT08U ch) { OS_ERR err; CPU_INT08U isTheFirstCh; O ...

  8. 1-关于单片机通信数据传输(中断发送,大小端,IEEE754浮点型格式,共用体,空闲中断,环形队列)

    补充: 程序优化 为避免普通发送和中断发送造成冲突(造成死机,复位重启),printf修改为中断发送 写这篇文章的目的呢,如题目所言,我承认自己是一个程序猿.....应该说很多很多学单片机的对于... ...

  9. uvaoj 133 - The Dole Queue(逻辑,环形队列数数)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. Activiti 使用自己的身份认证服务

    Activiti 中内置了用户和组管理的服务,由identityService 提供调用接口,默认在spring配置中如下: <bean id="identityService&quo ...

  2. KEIL UV3中光标不对齐解决

    Keil uVision3与uV2相比增加了对更多型号单片机的支持,另外还对一些的方面进行了优化.不过它却优化出一个让人头疼的问题,那就是光标位置显示不正确!这一问题给程序的编写带来了许多不便.不过不 ...

  3. 用友U8.70安装说明

    用友U8.70安装说明 U8.70安装说明一.安装前注意事项:1.       在安装U870之前,我们推荐您确保当前计算机操作系统是“干净”的,即计算机在安装过操作系统和更新过必要的系统补丁后没有安 ...

  4. java进阶计划

    鉴于自己在java 的学习过程中,像是无头苍蝇一样,东扎一把,西戳一下,没有一个明确的方向,也没有一个比较明确的方面,所以有了这个大致的计划. 计划的目标: 1. java本身的目标 对线程(thre ...

  5. phpcms:一、安装及新建模板

    1.复制D:\WWW\phpcms\phpcms\templates\目录下的default文件粘贴在当前目录下,并重命名为新模板名字(youpinzhiyuan2012) 2.打开D:\WWW\ph ...

  6. MVC 校验

    校验保障了MVC 应用程序安全性. Models 文件夹包含表示应用程序模型的类 1,创建一个项目MvcValidateDemo. 2,创建一个实体类UserInfo在Models中,包含Id.Use ...

  7. git 拆库 切库 切分 子目录建库

    如果git库目录是这样的: git根目录 project_a/ project_b/ ... 并且想为project_a单独创建一个代码库 # 拉一个新分支 git co -b project_a_r ...

  8. NetAnalyzer笔记 之 四. C#版的抓包软件

    [创建时间:2015-09-10 22:37:04] NetAnalyzer下载地址 不好意思啊,NetAnalyzer停更有点长了,今天继续填坑^&^ NetAnalyzer实现结构 在上一 ...

  9. Android学习总结——强制下线功能(广播)

    最近一口气买了好几本书,其中Android的<第一行代码>觉得真心不错,学到这个内容,顺便做个总结,加深印象. 强制下线的基本思想就是在界面上弹出一个对话框,让用户必须点击确定按钮跳转到登 ...

  10. flash与字符串:字符串与属性

    有时候,我们想通过设置一个DisplayObject 类是属性值,只是需要通过点来引用即可.有时候,通过字符串也可以引用显示对象里面的属性值.     下面举个例子 .                 ...