双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

gcc3.0编译代码后,程序运行中出现的问题,不理解,请帮忙

FishCrazy FishCrazy 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
最近编译以前写的一些代码包括实现链表、堆栈、二叉树和图的生成,插入,查询等功能,可是在编译通过之后运行程序会出现奇怪的现象,例如链表的代码大致如下:

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

typedef struct node{
char data;
struct node *next;
}LNode;

int create(LNode *h){
LNode *ptr;
int i=0; int n=0;
char tmp;
h=NULL;
printf("input the number of list:\n");
scanf("%d",&n);
for(;i<n;i++){
printf("input the %dth node of list: ");
scanf(" %c",&tmp);
ptr=(LNode*)malloc(sizeof(LNode));
if(!ptr) return 0;
ptr->data=tmp;
ptr->next=h;
h=ptr;
}
return 1;
}

int main(int argc, char** argv){
LNode *head, *ptr;
if(!create(head)){
printf("failed.\n");
exit(0);
}
ptr=head;
printf("print list:\n"); /*断点 1*/
while(ptr!=NULL){
printf("%c",ptr->data);
ptr=ptr->next;
}
printf("it's end.\n");
return 1;
}



每当程序运行到“断点 1”的时候("print list:"会被打印),程序就会自动终止
请大家帮忙看看问题可能出在什么地方?

另外,堆栈、队列、二叉树的代码同样可以编译运行,但是运行之后程序不会在终端上打印出任何东西。
...全文
评论 5
收藏 已收藏
分享
返回顶部
FishCrazy
数据结构与算法 33,027 成员 35,334 内容