大家好,我是小鸭酱,博客地址为: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. 对CNN模块的分析

    对 CNN 模块的分析,该论文(Systematic evaluation of CNN advances on the ImageNet)已经做过了,里面的发现是非常有帮助的:   使用没有 bat ...

  2. hdu Joseph

    #include <cstdio> #include <iostream> #include <algorithm> using namespace std; ]; ...

  3. 如何看懂XDEBUG+WEBGRIND?(转)

    看到一个很有用的东东,收藏.. http://blog.csdn.net/yukon12345/article/details/11408617 ~~~~~~~~~~ 使用:              ...

  4. javascript 中ASCII字符值转换

    char-->ascii    var a = "123";    a.charAt(1).charCodeAt();ascii-->char   String.fro ...

  5. emacs vim IDE

    原本想花点时间来学习下Vim或者emacs,结果在网上搜索到这篇文章 骂战挺多的,但是也长见识 http://bbs.csdn.net/topics/390306165 下面是windows下的ema ...

  6. Linux Shell逻辑运算符和表达式详解

    Shell 逻辑运算符涉及以下几种类型,只要适当选择,可以解决我们很多复杂的判断,达到事半功倍效果. 一.逻辑判断1.关于文件与目录的逻辑判断-f 常用.判断『文件』是否为普通文件,比如: if [ ...

  7. bzoj1619[Usaco2008 Nov]Guarding the Farm 保卫牧场

    Description The farm has many hills upon which Farmer John would like to place guards to ensure the ...

  8. centos无线网卡设置

    联想thinkpad E430 ,安装上centos后,无线网卡一直无法使用,经历了艰苦曲折到网卡驱动安装过程,历时2个晚上终于开心到用上无线网卡. 1.查看无线网卡到具体型号 [root@local ...

  9. js如何判断字符串是否进行过window.btoa()转码

    window.btoa()是基于Base64算法的.window.btoa()只能将ASCII字符进行转码 因此我们需要了解Base64的原理及主要特征:Base64的原理在这里就不多说了,网上很多讲 ...

  10. 第32讲 UI组件之 时间日期控件DatePicker和TimePicker

    第32讲 UI组件之 时间日期控件DatePicker和TimePicker 在Android中,时间日期控件相对来说还是比较丰富的.其中, DatePicker用来实现日期输入设置,    Time ...