C链表添加节点错误

renzhenshiduoa 2012-09-04 09:45:22
自己写的小程序,基本代码如下:
struct stu{
unsigned int age;
int score;
char *name;
struct stu *next;
};

struct stu *add_node(struct stu *list,struct stu node){
struct stu *head=list;
struct stu *p=list;
while(p->next!=NULL)
p=p->next;
p->next=(struct stu *)&node;
node.next=NULL;
return head;
}
void main(void)
{
struct stu node={11,22,"jack"};
struct stu *list,*p,*q;
list=初始化化函数,这里就没有贴出来;
list=add_node(list,node);
p=list;
for(q=p;q!=NULL;q=q->next){
printf("age:\t %d\t",q->age);
printf("score:\t %d\t",q->score);
printf("name:\t %s\n",q->name);
}
}
打印出来的结果,初始化的都正确,但是新增加的节点显示结果如下:
age: 138211520 score: 22 name: jack
请问age怎么会那么大啊,node没有给最后一个节点正确赋值啊。
...全文
111 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2012-09-05
  • 打赏
  • 举报
回复
其实也就是说你处理的是链表非空的情况,但是未处理链表为空的情况
「已注销」 2012-09-05
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

引用 4 楼 的回复:
这个是从头部插入节点的代码

#include <stdio.h>
#include <stdlib.h>

struct stu
{
unsigned int age;
int score;
char *name;
struct stu *next;
};

struct stu *init_list()
{
struct stu *p……
[/Quote]

哦,你看我的代码比你多出的那部分,也就是对链表头指针的next成员做处理。你的没有处理过。改成这样就好了

struct stu *add_node(struct stu *list,struct stu node){
struct stu *head=list;
struct stu *p=list;

if (NULL == p->next)
{
node.next = NULL;
head = &node;

return head;
}

while(p->next!=NULL)
{
p=p->next;
}
node.next=NULL;
p->next=(struct stu *)&node;


return head;
}
「已注销」 2012-09-05
  • 打赏
  • 举报
回复
这个是节点从链表尾端插入

#include <stdio.h>
#include <stdlib.h>

struct stu
{
unsigned int age;
int score;
char *name;
struct stu *next;
};

struct stu *init_list()
{
struct stu *pList = NULL;

pList = (struct stu *)malloc(sizeof(struct stu));
if (NULL != pList)
{
pList->age = 0;
pList->score = 0;
pList->name = "";
pList->next = NULL;
}
return pList;
}

struct stu *add_node(struct stu *list,struct stu *node)
{
struct stu *head=list;
struct stu *p = head;

if (NULL == p->next)
{
node->next = NULL;
head = node;

return head;
}

while (NULL != p->next)
{
p = p->next;
}
node->next = NULL;
p->next = node;

return head;
}

int main(void)
{
struct stu node={11,22,"jack"};
struct stu *list,*p,*q;

list = init_list();
list = add_node(list,&node);
p = list;

for(q=p; q!=NULL; q=q->next)
{
printf("age:\t %d\t",q->age);
printf("score:\t %d\t",q->score);
printf("name:\t %s\n",q->name);
}
getchar();
return 0;
}
renzhenshiduoa 2012-09-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
这个是从头部插入节点的代码

#include <stdio.h>
#include <stdlib.h>

struct stu
{
unsigned int age;
int score;
char *name;
struct stu *next;
};

struct stu *init_list()
{
struct stu *pList = NULL;

……
[/Quote]
谢谢啊,你这个从头开始插入的,但是参数设置的也是指针,我后来改成传指针参数是正确的,但我想问传结构体变量为什么会出错呢。
「已注销」 2012-09-05
  • 打赏
  • 举报
回复
这个是从头部插入节点的代码

#include <stdio.h>
#include <stdlib.h>

struct stu
{
unsigned int age;
int score;
char *name;
struct stu *next;
};

struct stu *init_list()
{
struct stu *pList = NULL;

pList = (struct stu *)malloc(sizeof(struct stu));
if (NULL != pList)
{
pList->age = 0;
pList->score = 0;
pList->name = "";
pList->next = NULL;
}
return pList;
}

struct stu *add_node(struct stu *list,struct stu *node)
{
struct stu *head=list;

if (NULL != head->next)
{
node->next = head;
head = node;
}
else
{
node->next = NULL;
head = node;
}

return head;
}

int main(void)
{
struct stu node={11,22,"jack"};
struct stu *list,*p,*q;

list = init_list();
list = add_node(list,&node);
p = list;

for(q=p; q!=NULL; q=q->next)
{
printf("age:\t %d\t",q->age);
printf("score:\t %d\t",q->score);
printf("name:\t %s\n",q->name);
}
getchar();
return 0;
}
BYD123 2012-09-05
  • 打赏
  • 举报
回复
1)按照你的结构,你的头结点是不放值的;
2)因你的初始化代码没有贴出来,猜想p = list->next;才对。
3)说明一点,add_node第二个参数不要传结构体,实践的做法是传指针。可以参考我的链表实现:
http://blog.csdn.net/byd123/article/details/7879412
renzhenshiduoa 2012-09-05
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
其实也就是说你处理的是链表非空的情况,但是未处理链表为空的情况
[/Quote]
哈哈,是啊,非常感谢。
Gloveing 2012-09-04
  • 打赏
  • 举报
回复
应该是 list=初始化化函数,这里就没有贴出来;
这里错了,你还是全贴吧
Gloveing 2012-09-04
  • 打赏
  • 举报
回复
应该是 list=初始化化函数,这里就没有贴出来;
这里错了,你还是全贴吧

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧