#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<cstdlib>
#define Maxsize 100
#define error 0
using namespace std;
template<class ElemType>
class Queue
{
protected:
int QueueSize;
ElemType *base;
int front;
int rear;
public:
Queue(int size):front(0), rear(0), QueueSize(size+1)
{
base = new ElemType[QueueSize];
if(!base)
{
cout<<"创建失败!"<<endl;
}else cout<<"创建成功!"<<endl;
}
Queue():front(0), rear(0), QueueSize(Maxsize)//构造函数
{
base = new ElemType[QueueSize];
if(!base)
{
cout<<"创建失败!"<<endl;
}else cout<<"创建成功qqq!"<<QueueSize<<endl;
}
~Queue()//析构函数
{
delete[] base;
rear = 0;
front = 0;
cout<<"销毁成功"<<endl;
}
void Destroy_Queue();//销毁队列
void Clear_Queue();//清空队列
bool IfEmpty_Queue();//空否
bool IfFull_Queue();//满否
void EnQueue(ElemType elem);//插入队尾
void DeQueue();//删除队头
int QueueLength()//返回队长
{return (rear-front+QueueSize)%QueueSize;}
int GetQueueSize()
{return QueueSize;}
ElemType Getfront(){return front;}//输出队头序列
ElemType Getrear(){return rear;}//输出队尾序列
int Front()//输出队首的值
{
if(IfEmpty_Queue())
{ cout<<"无...";
return 0;}
else
return base[front];
}
int Visit_Queue()
{
if(IfEmpty_Queue())
{
cout<<"空..."<<endl;
return 0;
}
if(front<rear)
{
for(int i=front;i<rear;i++)
{
cout<<base[i]<<" ";
}
return 0;
}else{
for(int i=front;i<rear+QueueSize;i++)
{
cout<<base[i]<<" ";
}
return 0;
}
}
};
template<class ElemType>
void Queue<ElemType>::Destroy_Queue()
{
rear = 0;
front = 0;
delete[] base;
cout<<"销毁成功"<<endl;
}
template<class ElemType>
void Queue<ElemType>::Clear_Queue()
{
rear = 0;
front = 0;
}
template<class ElemType>
bool Queue<ElemType>::IfFull_Queue()
{
if((front-rear+QueueSize)%QueueSize==1)
{
return true;}
else return false;
}
template<class ElemType>
bool Queue<ElemType>::IfEmpty_Queue()
{
if(rear==front) return true;
else return false;
}
template<class ElemType>
void Queue<ElemType>::EnQueue(ElemType elem)
{
if(IfFull_Queue())
cout<<" "<<endl;
else
{
base[rear] = elem;
rear = (rear + 1) % QueueSize;
}
}
template<class ElemType>
void Queue<ElemType>::DeQueue()
{
if(IfEmpty_Queue())
cout<<"空...无法删除"<<endl;
else
{
front = (front + 1) % QueueSize;
}
}
数据结构实验代码杨辉三角.rar
需积分: 5 134 浏览量
2024-05-01
20:32:37
上传
评论
收藏 354KB RAR 举报
温柔-的-女汉子
- 粉丝: 768
- 资源: 4011