动封装-个循环顺郭队列类 (Stack)
私有成员属性:存放队列的数组、两个变量分别记录队头和队尾下标
公有成员函数:入队(push( type value))
出队(pop());展示(show);求队列长度(size()) :要求时间复杂度在常量级别;判满(bool ful());判空(bool empty()
#include <iostream>
#define MAXSIZE 8
using namespace std;
typedef int datatype;
class stack
{
public:
//判满
bool full_stack()
{
if((last+1)%MAXSIZE == front)
return true;
else
return false;
}
//判空
bool empty_stack()
{
if(last == front)
return true;
else
return false;
}
//入队
void push(datatype e)
{
if(!full_stack())
{
data[last] = e;
last = (last+1)%MAXSIZE;
}else
{
cout<<"队列已满"<<endl;
return ;
}
}
//出队
void pop()
{
if(!empty_stack())
{
front = (front+1)%MAXSIZE;
}else
{
cout<<"队列为空"<<endl;
return;
}
}
//遍历
void output()
{
if(empty_stack())
{
cout<<"队列为空"<<endl;
return ;
}
cout<<"遍历结果"<<endl;
for(int i = front; i != last; i = (i+1)%MAXSIZE)
{
cout<<"第"<<(i+9-front)%MAXSIZE<<"个数据 :";
cout<<data[i] <<endl;
}
}
//求队列长度
int size_stack()
{
return (MAXSIZE - front + last)%MAXSIZE;
}
private:
datatype data[MAXSIZE];
int front = 0;
int last = 0;
};
int main()
{
stack s1;
datatype e;
int n = 0;
while(n<=0||n>8)
{
cout<<"输入数据个数"<<endl;
cin>>n;
}
//循环输入数据
for(int i = 0;i<n;i++)
{
cout<<"第"<<i+1<<"个数据 :";
cin>>e;
getchar();
s1.push(e);
}
//遍历数据
s1.output();
//删除数据并遍历数据
cout<<"删除数据"<<endl;
s1.pop();
s1.output();
//队列长度
cout <<"队列长度 :"<<s1.size_stack()<<endl;
return 0;
}