鱼C论坛

 找回密码
 立即注册
查看: 4263|回复: 3

C语言的链表怎么插入结点

[复制链接]
发表于 2012-12-10 14:07:49 | 显示全部楼层 |阅读模式
5鱼币
本帖最后由 贱人一个 于 2012-12-10 17:38 编辑

#include "stdio.h"
#include "malloc.h"
#include "string.h"
struct str
{
char name[1];
int n;
struct str *next;
}ne[5];
void m(struct str *c)
{
struct str *p=NULL;
p=c;
int len;
char le[1];

for(;NULL!=p;p=p->next)
{
printf("字符 空格 数字 ");
scanf("%s %d",&le,&len);   //输入数据
p->n=len;
strcpy(p->name,le);
}
p=c;
for(;NULL!=p;p=p->next)
{
printf("%s %d\n",p->name,p->n);
}
}
void k(struct str *c)  //插入 节点
{
struct str *p=NULL;
p=c;
struct str *S=NULL;
S->next=p->next;
p->next=S;
for(;NULL!=p;p=p->next)
{
  printf("%s %d\n",p->name,p->n);
}


}
void main()
{
struct str *p=NULL;
p=&ne[0];
ne[0].next=&ne[1];
ne[1].next=&ne[2];
ne[2].next=&ne[3];
ne[3].next=&ne[4];
ne[4].next=NULL;
m(p);    //输入数据
k(p);    //插入节点
}

插入节点 错误

最佳答案

查看完整内容

错误出现在了k函数中,”S->next=p->next;p->next=S;“这里面,这个是单向链表,so,你如果想把p插入到S的后边,你就要这样写 p->next = S->next; s->next->p; 你如果先将S->next变成了p,那么后边你要用S之前的next就会找不到了。。。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-12-10 14:07:50 | 显示全部楼层
错误出现在了k函数中,”S->next=p->next;p->next=S;“这里面,这个是单向链表,so,你如果想把p插入到S的后边,你就要这样写 p->next = S->next; s->next->p;   你如果先将S->next变成了p,那么后边你要用S之前的next就会找不到了。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-12-10 14:18:47 | 显示全部楼层
r指针都没赋值。。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-12-10 18:22:02 | 显示全部楼层
错误的地方有很多啊···  void m(struct str *c)
{
struct str *p=NULL;
p=c;   //这里都是错误的。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-16 20:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表