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 ...
随机推荐
- Dagger2在Android开发中的应用
世界是普遍联系的,任何事物和个体都直接或间接相互依赖,在时空长河中共同发展.在面向对象的世界中,更是如此,类与类之间的依赖,关联关系,模块(亦或是分层架构中的层)之间的耦合关系,都是我们在软件开发实践 ...
- Chrome浏览器扩展开发系列之四:Browser Action类型的Chrome浏览器扩展
Browser Action类型的Google Chrome扩展程序,通常在Chrome浏览器的工具栏中,地址栏的右侧,有一个始终存在的图标.也就是说,这个图标与浏览器相关,只要安装了该Chrome扩 ...
- AngularJS 和 Bootstrap
AngularJS Bootstrap AngularJS 的首选样式表是 Twitter Bootstrap, Twitter Bootstrap 是目前最受欢迎的前端框架. 查看 Bootstra ...
- Express 学习笔记纯干货(Routing、Middleware、托管静态文件、view engine 等等)
原始文章链接:http://www.lovebxm.com/2017/07/14/express-primer/ 1. Express 简介 Express 是基于 Node.js 平台,快速.开放. ...
- POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题)
POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题) Descripti ...
- Java 基本语法----关键字、标识符
关键字 关键字的定义和特点 定义:被Java语言赋予了特殊含义,用做专门用途的字符串(单词)特点:关键字中所有字母都为小写 用于定义数据类型的关键字 class interface enum byte ...
- nodejs 全局变量和全局对象
1.全局对象 所有模块都可以调用 1)global:表示Node所在的全局环境,类似于浏览器中的window对象. 2)process:指向Node内置的process模块,允许开发者与当前进程互动. ...
- The first day,I get a blogs!!
我拥有了自己的博客,很happy! 今天学习了kvm,虽然命令行界面比较枯燥,还好不算太难,在大家的热心帮助下我创建了一个虚拟机!!
- InnoDB关键特性之change buffer
一.关于IOT:索引组织表 表在存储的时候按照主键排序进行存储,同时在主键上建立一棵树,这样就形成了一个索引组织表,一个表的存储方式以索引的方式来组织存储的. 所以,MySQL表一定要加上主键,通过主 ...
- java内存区域——深入理解JVM读书笔记
本内容由<深入理解java虚拟机>的部分读书笔记整理而成,本读者计划连载. 通过如下图和文字介绍来了解几个运行时数据区的概念. 方法区:它是各个线程共享的区域,用于内存已被VM加载的类信息 ...