1.队列
#include <iostream>
#define MAX 8
typedef int datatype;
using namespace std;
class queue
{
private:
datatype data[MAX];
int front;
int tail;
public:
void init() //初始化
{
front=0; //首位一致因此为空
tail=0;
}
bool empty()
{
return front==tail?1:0; //刚开始不入队就为1,为空
}
bool full()
{
return front==(tail+1)%MAX?1:0; //0,1,2,3,(),0,1,2,3,(),....已满加一取余为front//输出1,已满//首尾相差最大值为满
}
int size()
{
cout<<"计算了队列当前的数据为:";
cout<<((tail+MAX-front)%MAX)<<"个"<<endl; //...3,(),0,1 ...//类似首为3,前面是总和加上尾部减去开头,就是大小
return ((tail+MAX-front)%MAX);
}
int size1()
{
return ((tail+MAX-front)%MAX);
}
void push(datatype e) //入队 e
{
if(full()==1)
{
cout<<"队列满了queue full,入不了"<<endl; //满了
}
data[tail]=e;
tail=(tail+1)%MAX; //计算尾部在哪个位置
}
void pop() //出队
{
if(empty()==1) //先看队列是否为空
{
cout<<"队列已空queue empty 不用出队"<<endl;
}
cout<<"已将首位数据出队\n";
front=(front+1)%MAX; //不空,最前面的向后移一位
}
void show() //队列具体情况
{
int i; //队伍序号
for(i=front;i!=tail;i=(i+1)%MAX) //只要front 首位与尾部不是同一个位置 ,后移一位
{
cout<<data[i]<<" "; //显示每一位的数据
}
cout<<endl; //换行
}
};
int main()
{
queue c1; //进入程序,类名是c1
c1.init(); //初始化队列,首尾同一位置
cout<<"队列初始化完成\n";
if(c1.empty()==1) //需要判断队伍情况是否存在队伍
{
cout<<"队列一个数据没有 queue empty 队列可以加入数据"<<endl;
}
else
{
cout<<"队列有数据 no empty 队列有数据"<<endl;
}
if(c1.full()==1) //判断队列是否已经满了
{
cout<<"队列满了没有位置了 full 不能加数据了"<<endl;
}
else
{
cout<<"队列不满有位置空缺 no full 可以加入数据"<<endl;
}
cout<<"队列不满加入队数据为: ";
c1.push(0); //对头加入数据0
c1.push(1);
c1.push(2);
c1.push(3);
c1.push(4);
c1.push(5);
c1.push(6); //队伍第七个加入数据6
c1.show(); //查看队伍情况
c1.size(); //计算队伍有多少个数据
c1.pop(); //队伍需要一个出队
cout<<"队伍当前数据为: ";
c1.show(); //查看现在队伍情况
c1.size(); //计算现在队伍有多少个数据
if(c1.full()==1) //判断队列是否已经满了
{
cout<<"队列满了 full 不能加数据了"<<endl;
}
else
{
cout<<"队列存在空缺 no full 可以加入数据"<<endl;
cout<<"空缺为: "<<(MAX-c1.size1())<<"个"<<endl;
}
system("color 17"); //输出窗口的背景是1号颜色,字体是7号颜色
cout << "队列布置情况已结束!" << endl;
return 0;
}
2.栈
#include <iostream> typedef int datatype; #define MAX 5 using namespace std; class pen { private: datatype data[MAX]; int top; public: void init() //栈的初始化 { top=-1; //栈从0开始,没有为-1 } bool empty() //栈为空 { return (top==-1)?1:0; //栈顶是-1,所以就是空 } bool full() //栈为满 { return (top==MAX-1)?1:0; //栈顶减1 } void size() //栈的大小 { int s=0; //初始化大小为0 s=top+1; //实际大小就是栈顶加一 if(s==-1) { cout<<"0\n"; } else { cout<<s<<endl; //输出大小 } } void push(datatype e) //数据入栈 { if(full()==1) //栈满了 { cout<<"栈是满的,存不了数据"<<endl; //输出栈满了 } top++; //输入数据后栈顶往上加一 data[top]=e; //栈顶数据为输入的值 } void pop() //数据出栈 { if(empty()==1) //判断栈是否已经空了 { cout<<"栈是空的,没有一个数据"<<endl; //输出栈空了 } top--; //栈顶减一 } void pop1() //输出数据出栈值 { cout<< data[top]<<" "; if(empty()==1) //判断栈是否已经空了 { cout<<endl; //输出栈空了 } } void show() //栈的展示函数 { if(empty()==1) //判断栈是否已经空了 { cout<<"栈是空的,没有一个数据"<<endl; //输出栈空了 } int i; //初始化栈的位置为i for(i=0;i<=top;i++) //位置的取值范围 { cout<<data[i]<<" "; //输出栈的数据 } cout<<endl; //换行 } void show1() //栈的展示函数 { int i; //初始化栈的位置为i for(i=0;i<=top;i++) //位置的取值范围 { cout<<" "<<data[i]<<" "; //输出栈的数据 } cout<<endl; //换行 } }; int main() { pen s1; //栈的类对象,就是栈名,命名一个栈 s1.init(); //先初始化 if(s1.empty()==1) //判断这个栈是不是空的 { cout<<"栈是空的,可以输入数据"<<endl; //栈是空的,输出栈是空的 top==-1 } else { cout<<"栈没有空,里面有数据但不知道有没有满"<<endl; //栈不是空的,输出栈不是空的 top!=-1 } if(s1.full()==1) //栈是不是满了 { cout<<"栈满了,输入不了数据"<<endl; //栈满并打印 top==MAX-1 } else { cout<<"栈没满可以继续输入数据"<<endl; //没满并打印没满 top!=MAX-1 } s1.empty(); //判断是否是空栈,能不能放数据 s1.full(); //判断是不是满栈,还有没有位置 cout<<"向栈里输入以下数据:\n"; s1.push(0); //向栈里输入数据 s1.push(2); s1.push(0); s1.push(6); //向栈里输入数据 s1.show1(); //输入了数据,查看栈的情况 cout<<"栈里的数据为:"; s1.show(); //查看栈的情况 cout<<"栈的大小为: "; s1.size(); //计算栈的大小 cout<<"有数据出栈:"; s1.pop1(); cout<<"\n"; s1.pop(); //一个数据出栈 cout<<"栈里的当前数据为:"; s1.show(); //查看栈的情况 cout<<"此时栈的大小: "; s1.size(); //计算栈的大小 cout<<"有数据出栈:"; s1.pop1(); s1.pop(); //一个数据出栈 s1.pop1(); s1.pop(); //继续一个数据出栈 s1.pop1(); s1.pop(); //继续一个数据出栈 cout<<endl; cout<<"此时栈的大小:"; s1.size(); //计算栈的大小 cout<<"栈里的当前数据为:"; s1.show(); //查看栈的情况 cout<<"栈的情况统计好了"<<endl; system("color 17"); return 0; }