链表从指定节点后方插入新节点
在指定数据后面插入节点的案例:在Test结构体(这个结构体的data为2)后面插入一个Test结构体(data=6)
#include<stdio.h> struct Test { int data; struct Test * next; }; printfLink(struct Test * head) { while(head!=NULL) { printf("%d\n",head->data); head=head->next; } putchar('\n'); } void InsertNode(struct Test *head,int data,struct Test *new) { struct Test * p=head; while(p!=NULL) { if(p->data==data) { new->next=p->next; p->next=new; } p=p->next; } } int main() { struct Test t1={1,NULL}; struct Test t2={2,NULL}; struct Test t3={3,NULL}; t1.next=&t2; t2.next=&t3; printf("before insert:\n"); printfLink(&t1); struct Test new={6,NULL}; InsertNode(&t1,2,&new); printf("after insert:\n"); printfLink(&t1); }
输出结果:
before insert:
1
2
3
after insert:
1
2
6
3
4556
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端