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

tianshilei1992 2011-10-28 05:22:47
题目要求是:商店货架以栈的方式摆放商品。生产日期越接近的越靠栈底,出货时从栈顶取货。一天营业结束,如果货架不满,则需上货。入货直接将商品摆放到货架上,则会使生产日期越近的商品越靠近栈顶。这样就需要倒货架,使生产日期越近的越靠近栈底。
我自己写了以后能够编译通过运行,但是第一次输入时候好着,紧接着再输入一次商品就会崩溃。大家帮忙看下哪里出问题了。

#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指针为空,不知道怎么回事……
...全文
98 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianshilei1992 2011-10-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xiaozhiwei 的回复:]

建两个栈,

一个栈作为 商品栈, 一个作为 倒货栈,

问题很多,
[/Quote]
请帮忙分析一下哪里问题
我就是想创建2个堆栈呀,一个tmp,一个top
tianshilei1992 2011-10-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mougaidong 的回复:]

是在.c文件中 还是在.cpp中 ?
[/Quote]
在.cpp中
tianshilei1992 2011-10-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 kgzhw 的回复:]

我觉得问题出在push2中,你解释一下push2什么意思。
[/Quote]
Push2是要把商品堆栈的内容推到临时堆栈中呀,这样能倒过来
tianshilei1992 2011-10-29
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 xiaozhiwei 的回复:]

while (s -> sp.date > (*top) -> sp.date)


这里 *top 只是用作栈头,里面存放的数据是无效的,

应该是 while (s -> sp.date > (*top) ->next-> sp.date)
[/Quote]

哦!对!酱紫!终于知道怎么错了!谢谢了!
xiaozhiwei 2011-10-29
  • 打赏
  • 举报
回复
while (s -> sp.date > (*top) -> sp.date)


这里 *top 只是用作栈头,里面存放的数据是无效的,

应该是 while (s -> sp.date > (*top) ->next-> sp.date)
xiaozhiwei 2011-10-28
  • 打赏
  • 举报
回复

#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;
LinkStackNode *node;

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


// 压栈
void Push(LinkStack top, LinkStackNode *node)
{
node->next = top->next;
top->next = node;
}

// 出栈
LinkStackNode* Pop(LinkStack top)
{
LinkStackNode *node;

if (top->next == NULL)
return NULL;

node = top->next;
top->next = top->next->next;

return node;
}

bool IsEmpty(LinkStack top)
{
if (top->next == NULL)
return true;
else
return false;
}

/*添加商品到商品货架上*/
void AddCommodity(LinkStack *top, LinkStack *tmp)
{
int x = 0, i = 1;
LinkStackNode *s;

LinkStackNode *tempNode;

while (i == 1)
{
s = (LinkStackNode *)malloc(sizeof(LinkStackNode));
printf("请输入商品名称:");
scanf("%s", s -> sp.name);
printf("请输入商品生产日期:");
scanf("%d", &s -> sp.date);
s->next =NULL;

while (!IsEmpty(*top))
{
if (s->sp.date > (*top)->next->sp.date)
{
tempNode = Pop(*top);
Push(*tmp, tempNode);
}
else
{
break;
}
}

Push(*top, s);
while (!IsEmpty(*tmp))
{
tempNode = Pop(*tmp);
Push(*top, tempNode);
}

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); break;
case 2: AddCommodity(top, tmp); break;
case 3: PrintAbout(top, tmp); break;
case 4: exit(0);break;
}
}

void main()
{
LinkStack tmp;
InitStack(&tmp);
LinkStack top;
InitStack(&top);
Welcome(&top, &tmp);
system("PAUSE");
}
kgzhw 2011-10-28
  • 打赏
  • 举报
回复
我觉得问题出在push2中,你解释一下push2什么意思。
xiaozhiwei 2011-10-28
  • 打赏
  • 举报
回复
建两个栈,

一个栈作为 商品栈, 一个作为 倒货栈,

问题很多,
turing-complete 2011-10-28
  • 打赏
  • 举报
回复
是在.c文件中 还是在.cpp中 ?

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧