C语言 链表建立中的问题

来源:百度知道 编辑:UC知道 时间:2024/06/17 02:29:31
#include <stdio.h>
#include <malloc.h>

typedef struct node
{
int date;
struct node *link;
}numb;

numb* link()
{
numb *h,*s,*p;
int i=1;
if(h=( (numb *)malloc(sizeof(numb)) )==NULL);
{
printf("No Memory!!");
return h;
}
h->link = NULL;
s=p=h;
s->date=0;

while(s->date!=-1)
{
if(s=( (numb *)malloc(sizeof(numb)) ) ==NULL);
{
printf("No Memory!!");
return h;
}
if(h->link==NULL)
h->link=s;
printf("%3d :",i);
scanf("%d",s->date);
p->link=s;
s->link=NULL;
p=s;
++i;

}

}

int main(void)
{

printf("-----Input Number----\n");
link();

}

我想用链表保存用户输入的数据。

可总是出现问题。

请高人指教。

(问题 : 总是执行

小问题会造成想不到的结果。
你的IF后面有分号!
#include <stdio.h>
#include <malloc.h>

typedef struct node
{
int date;
struct node *link;
}numb;

numb* link()
{
numb *h,*s,*p;
int i=1;
if(h=( (numb *)malloc(sizeof(numb)) )==NULL);//<==这里不应该有分号。是初学者吗?好好看一下书吧。
{
printf("No Memory!!");
return h;
}
h->link = NULL;
s=p=h;
s->date=0;

while(s->date!=-1)
{
if(s=( (numb *)malloc(sizeof(numb)) ) ==NULL);//<==这里和上面的问题一样
{
printf("No Memory!!");
return h;
}
if(h->link==NULL)
h->link=s;
printf("%3d :",i);
scanf("%d",s->date);
p->link=s;
s->link=NULL;
p=s;
++i;

}

}

int main(void)
{

printf("-----Input Number----\n");
link();

}

还有你的编程风格不是很好。
指针在定义时最好初始化。