SqQueue(环状队列(顺序表结构))
template<typename ElemType>
class SqQueue
{
protected:
int count;
int front,rear;
int maxSize;
ElemType *elem;
public:
SqQueue(){}
SqQueue(int size);
virtual ~SqQueue();
int Length() const;
bool Empty() const;
void Clear();
void Traverse(void (*visit)(const ElemType &))const;
bool OutQueue(ElemType &e);
bool GetHead(ElemType &e) const;
bool InQueue(const ElemType &e);
SqQueue(const SqQueue<ElemType> ©);
SqQueue<ElemType> &operator =(const SqQueue<ElemType> ©);
};
template<typename ElemType>
//构造函数
SqQueue<ElemType>::SqQueue(int size)
{
maxSize=size;
elem=new ElemType[maxSize];
rear=front=;
count=;
}
template<typename ElemType>
//虚虚构函数
SqQueue<ElemType>::~SqQueue()
{
delete []elem;
}
template<typename ElemType>
//SqQueue长度
int SqQueue<ElemType>::Length() const
{
return count;
}
template<typename ElemType>
//判断空队列
bool SqQueue<ElemType>::Empty() const
{
return count==;
}
template<typename ElemType>
//清空队列
void SqQueue<ElemType>:: Clear()
{
rear=front=;
count=;
}
template<typename ElemType>
//遍历队列
void SqQueue<ElemType>::Traverse(void(*visit)(const ElemType & ))const
{
for(int pos=front;pos!=rear;pos=(pos+)%maxSize)
(*visit)(elem[pos]); }
template<typename ElemType>
//队首出
bool SqQueue<ElemType>::OutQueue(ElemType &e)
{
if(!Empty())
{
e=elem[front];
front=(front+)%maxSize;
count--;
return true;
}
else return false;
}
template<typename ElemType>
//加入新队尾
bool SqQueue<ElemType>::InQueue(const ElemType &e)
{
if(count==maxSize)
return false;
else
{
elem[rear]=e;
rear=(rear+)%maxSize;
count++;
return true;
} }
template<typename ElemType>
//复制构造
SqQueue<ElemType>::SqQueue(const SqQueue<ElemType> ©)
{
maxSize=copy.maxSize;
elem=new ElemType[maxSize];
front=copy.front;
rear=copy.rear;
count=copy.count;
for(int pos=front;pos!=rear;pos=(pos+)%maxSize)
elem[pos]=copy.elem[pos];
}
template<typename ElemType>
//重载=operator
SqQueue<ElemType> &SqQueue<ElemType>::operator=(const SqQueue<ElemType> ©)
{
if(©!=this)
{
maxSize=copy.maxSize;
delete []elem;
elem=new SqQueue<ElemType>[maxSize];
count=copy.count;
front=copy.front;
rear=copy.rear;
for(int pos=front;pos!=rear;pos=(pos+)%maxSize)
elem[pos]=copy.elem[pos];
return *this;
} }
SqQueue(环状队列(顺序表结构))的更多相关文章
- C语言 线性表 顺序表结构 实现
一个能够自动扩容的顺序表 ArrList (GCC编译). #include <stdio.h> #include <stdlib.h> #include <string ...
- c语言-顺序表
在数据结构中包含两种,一种线性结构(包括顺序表,链表,栈,队列),一种非线性结构(树,图), 顺序表,其实就是在内存动态数组,Java中的ArrayList就是一个典型的顺序表,它在顺序表的基础上增加 ...
- 顺序表及其多种实现方式 --- C/C++
所谓顺序表,即线性表的顺序存储结构.下面给出的是数据结构---线性表的定义. ADT List{ 数据对象: 线性表的数据对象的集合为{a1,a2,a3,...,an},每个元素的类型为ElemTyp ...
- C++中如何建立一个顺序表
准备数据 #define MAXLEN 100 //定义顺序表的最大长度 struct DATA { char key[10]; //结点的关键字 char name[20]; int age; }; ...
- 顺序表--MyArrayList的实现
实现的MyArrayList实为顺序表结构,其中要实现Iterable时必须在内部实现Iterator,即为该表的迭代器. public class MyArrayList<AntType> ...
- C#顺序表 & 单向链表(无头)
C# 顺序表 非常标准的顺序表结构,等同于C#中的List<T>,但是List<T>在排错查询和数据结构替换上存在缺陷,一些情况会考虑使用自己定义的数据结构 1.优化方向 下表 ...
- python基础下的数据结构与算法之顺序表
一.什么是顺序表: 线性表的两种基本的实现模型: 1.将表中元素顺序地存放在一大块连续的存储区里,这样实现的表称为顺序表(或连续表).在这种实现中,元素间的顺序关系由它们的存储顺序自然表示. 2.将表 ...
- Java算法 -- 顺序表
顺序表结构定义:就是按照顺序存储方式存储的线性表 1.定义一个顺序表的基本数据: static final int MAXLEN = 100; Class Student{ private Strin ...
- C语言顺序表
顺序表结构可设为一个数组和一个指向尾部的变量,数组用来存放元素,指向尾部的变量在插入元素的时候加一,删除元素的时候减一,始终指向尾部. typedef int elemtype; typedef st ...
随机推荐
- 一些爬虫中的snippet
1.tornado 一个精简的异步爬虫(来自tornado的demo) #!/usr/bin/env python import time from datetime import timedelta ...
- [编织消息框架][netty源码分析]11 UnpooledHeapByteBuf 与 ByteBufAllocator
每种ByteBuf都有相应的分配器ByteBufAllocator,类似工厂模式.我们先学习UnpooledHeapByteBuf与其对应的分配器UnpooledByteBufAllocator 如何 ...
- 复写equals、hashCode和toString方法
equals.hashCode和toString 这三个方法都是object类的方法,由于所有的类都是继承这个类,所以每一个类都有这三个方法. 1.复写equals方法 原则: 首先,两个实例是相同的 ...
- 个人开源项目testall 持续更新中···
项目在GitHub上:https://github.com/x113773/testall ,喜欢的给个星星呀,亲~ 打算把用到过的和学习过的,所有前后端技术都集成到这个项目里,并在issues里配以 ...
- 定制Android开发者专属T恤
之前在T社上买了一件定制的T恤,感觉质量挺不错的,那是段子张发起的众筹.正面有hello google这几个字母. 我自己本身是一个Android粉,从nexus手机到pixel手机,坚持买原生的操作 ...
- webpack3中文版使用参考文档--全面解析webpack.config.js
Webpack目前官方发布的最新版本是3.1.0,相对于2.0的怎么本,在语法上没有变动,只是新增了功能.使用webpack,需要事先安装node.js,并对node.js生态有一些基本的了解,比如( ...
- lsdslam代码笔记
0.1. question 0.2. 算法框架 0.3. 代码解析 0.3.1. 数据结构 0.3.1.1. Frame 0.3.1.2. FrameMemory 0.3.1.3. FramePose ...
- LRU算法总结
LRU算法总结 无论是哪一层次的缓存都面临一个同样的问题:当容量有限的缓存的空闲空间全部用完后,又有新的内容需要添加进缓存时,如何挑选并舍弃原有的部分内容,从而腾出空间放入这些新的内容.解决这个问题的 ...
- Tomcat7安装(linux环境)
1.获取安装包 如果没有tomcat,则创建之,并下载二进制文件到该目录,如下: mkdir /opt/tomcat cd /opt/tomcat wget http://mirrors.hust.e ...
- Express 学习笔记纯干货(Routing、Middleware、托管静态文件、view engine 等等)
原始文章链接:http://www.lovebxm.com/2017/07/14/express-primer/ 1. Express 简介 Express 是基于 Node.js 平台,快速.开放. ...