*尚品货架可以看成一个栈,栈顶商品的生产日期最早,栈底商品的生产日期最近。
*上货时,需要倒货架,以保证生产日期较新的商品在较靠里的位置
针对一种特定商品,实现上述管理过程
测试数据:由学生自己确定一组测试数据。注意测试边界数据,如空栈。
源代码
#include <iostream>
using namespace std;
struct stack{ //定义一个栈
int* Slist;
int top;
int Maxsize;
};
void Initstack(stack&SL, const int MS); //栈初始化
bool Stackempty(stack&SL);//判栈空
bool Stackfull(stack&SL); //栈满
void Clearstack(stack&SL); //清空栈
int Push(stack&SL, int&item);//新元素推进栈
int Pop(stack&SL);//出栈
void Traverstack(stack&SL); //输出栈中元素
void Initstack(stack&SL, const int MS) //栈的初始化
{
SL.Slist = new int[MS];
if (!SL.Slist){
cout << "给栈分配内存失败。" << endl;
exit(1);
}
SL.Maxsize = MS;
SL.top = -1;
}
bool Stackempty(stack&SL) //判空
{
return SL.top == -1;
}
bool Stackfull(stack&SL)//判满
{
return SL.top == SL.Maxsize;
}
void Clearstack(stack&SL)/

该博客探讨了如何使用栈数据结构来管理商品货架,确保最新生产日期的商品位于货架底部。文章提供了C++实现的源代码,并强调在上货时需要倒货架以保持商品的新鲜度。同时,提到了测试数据包括边界情况,如空栈。
最低0.47元/天 解锁文章
1091

被折叠的 条评论
为什么被折叠?



