# 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;
}
求错误 详细原因 我说语法错误
不错 很详细 但是 那个 结构体 不加名字 是咋回事啊
追答/*
如果不在struct后面起个名字
那么属于结构体的匿名定义方式
所以在成员中定义结构体指针变量时你需要用struct *
但是这样用,
当你把一个node * 的指针变量赋给该struct *时
会提示不能从node * 转换为struct $S1 *
也就是说虽然你用的是struct *
但是其真正类型是struct $S1 *
所以在下面添加新结点时是添加不上的
至于为什么会是这个效果,我也不清楚 呵呵
我只能推测到这
供你参考吧,建议定义结构体最好用第一种方式
即:struct 起个名字
{
成员列表
};(分号不能省)
*/
第一处的问题 能否详细说下啊
追答NODE, *PNODE是你用typedef 自定义的类型,用他就可以声明一个结构体变量(与int等声明有共通之处) struct node * next;表示一个结构体指针,再具体的话你应该再仔细看下链表的有关数据结构的知识了