C++循环顺序队列
来源:互联网 发布:qt linux sleep头文件 编辑:程序博客网 时间:2024/04/23 15:31
顾名思义:采用顺序结构存放的队列称为顺序队列
循环顺序队列可以避免队列的假溢出现象的发生。如下图示,循环队列的几种特殊情况。
学习完顺序循环队列个人感觉应该注意的事项:
front、rear只是表示在base[i]这个顺序表中的索引值,而不是内存的绝对地址,这样也才在后面的循环的时候处理起来比较方便
队列循环的关键
front=(front+1)%queueSize;
下面是个人的循环队列操作工程文件:
///////////////////////////////////////////////////////////////////////////sqQueue.h ////////////////////////////////////////////////////////////////////////#ifndef MYHEAD_H #define MYHEAD_H #include"myhead.h"#endif#include<iomanip>////////////////////////////////////////////////////////////////////////////////循环顺序队列数据结构C++类声明(基类)template <typename ElemType>class SqQueue{public: void clear();//把循环顺序队置空 Status deQueue(ElemType & e);//出队列 Status enQueue(ElemType & e);//进队列 Status getFront(ElemType & e);//读循环顺序队列队头的元素 int getLength();//求循环顺序队中元素个数 bool isEmpty();//判断循环顺序队是否为空 bool isFull();//判断循环顺序队是否为满 SqQueue<ElemType> operator =(SqQueue<ElemType> rightQ);//重载赋值运算符的定义 void display(); void randSqueue(); //****************************下面为系统自动调用构造函数及析构函数声明*************************// SqQueue(int size=20);//构造函数 ~SqQueue();//析构函数 SqQueue(const SqQueue<ElemType> & otherQ);//拷贝初始化构造函数protected: int rear; int front; int queueSize; ElemType *base;};/////////////////////////////////////////////////////////////////////////////////////////////////循环顺序队列数据结构C++类实现(基类)template <typename ElemType>void SqQueue<ElemType>::clear(){ front=rear;}template <typename ElemType>Status SqQueue<ElemType>::deQueue(ElemType & e){ if(isEmpty()) return ERROR; e=base[front]; front=(front+1)%queueSize; return OK;}template <typename ElemType>Status SqQueue<ElemType>::enQueue(ElemType &e){ if(isFull()) return ERROR; base[rear]=e; rear=(rear+1)%queueSize; return OK;}template <typename ElemType>Status SqQueue<ElemType>::getFront(ElemType & e){ if(isEmpty()) return ERROR; e=base[front] return OK;}template <typename ElemType>int SqQueue<ElemType>::getLength(){ return (rear-front+queueSize)%queueSize;}template <typename ElemType>bool SqQueue<ElemType>::isEmpty(){ return rear==front?true:false;}template <typename ElemType>bool SqQueue<ElemType>::isFull(){ return (rear+1)%queueSize==front?true:false;}///////系统构造函数及析构函数的实现template <typename ElemType>SqQueue<ElemType>::SqQueue(int size){ base=new ElemType[size]; assert(base!=0); front=rear=0; queueSize=size;}template<typename ElemType>SqQueue<ElemType>::~SqQueue(){ delete []base;}template<typename ElemType>SqQueue<ElemType>::SqQueue(const SqQueue<ElemType>& otherQ){ base=new ElemType[otherQ.queueSize]; assert(base!=0); queueSize=otherQ.queueSize; front=otherQ.front; rear=otherQ.rear; for (int i = front;i%queueSize!=rear) { base[i]=otherQ.base[i]; i=(i+1)%queueSize; }}template<typename ElemType>void SqQueue<ElemType>::display(){ int n=getLength(); cout<<endl; cout<<"当前的队列为:"<<endl; for (int i = 0; i < n; i++) { cout<<setw(6)<<base[i+front]; } cout<<endl; cout<<setw(6)<<"↑"; for (int i = 0; i < n-1; i++) { cout<<setw(6)<<" "; } cout<<setw(6)<<"↑"<<endl; cout<<setw(6)<<"front"; for (int i = 0; i < n-1; i++) { cout<<setw(6)<<" "; } cout<<setw(6)<<"rear"<<endl;}template<typename ElemType>void SqQueue<ElemType>::randSqueue(){ ElemType Elem[11]; srand(unsigned(time(NULL))); int n=rand()%10+1; cout<<"产生的随机数组是:"<<endl; for (int i = 0; i < n; i++) { Elem[i]=rand()%100+1; cout<<setw(6)<<Elem[i]; } for (int i = 0; i < n; i++) { base[i]=Elem[i]; rear++; } display();}
// SqQueueTest.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include"SqQueue.h"#include<iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ SqQueue<int> SQ(10); SQ.randSqueue(); char YesOrNo='Y'; int num,a; Status sta; while (YesOrNo=='Y'||YesOrNo=='y') { cout<<"1. 进队列:"<<endl; cout<<"2. 出队列:"<<endl; cin>>num; switch (num) { case 1: cout<<"输入进队列的值:"<<endl; cin>>a; sta=SQ.enQueue(a); if (sta==ERROR) cout<<"队列已满!!!"<<endl; break; case 2: sta=SQ.deQueue(a); if (sta==ERROR) cout<<"队列已空!!!"<<endl; else cout<<"弹出的元素为:"<<a<<endl; break; default: break; } SQ.display(); cout<<"是否想要继续?是请输入Y"<<endl; cin>>YesOrNo; } system("pause"); return 0;}
0 0
- 循环队列 顺序存储 C实现
- 顺序循环队列的c语言实现
- 循环队列-顺序存储-c语言实现
- 顺序队列(循环队列)基本操作实现 C语言
- 数据结构(C实现)------- 顺序队列(非循环队列)
- c/c++实现顺序循环队列和链式队列
- C语言实现,顺序队列,循环队列,和栈!
- SeqQueue--顺序循环队列
- 顺序循环队列
- 顺序循环队列
- 循环顺序队列
- C++循环顺序队列
- 循环队列-顺序表
- 循环顺序队列
- _DataStructure_C_Impl:顺序循环队列
- C++循环顺序队列
- 顺序循环队列
- 顺序循环队列
- 电话线信号音标准及解释
- [日志处理工作之八]日志处理架构
- snmp协议访问服务器得到CPU,内存等
- linux下ffmpeg的使用方法
- JQuery如何判断标签指定属性是否存在
- C++循环顺序队列
- Linux系统强大的控制台、终端和shell的关系
- Offer被拒后,张一鸣决定跟技术人才聊聊,你的下一份工作应该在哪里
- 【敏捷软件开发:原则、模式与实践】之代码重构
- [IOS]PDF阅读
- uva 1304 - Art Gallery(半平面相交)
- CentOS 7 下安装 Redis 2.8.7
- XML example with RESTEasy + JAXB
- js要入门?