gcc3.0编译代码后,程序运行中出现的问题,不理解,请帮忙
最近编译以前写的一些代码包括实现链表、堆栈、二叉树和图的生成,插入,查询等功能,可是在编译通过之后运行程序会出现奇怪的现象,例如链表的代码大致如下:
#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:"会被打印),程序就会自动终止
请大家帮忙看看问题可能出在什么地方?
另外,堆栈、队列、二叉树的代码同样可以编译运行,但是运行之后程序不会在终端上打印出任何东西。