帮忙看一下这个程序错在哪里了?

madiankun 2006-08-17 04:32:31
typedef struct node{
int data;
struct node *next;
}SNode;
SNode *push_l(SNode *top)/*进栈*/
{ SNode *h,*p;
int c;
h=(SNode *)malloc(SNode));
scanf("%d",&c);
h->data=c;h->next='\0';
while(c!-1)
{ p=(SNode *)malloc(sizeof(SNode));
scanf("%d",&c);
p->data=c;
p->next=top; top=p;
}return top;
}
SNode *pop_l(SNode *top,int *y,int *flag)/*出栈*/
{ int p;
if(top) *flag=0;
else
{ p=top; *y=p->data;
top=p->next; free(p);
*flag=1;
return top;
}
}
SNode *print(SNode *top)/*打印栈中剩下的数据*/
{ SNode *p;
p=top;
printf("The left data inside stack is:\n");
printf("Head");
while(p)
{ printf("->%d",p->data);
p=p->next;
}
printf("->end!\n");
}
main()
{ SNode *h;
int *y,*f;
h=push_l(h);
h=pop_l(h,y,f);
if(f)
{ printf(The stack is empty!\n");
exit(0);
}
print(h);
printf("The data left stack is %d\n",*y);
}
上面是关于对栈的操作,但有几处错误,请高手指点一下,不盛感激呀。
...全文
167 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
iunion 2006-08-18
  • 打赏
  • 举报
回复
無數的低級錯誤
daniel_zwn 2006-08-17
  • 打赏
  • 举报
回复
#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>


typedef struct node{
int data;
struct node *next;
}SNode;
SNode *push_l(SNode *top)/*进栈*/
{ SNode *h,*p;
int c;
//h=(SNode *)malloc(SNode));
h=(SNode *) malloc(sizeof(SNode));
scanf("%d",&c);
h->data=c;h->next='\0';
while(c!=1)//while(c!-1)
{ p=(SNode *)malloc(sizeof(SNode));
scanf("%d",&c);
p->data=c;
p->next=top; top=p;
}return top;
}
SNode *pop_l(SNode *top,int *y,int *flag)/*出栈*/
{ SNode *p;//int p;
if(!top){*flag=0;return NULL;}//if(top) *flag=0;
else
{ p=top; *y=p->data;
top=p->next; free(p);
*flag=1;
return top;
}
}
void print(SNode *top)//SNode *print(SNode *top)/*打印栈中剩下的数据*/
{ SNode *p;
p=top;
printf("The left data inside stack is:\n");
printf("Head");
while(p)
{ printf("->%d",p->data);
p=p->next;
}
printf("->end!\n");
}
main()
{ SNode *h;
int a,b;
int *y,*f;
y=&a;f=&b;
h=NULL;
h=push_l(h);
h=pop_l(h,y,f);
if(f)
{ printf("The stack is empty!\n");//printf(The stack is empty!\n");
exit(0);
}
print(h);
printf("The data left stack is %d\n",*y);
}



低级错误.

70,023

社区成员

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

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