链表中每个元素都是不可以一样的

如题所述

这个你说的链表 要看结构体里面指针的类型 如:
struct name{
char name[20];
struct name *next;
};
struct num{
int num;
struct name *next;
} ;
struct head{
struct name *namenext;
struct num *numnext;
};

在第一个结构体里面 指针类型是struct name * 所以只能指向struct name 类型的结构

第二个结构体里面 指针类型也是struct name * 所以也是只能指向struct name 类型的结构 而不能指向struct num类型的结构

最后一个的话 里面有两种指针,也就是说 你创建了一个
struct head *myhead = (struct head *)malloc(sizeof(struct head));
struct name *myname = (struct name *)malloc(sizeof(struct name));
struct num *mynum = (struct num *)malloc(sizeof(struct num));
那么head即能指向struct name 也能指向struct num
即 head->namenext = nyname;
head->numnext = mynum;
这两个指针都可以做为下一级的指针,不过这样做没有统一性 到时候难以区分是用的哪一个next指针,,,会有点混乱,,,所以不建议这么做,

大部分的链表头结点和其他结点的类型是一样的,那么在结构里定义的时候要跟第一个结构体一样,定义成自己的类型的指针。
当然头结点可能会是一种独特的类型,那么就单独定义一个头结构 里面的next指针指向的是其他你需要的类型的指针。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-02-16
每个元素的值可以不一样,但是类型是一样的。
第2个回答  2012-02-16
链表是由结构体连接而成,类型必须一致,内容不同是可以的