#pragma once
//循环顺序队列
#ifndef My_Head_H
#define My_Head_H
#include "G://code/c++/myhead.h"
#endif // !My_Head_H
template <typename ElemType>
class SqQueue
{
public:
//把队列置空
void clear();
//出队列(删除队头结点)
Status deQueue(ElemType& e);
//进队列(在队尾加入结点)
Status enQueue(ElemType e);
//求队列中结点个数
int Get_Length();
//以随机数填充i个结点
void Random_Fill_Queue(int i);
//打印所有结点
void Display_Queue();
//判断是否为空
bool Is_Empty();
//判断是否队列已满
bool Is_Full();
SqQueue(int size = 100);
~SqQueue();
protected:
int rear; //队尾指针
int front; //队首指针
int Queue_Size;//循环队列最大储存空间
ElemType* base;
};
template<typename ElemType>
inline void SqQueue<ElemType>::clear()
{
front = rear;//队首队尾指针相等即队列为空;
}
template<typename ElemType>
Status inline SqQueue<ElemType>::deQueue(ElemType& e)
{
if (Is_Empty()) return ERROR;
e = base[front]; //通过参数返回出元素
front = (front + 1) % Queue_Size;//修改头指针
return OK;
}
template<typename ElemType>
Status inline SqQueue<ElemType>::enQueue(ElemType e)
{
if (Is_Full()) return ERROR;
base[rear] = e;
rear = (rear + 1) % Queue_Size;
return OK;
}
template<typename ElemType>
inline int SqQueue<ElemType>::Get_Length()
{
return (rear - front + Queue_Size) / Queue_Size;
}
template<typename ElemType>
inline void SqQueue<ElemType>::Random_Fill_Queue(int i)
{
for (int count = 0; count < i; count++)
{
enQueue(random(100));
}
}
template<typename ElemType>
inline void SqQueue<ElemType>::Display_Queue()
{
cout << "Here is all Queue data:" << endl;
int p = front;
while (p!=rear)
{
cout << "->" << base[p];
p = (p + 1) % Queue_Size;
}
cout << endl;
}
template<typename ElemType>
inline bool SqQueue<ElemType>::Is_Empty()
{
return front == rear ? true : false;
}
template<typename ElemType>
inline bool SqQueue<ElemType>::Is_Full()
{
return (rear + 1) % Queue_Size == front ? true : false;
}
template <typename ElemType>
SqQueue<ElemType>::SqQueue(int size)
{
base = new ElemType[size];
front = rear = 0;
Queue_Size = size;
}
template <typename ElemType>
SqQueue<ElemType>::~SqQueue()
{
delete[]base;
}
数据结构-顺序循环队列的c++实现
最新推荐文章于 2022-04-08 21:09:00 发布