C语言问题啊啊啊啊

# include <stdio.h>
# include <malloc.h>

typedef struct
{
int data;
struct * next;
}NODE, *PNODE;

PNODE init(void)
{
PNODE head=NULL, p=NULL, toil=NULL;
char ch;

head=(PNODE)malloc(sizeof(NODE));
head->data=NULL;
head->next=NULL;

do
{
p=(PNODE)malloc(sizeof(NODE));
printf("SR\n");
scanf("%d", &p->data);
p->next=NULL;

if(!head)
{
head->next=p;
toil=p;
}
else
{
toil->next=p;
toil=toil->next;
}
printf("Whether to continue(y/n?)");
fflush(stdin);
scanf("%c", &ch);
}while(ch=='y');

}
int main(void)
{
PNODE head=init();

return 0;
}
求错误 详细原因 我说语法错误

/*
Hi!
有几点说明:
1、定义结构体时在struct后面加个名字
即 可该为:
typedef struct node
{
int data;
struct node * next;
}NODE, *PNODE;
原因是没有struct * 类型
2、do......while,,,,,,
中的if(!head)该为if(!head->next)
原因:
head定义后是一个指针常量
所以(!head)判断一定为假
即if里的语句永远不会执行
但是本题要求必须执行一次
3、init方法没写return语句
所以在main方法中是无法返回值的
应加上 return head;

供你参考!
*/追问

不错 很详细 但是 那个 结构体 不加名字 是咋回事啊

追答

/*
如果不在struct后面起个名字
那么属于结构体的匿名定义方式
所以在成员中定义结构体指针变量时你需要用struct *
但是这样用,
当你把一个node * 的指针变量赋给该struct *时
会提示不能从node * 转换为struct $S1 *
也就是说虽然你用的是struct *
但是其真正类型是struct $S1 *
所以在下面添加新结点时是添加不上的
至于为什么会是这个效果,我也不清楚 呵呵
我只能推测到这

供你参考吧,建议定义结构体最好用第一种方式
即:struct 起个名字
{
成员列表
};(分号不能省)
*/

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-03-15
1.结构体定义错误
typedef struct Node
{
int data;
struct Node* next;
}NODE, *PNODE;
2.head->data是int型,赋值NULL肯定错误,等于0.
我没看懂你想实现什么功能,就看到了这俩个错误。
第2个回答  2012-03-15
第一处 typedef struct node
{
int data;
struct node * next;
}NODE, *PNODE;
第二处 PNODE init(void) 无返回值 return head;
第三处是你的do..while循环逻辑有问题了追问

第一处的问题 能否详细说下啊

追答

NODE, *PNODE是你用typedef 自定义的类型,用他就可以声明一个结构体变量(与int等声明有共通之处) struct node * next;表示一个结构体指针,再具体的话你应该再仔细看下链表的有关数据结构的知识了

相似回答