队列顺序存储结构的C++模板类头文件源代码实现

线性表和栈分别是顺序存储结构和链式存储结构的代表,而队列和栈可以用以上两种数据结构进行表达,不过是把它们的成员函数加一改动,主要是限制为先进先出和后进先出的功能,本文主要介绍队列的顺序存储结构,这里只是简单队列,没有考虑双向队列和循环队列的情况,其实双向队列主要是针对链式存储结构的,方便从两边上下查找,循环队列的主要意义就是节省内存空间,因为一个数组在插入和删除同时进行的过程中,front和rear值不断增加,看似队列已满,其实front前的数组是空闲的,可以通过循环的方式覆盖。

下面是队列顺序存储结构的C++模板类头文件源代码:

//linearqueue.h
#ifndef LINEARQUEUE_H
#define LINEARQUEUE_H
#include <IOSTREAM>
const int CAPACITY=100;
template<class Type>
class LinearQueue
{
private:
Type Data[CAPACITY];
int front,rear;
public:
LinearQueue():front(0),rear(0){}
~LinearQueue(){}
bool AddElement(Type);
Type DeleteElement();
bool IsEmpty();
bool IsFull();
void Print();
};
template<class Type>
bool LinearQueue<Type>::AddElement(Type x)
{
if(IsFull())
return false;
Data[rear++]=x;
return true;
}
template<class Type>
Type LinearQueue<Type>::DeleteElement()
{
if(IsEmpty())
return Type(-111);
return Data[front++];
}
template<class Type>
bool LinearQueue<Type>::IsEmpty()
{
if(front==rear)
return true;
return false;
}
template<class Type>
bool LinearQueue<Type>::IsFull()
{
if(rear>CAPACITY)
return true;
return false;
}
template<class Type>
void LinearQueue<Type>::Print()
{
for(int i=front;i<rear;i++)
{
std::cout<<Data[i]<<" ";
}
std::cout<<std::endl;
}
#endif


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值