大家好,我是小鸭酱,博客地址为: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. Proguard中optimize设置不当引发SimException

    今天来说一下Proguard中关于optimize的问题.先上一张异常图片 最近项目重构,重新调整了各个组件之间的依赖关系.过程中,在项目Proguard这块卡住了,最开始还好,Proguard只是提 ...

  2. [SQL注入1]From SQL injection to Shell

    第一次写,希望大神们多指点. 对于刚接触WEB渗透测试这块的朋友们,很希望能有个平台可以练习.网络上有不少,十大渗透测试演练系统,我这里推荐一个在10以外,适合初学者一步一步进步的平台PENTESTE ...

  3. FLASH驱动之-块设备驱动系统构架

    一.  块设备是只能以块为单位进行访问的设备,块的大小一般是512个字节的整数倍,常见的块设备包括硬件,SD卡,光盘,flash等.驱动程序是块的整数倍从设备读写得到数据.块设备的最小访单位为块,不同 ...

  4. [每日一题JS] 正则表达式

    判断字符串是否是这样组成的,第一个必须是字母,后面可以是字母.数字.下划线,总长度为5-20 var reg = /\b[a-zA-Z]{1}[a-zA-Z0-9_]{4,19}\b/; var fl ...

  5. cf C. Fixing Typos

    http://codeforces.com/contest/363/problem/C s2用于存处理之后的字符串,再遍历s1的时候,s2会有两种情况1.s2最后两个字符是相同的如xx,如果这时再遇到 ...

  6. cf E. Neatness

    http://codeforces.com/contest/359/problem/E 题意:要关掉所有房间的灯,一个步骤要么开灯,要么关灯,要么向有灯的方向前进一格.输出一种关掉所有灯的方案.不能关 ...

  7. poj1740 A New Stone Game

    题意:对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分给其它的某些堆. 真是好♂题,代码不长就是好♂题. 首先考虑两堆相同的 ...

  8. 简述TVS是的命名和封装

    1. 瞬态抑制二极管简称TVS (Transient Voltage Suppressor ),TVS的电气特性由P-N结面积,参杂浓度及晶片阻质决定的.其耐突波电流的能力与其P-N结面积成正比. 特 ...

  9. 自制单片机之五……LCD12864的驱动

    LCD12864的驱动LCD12864在市面上主要分为两种,一种是采用st7920控制器的,它一般带有中文字库字模,价格略高一点.另一种是采用KS0108控制器,它只是点阵模式,不带字库.很可惜,我的 ...

  10. HDU--3466(0-1背包+贪心/后效性)

    题意是: 给你一些钱 m ,然后在这个国家买东西, 共有 n 件物品,每件物品有  价格 P    价值 V    还有一个很特别的属性 Q, Q 指 你如过想买这件物品 你的手中至少有这钱Q . 虽 ...