C语言结构体链表 节点插入方法(从前和从后)

封装一个函数 实现能从指定节点插入新节点(可选择从前或从后)
函数原型如下:

struct Test* insertNode(struct Test *p,struct Test *new,int data,char* way) 

第一个参数是原链表头节点地址
第二个参数是要插入的新节点地址
第三个参数是要插入的节点的位置,用结构体的值来表示(实际项目可自由更改)
第四个参数是插入方式(前:AHEAD / 后:BEHIND)

介绍后插入节点的思路:
一:从前插入:
1.如果是从头节点前插入,则直接令新节点的下一个等于头节点,然后返回新节点即可
在这里插入图片描述2.如果不是头节点,则在遍历链表的过程中判断现节点P的下一个节点P->NEXT的值是否是要插入的位置,如果是:
第一步:那么新节点的NEXT等于P->NEXT
第二步:P的NEXT指向新节点。
在这里插入图片描述
在这里插入图片描述代码如下:

struct Test* insertNodeAhead(struct Test *p,struct Test *new,int data)//从前插入节点
{
	struct Test *head=p;
	if(head->data==data)//插入在第一个之前
	{
		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;
}

二:从后插入:
从后插入比较简单,只需判断一种情况:
在遍历链表的过程中判断现节点P的值是否是要插入的位置,如果是:
第一步:那么新节点的NEXT等于P->NEXT
第二步:P的NEXT指向新节点。

在这里插入图片描述在这里插入图片描述代码如下:

struct Test* insertNodeBehind(struct Test *p,struct Test *new,int data)//从后插入节点
{
	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)//从后插入节点
{
	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)//从前插入节点
{
	struct Test *head=p;
	if(head->data==data)//插入在第一个之前
	{
		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);

}

运行结果如下:
在这里插入图片描述

  • 11
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值