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 ...
随机推荐
- 手把手教你 基础 整合最优雅SSM框架:SpringMVC + Spring
我们看招聘信息的时候,经常会看到这一点,需要具备SSH框架的技能:而且在大部分教学课堂中,也会把SSH作为最核心的教学内容. 但是,我们在实际应用中发现,SpringMVC可以完全替代Struts,配 ...
- 新浪云计算SAE部署代码过程
第一步:创建本地工作目录: 创建一个新文件夹,可以使用应用名为文件夹名,比如命名为test 第二步:从SAE的SVN仓库检出(checkout)一个应用的全部版本代码,右键–>点击“SVN Ch ...
- PHP实现简单的评论与回复功能还有删除信息
我们首先先看一下功能 上面黑色的是评论的下面红色的字体是回复的 再来看看怎么实现的 1.发布评论 <form action="pinglunchili.php" method ...
- c# 测试通过
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data; using S ...
- ReactiveCocoa源码解析(五) SignalProtocol的observe()、Map、Filter延展实现
上篇博客我们对Signal的基本实现以及Signal的面向协议扩展进行了介绍, 详细内容请移步于<Signal中的静态属性静态方法以及面向协议扩展>.并且聊了Signal的所有的g功能扩展 ...
- [图形学] 习题8.12 NLN二维线段裁剪算法实现
Nicholl-Lee-Nicholl二维线段裁剪算法相对于Cohen-Sutherland和Liang-Barsky算法来说,在求交点之前进行了线段端点相对于几个区域的判断,可以确切的知道要求交点的 ...
- 虚幻引擎UE4如何制作可拖动(Drag and Drop)的背包(Scrollbox)
本教程适合初学者(学习经历已有30天的UE4初学者). 最终效果 由于隐私保护,不想截实际的效果图,下面给出了示意图,左边是背包A,右边是背包B,将其中的子项目从左侧拖往右侧的背包,然后在插入位置放置 ...
- log 的 debug()、 error()、 info()方法的区别
软件中总免不了要使用诸如 Log4net, Log4j, Tracer 等东东来写日志,不管用什么,这些东东大多是大同小异的,一般都提供了这样5个日志级别: × Debug × Info ...
- IIS 反向代理 golang web开发
一. beego 开发编译 bee run 后会编译成 exe文件 编译生成后发布文件结构为 cmd 运行 cd D:/run beegoDemo.exe run 默认配置端口 不能为 80 跟iis ...
- data-packed volume container - 每天5分钟玩转 Docker 容器技术(43)
在上一节的例子中 volume container 的数据归根到底还是在 host 里,有没有办法将数据完全放到 volume container 中,同时又能与其他容器共享呢? 当然可以,通常我们称 ...