为了账号安全,请及时绑定邮箱和手机立即绑定
  • 环形队列的实际应用,定义customer类 #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 #include<iostream> #include<stdlib.h> #include<string> #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; } 因为不再是int类型,所以队列类中要改变一些数据类型,函数类型,如下: (public下的)bool EnQueue(Customer element);//Customer类型的元素,下同,实现要保持一致(略) bool DeQueue(Customer &element); (private下的)Customer *m_pQueue;//Customer类型的指针 遍历的循环体要有所改动: //实际应用循环体要有所改动 void MyQueue::QueueTraverse () { for(int i=m_iHead;i<m_iQueueLen+m_iHead;i++)//m_iQueueLen+m_iHead保证循环次数不会出错,防止当头为1,长度为3时只能循环两次,队列长度指挥循环次数 { m_pQueue[i%m_iQueueCapacity].printInfo ();//直接调用类中有的打印函数,不需要再用cout } }
    查看全部
  • 接图片代码: p->QueueTraverse ();//发现至此不能遍历出来,因此遍历实现有误,需更正 【更正: void MyQueue::QueueTraverse () { for(int i=m_iHead;i<m_iQueueLen+m_iHead;i++)//m_iQueueLen+m_iHead保证循环次数不会出错,防止当头为1,长度为3时只能循环两次,队列长度指挥循环次数 { //cout<< m_pQueue[i%m_iQueueLen] <<endl;//防止下标i溢出,环形队列头不一定从[0]开始 cout<< m_pQueue[i%m_iQueueCapacity] <<endl;//更正应对m_iQueueCapacity取余,对常数容量取余 } cout<<endl;//为了清晰 }】 p->ClearQueue (); p->QueueTraverse (); p->EnQueue (20); p->EnQueue (30); p->QueueTraverse (); delete p; p=NULL; return 0; }
    查看全部
    0 采集 收起 来源:环形队列检测

    2018-03-22

  • 环形队列实现部分二:(重点部分) bool MyQueue::EnQueue (int element) { if(QueueFull()){return false;} else { m_pQueue[m_iTail]=element; m_iTail++; m_iTail=m_iTail%m_iQueueCapacity;//防止下标溢出 m_iQueueLen++;//长度随时改变 return true;//告诉系统插入成功 } } bool MyQueue::DeQueue (int &element) { if(QueueEmpty()){return false;} else { element=m_pQueue[m_iHead];//首元素摘出 m_iHead++;//改变头位置 m_iHead=m_iHead%m_iQueueCapacity;//防止下标溢出 m_iQueueLen--;//减少个数 return true; } } void MyQueue::QueueTraverse () { for(int i=m_iHead;i<m_iQueueLen;i++) { cout<< m_pQueue[i%m_iQueueLen] <<endl;//防止下标i溢出,环形队列头不一定从[0]开始 } }
    查看全部
    0 采集 收起 来源:环形队列检测

    2018-03-22

  • 环形队列实现部分一: #include"MyQueue.h" #include<iostream> using namespace std; MyQueue::MyQueue(int queueCapacity)//构造函数用来创建队列 { m_iQueueCapacity=queueCapacity; /*m_iHead=0; m_iTail=0; m_iQueueLen=0;*/ ClearQueue(); m_pQueue=new int[m_iQueueCapacity]; } MyQueue::~MyQueue () { delete []m_pQueue; m_pQueue=NULL; } void MyQueue::ClearQueue () { m_iHead=0; m_iTail=0; m_iQueueLen=0; } bool MyQueue::QueueEmpty () const { if(m_iQueueLen==0) { return true; } else { return false; } //return m_iQueueLen==0?true:false; } int MyQueue::QueueLength () const { return m_iQueueLen; } bool MyQueue::QueueFull () const { if(m_iQueueLen==m_iQueueCapacity) {return true;} else{return false;} }
    查看全部
    0 采集 收起 来源:环形队列检测

    2018-03-22

  • 队列的定义 c++//c对比 c++有自己的数据成员所以不需要参数,而要指明数据类型,因为已经指明数据类型,所以不需要visit函数(遍历不同种类型时要用的函数)
    查看全部
  • 数据结构概念
    查看全部
    0 采集 收起 来源:课程简介

    2016-05-25

  • 队列 先进先出
    查看全部
    0 采集 收起 来源:队列

    2016-05-24

  • 环形队列,,内存没有空着效率高;内存中的数据也不用整体移动
    查看全部
    0 采集 收起 来源:队列

    2016-05-20

  • 环形队列
    查看全部
    0 采集 收起 来源:队列

    2016-05-19

  • 队列的用途
    查看全部
    0 采集 收起 来源:队列

    2016-05-12

  • 环形队列
    查看全部
    0 采集 收起 来源:队列

    2016-05-12

  • 普通队列
    查看全部
    0 采集 收起 来源:队列

    2016-05-12

  • 队列:普通队列和环形队列
    查看全部
    0 采集 收起 来源:队列

    2016-05-12

  • 队列:先入先出(First in Fitst out)FIFO
    查看全部
    0 采集 收起 来源:队列

    2016-05-12

举报

0/150
提交
取消
课程须知
本课程是程序世界中的核心课程 由于本门课程是以C++为编码实现的,所以需要大家熟练掌握C++语言基础语法。
老师告诉你能学到什么?
1、什么是数据结构、什么是队列以及队列的实现原理 2、如何设计队列的类,如何完善类的设计 3、如何实现队列的相关函数 4、如何检验代码的正确性,如何完善代码 5、如何与实际相结合,利用数据结构解决实际问题

微信扫码,参与3人拼团

意见反馈 帮助中心 APP下载
官方微信
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!