error C2223: left of '->next' must point to struct/union

#include<stdio.h>
# include <malloc.h>
# include <stdlib.h>
typedef char LinkedList;
typedef struct node
{
LinkedList data;
struct node *next;
};

int main(void)
{
int n=0;
LinkedList p,k;
k=(LinkedList)malloc(sizeof(struct node));
k->next=NULL;
while(n<5)
{
p=(LinkedList)malloc(sizeof(struct node));
p->data=2*n+1;
p->next=k->next;
k->next=p;
n++;
printf("%s ",p->data);
}
k->data=n;
return 0;
}
编译不过去,什么原因,请高手帮助?

你的定义有问题,应该改为:

typedef struct node* LinkedList;
typedef struct node
{
  int data;
 struct node *next;
};


printf("%s ",p->data);改为printf("%d ",p->data);

完整代码;

#include<stdio.h>
# include <malloc.h>
# include <stdlib.h>
typedef struct node* LinkedList; 
typedef struct node
{
  int data;
 struct node *next;
};

int main(void)
{
    int n=0; 
    LinkedList p,k;
    k=(LinkedList)malloc(sizeof(struct node));
    k->next=NULL;
    while(n<5)
    {    
        p=(LinkedList)malloc(sizeof(struct node));
        p->data=2*n+1;
        p->next=k->next;
        k->next=p;
        n++;
        printf("%d ",p->data);
    }
    k->data=n;
    return 0;
}

追问

程序无法运行

追答

printf("%d ",p->data); 后面的修改了,就好了吧,呵呵,估计你开始看的是我前面的修改,后来改了

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-10-21
你的k应该定义为结构体变量,如果你要用->,还必须是个指针变量
struct node *k;
申请空间也一样
k=(struct node*)malloc(sizeof(struct node));
p也一样;
你好像是吧typedef的用法弄错了,你是不是本来想写typedef struct node* LinkedList;的
第2个回答  2013-10-21
typedef char LinkedList;

p 这个只是char的变量吧。。好像错了很多,你这个程序
相似回答