当前位置:网站首页>C language structure linked list node insertion method (before and after)
C language structure linked list node insertion method (before and after)
2022-08-06 21:32:00 【Linux persuasion division CK】
封装一个函数 Implements the ability to insert new nodes from the specified node(You can choose from before or after)
函数原型如下:
struct Test* insertNode(struct Test *p,struct Test *new,int data,char* way)
The first parameter is the address of the head node of the original linked list
The second parameter is the new node address to insert
The third parameter is the position of the node to insert,Represented by the value of the structure(The actual item can be freely changed)
The fourth parameter is the insertion method(前:AHEAD / 后:BEHIND)
The idea of inserting nodes after the introduction is introduced:
一:Insert before:
1.If it is inserted before the head node,Then directly make the next node of the new node equal to the head node,Then go back to the new node
2.如果不是头节点,In the process of traversing the linked list, the current node is judgedP的下一个节点P->NEXTWhether the value of is the position to insert,如果是:
第一步:Then the new nodeNEXT等于P->NEXT
第二步:P的NEXT指向新节点.
代码如下:
struct Test* insertNodeAhead(struct Test *p,struct Test *new,int data)//Insert the node from before
{
struct Test *head=p;
if(head->data==data)//Insert before the first
{
new->next=head;
return new;
}
while(p->next!=NULL)
{
if(p->next->data==data)
{
new->next=p->next;
p->next=new;
return head;
}
p=p->next;
}
printf("no this data:%d\n",data);
return head;
}
二:Insert from behind:
Inserting from the back is simpler,Just judge one situation:
Determine the current node in the process of traversing the linked listPWhether the value of is the position to insert,如果是:
第一步:Then the new nodeNEXT等于P->NEXT
第二步:P的NEXT指向新节点.

代码如下:
struct Test* insertNodeBehind(struct Test *p,struct Test *new,int data)//Insert nodes from the back
{
struct Test* head=p;
while(p!=NULL)
{
if(p->data==data)
{
new->next=p->next;
p->next=new;
return head;
}
p=p->next;
}
printf("no this data:%d\n",data);
return head;
}
整体代码实现如下:
#include<stdio.h>
#include<string.h>
#define AHEAD "ahead"
#define BEHIND "behind"
struct Test
{
int data;
struct Test *next;
};
void printLink(struct Test *p)
{
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->next;
}
}
struct Test* insertNodeBehind(struct Test *p,struct Test *new,int data)//Insert nodes from the back
{
struct Test* head=p;
while(p!=NULL)
{
if(p->data==data)
{
new->next=p->next;
p->next=new;
return head;
}
p=p->next;
}
printf("no this data:%d\n",data);
return head;
}
struct Test* insertNodeAhead(struct Test *p,struct Test *new,int data)//Insert the node from before
{
struct Test *head=p;
if(head->data==data)//Insert before the first
{
new->next=head;
return new;
}
while(p->next!=NULL)
{
if(p->next->data==data)
{
new->next=p->next;
p->next=new;
return head;
}
p=p->next;
}
printf("no this data:%d\n",data);
return head;
}
struct Test* insertNode(struct Test *p,struct Test *new,int data,char* way)
{
struct Test *head=p;
int ret=-1;
if(strstr(way,"behind")!=NULL)
{
ret=1;
}
else if(strstr(way,"ahead")!=NULL)
{
ret=0;
}
switch(ret)
{
case 1:
head=insertNodeBehind(head,new,data);
break;
case 0:
head=insertNodeAhead(head,new,data);
break;
default:
printf("Parm error\n");
return NULL;
}
return head;
}
int main()
{
struct Test t1={
1,NULL};
struct Test t2={
2,NULL};
struct Test t3={
3,NULL};
struct Test t4={
4,NULL};
struct Test t5={
5,NULL};
struct Test new1={
8,NULL};
struct Test new2={
0,NULL};
struct Test new3={
100,NULL};
struct Test *head=&t1;
t1.next=&t2;
t2.next=&t3;
t3.next=&t4;
t4.next=&t5;
printLink(head);
head=insertNode(head,&new1,3,BEHIND);
head=insertNode(head,&new2,5,AHEAD);
head=insertNode(head,&new3,1,AHEAD);
printf("after insert:\n");
printLink(head);
}
运行结果如下:
边栏推荐
猜你喜欢

为什么要叠多层的隐藏层

Liunx文件目录操作命令(cd、pwd、ls、mkdir、rmdir)

亿流量大考(5):百亿流量全链路99.99%高可用架构最佳实践

The predicament of e-commerce giants is reversed, and Alibaba and Amazon have the same goal

0x000000f4蓝屏是怎么回事 win7蓝屏0x000000f4解决方法

【WPF】级联Combobox及其与ListView的联动

distributed theory

June Broker Gold Works Picks

从To C到To B、To G,多多云科技如何实现转型

从 VLAN 到 IPVLAN: 聊聊虚拟网络设备及其在云原生中的应用
随机推荐
4G DTU的特点及应用方法
微信小程序发布动态页面模板
leetcode 769. Max Chunks To Make Sorted 最多能完成排序的块(中等)
A Preliminary Study on Netgen Volume Meshing and Display of CAD Based on OSG+OCC
Before start of result set报错(已解决)
LoRa组网解决方案
如何使用 saplink 安装其他网站上提供的 ABAP 程序试读版
What can the "Ninth-Order Evaluation Model" bring?
【pytorch 模型量化方法总结】
Tomato学习笔记-Vscode配置Makefile(使用task.jason和launch.jason)
【AI芯片CAISA】
Personal Information Protection Law of the People's Republic of China
win32概述及框架
吃透Chisel语言.33.Chisel进阶之硬件生成器(二)——Chisel组合逻辑电路生成:以BCD编码表为例
8086CPU标志寄存器
Harvested the "Innovation Pioneer" of the first computing power conference, what trends did Jinan Supercomputing "Data Storage Cluster System" reveal?
2. 双链表的定义+ 代码实现
2022-8-6 集合容器
STM32MP157A | 05 - driver development based on RGB LCD LTDC interface drivers
oracle关联表查询用in关联查询用字符隔开逗号隔开