大家帮我看看我的数据结构的题怎么总是出错

tianshilei1992 tianshilei1992 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
题目要求是:商店货架以栈的方式摆放商品。生产日期越接近的越靠栈底,出货时从栈顶取货。一天营业结束,如果货架不满,则需上货。入货直接将商品摆放到货架上,则会使生产日期越近的商品越靠近栈顶。这样就需要倒货架,使生产日期越近的越靠近栈底。
我自己写了以后能够编译通过运行,但是第一次输入时候好着,紧接着再输入一次商品就会崩溃。大家帮忙看下哪里出问题了。

#include <stdio.h>
#include <stdlib.h>

/*商品属性结构体*/
typedef struct Commodity
{
char name[10];
long date;
}Commodity;

/*货架堆栈*/
typedef struct node
{
Commodity sp;
struct node *next;
}LinkStackNode, *LinkStack;

void Welcome(LinkStack *top, LinkStack *tmp);

/*初始化堆栈*/
void InitStack(LinkStack *top)
{
*top = (LinkStack)malloc(sizeof(LinkStackNode));
(*top) -> next = NULL;
}

/*打印货架商品货架状态*/
void State(LinkStack *top, LinkStack *tmp)
{
int a, i = 0;
if ((*top) -> next == NULL)
{
printf("商品货架无商品!退出请输入0,返回上一层请输入1:");
scanf("%d", &a);
switch (a)
{
case 0: exit(0);
case 1: Welcome(top, tmp);
}
}
else
{
while ((*top) -> next == NULL)
{
printf("%d.商品名称:%s,生产日期:%", i, (*top) -> sp.name);
i++;
(*top) = (*top) -> next;
}
}
}

/*将商品压入堆栈中*/
void Push1(LinkStack *top, LinkStack *s)
{
(*s) -> next = (*top) -> next;
(*top) -> next = (*s);
}

/*将商品从商品堆栈中压入临时堆栈中*/
void Push2(LinkStack *top, LinkStack *tmp)
{
LinkStackNode *p, *q;
p = (*top) -> next;
q = (*tmp) -> next;
(*top) -> next = p -> next;
p -> next = q;
(*tmp) -> next = p;
}

/*添加商品到商品货架上*/
void AddCommodity(LinkStack *top, LinkStack *tmp)
{
int x = 0, i = 1;
LinkStackNode *s;
while (i == 1)
{
s = (LinkStackNode *)malloc(sizeof(LinkStackNode));
printf("请输入商品名称:");
scanf("%s", s -> sp.name);
printf("请输入商品生产日期:");
scanf("%d", &s -> sp.date);
if ((*top) -> next != NULL)
{
while (s -> sp.date > (*top) -> sp.date)
{
Push2(top, tmp);
}
Push1(top, &s);
while ((*tmp) -> next != NULL)
{
Push2(tmp, top);
}
i = 0;
}
else
{
Push1(top, &s);
i = 0;
}
printf("添加完成,是否需要继续添加?是请输入1,否则请输入0:");
scanf("%d", &i);
}
printf("操作完成,退出请输入0,返回上一层请输入1:");
scanf("%d", &x);
if (x == 1) Welcome(top, tmp);
else exit(0);
}

/*打印关于*/
void PrintAbout(LinkStack *top, LinkStack *tmp)
{
int i;
printf("计算机科学技术1003班 田世磊\n退出请输入0,返回上一层请输入1:");
scanf("%d", &i);
if (i == 1) Welcome(top, tmp);
else exit(0);
}

/*打印欢迎屏幕*/
void Welcome(LinkStack *top, LinkStack *tmp)
{
int i;
printf("欢迎进入XXX商品管理系统,请根据下面数字提示选择进入不同功能\n1.查看货架商品状态\n2.添加商品\n3.关于\n4.退出\n请输入数字:");
scanf("%d", &i);
switch (i)
{
case 1: State(top, tmp);
case 2: AddCommodity(top, tmp);
case 3: PrintAbout(top, tmp);
case 4: exit(0);
}
}

void main()
{
LinkStack tmp;
InitStack(&tmp);
LinkStack top;
InitStack(&top);
Welcome(&top, &tmp);
system("PAUSE");
}

VS2010提示p指针为空,不知道怎么回事……
...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部
tianshilei1992
C语言 70,040 成员 243,237 内容