大家好,我是小鸭酱,博客地址为: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. 原生js制作弹出框

    完整代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  2. Responder一点也不神秘————iOS用户响应者链完全剖析

    一.事件分类 对于IOS设备用户来说,他们操作设备的方式主要有三种:触摸屏幕.晃动设备.通过遥控设施控制设备.对应的事件类型有以下三种: 1.触屏事件(Touch Event) 2.运动事件(Moti ...

  3. 对JAVA的static深刻理解(结合C语言的思考)

    public class statictest { String X = "我是非静态变量"; static int butterfly =0; static String sta ...

  4. Itext 中的文本信息绝对定位

    PdfContentByte pcb = pw.getDirectContent(); pcb.beginText(); pcb.setFontAndSize(bfChinese, 12); pcb. ...

  5. win8下nodejs安装配置记录

    1:打开nodejs官网http://nodejs.org/ 下载安装版. 2:安装完成后,打开cmd输入node -v 查看是否安装成功: 3:安装express,通过全局安装方式进行安装: 安装完 ...

  6. bzoj1143

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1143 首先用传递闭包,知道一个点是否可以到达另一个点,即mp[i][j]==1表示i可以到j: ...

  7. Decorator学习笔记

    初学者,自己的理解,请各位前辈不吝指正! Decorator,装饰模式,设计模式之一,谈谈我的理解,装饰这个词在我概念中就是给某个事物加上一些美丽的外表,把它变得更加完美.但是装饰是可以随时改变的,可 ...

  8. chapter 2: Representing and manipulating information

    C allows conversion between unsigned and signed. The rule is that the underlying bit representation ...

  9. C# - List操作- 去掉重复

    ChangeList里面会有重复的数据,这时可以这样去掉重复的item // Remove duplicated info var dup = ChangeList.Where(item => ...

  10. 解决IE6下DIV无法实现1px高度问题

    2.多加一个line-height:1px的属性,不过得在DIV里多加一个 ,也就是空格,以下为引用的内容: <styletypestyletype="text/css"&g ...