单链表的实现

单链表

cpp 复制代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct lianbiao
{
	int num;
	struct lianbiao* next;
}LB;
//头插 
void toucha(LB** pc, int x)
{
	LB* newnode = (LB*)malloc(sizeof(LB));
	newnode->num = x;
	newnode->next = NULL;
	if (*pc == NULL)
	{
		*pc = newnode;
	}
	else
	{
		newnode->next = *pc;
		*pc = newnode;
	}
}

//打印 
void Print(LB* pc)
{
	LB* cur = pc;
	while (cur)
	{
		printf("%d ", cur->num);
		cur = cur->next;
	}
	printf("null\n");
}

//尾插 
void weicha(LB** pc, int x)//尾插
{
	LB* newnode = (LB*)malloc(sizeof(LB));
	newnode->num = x;
	newnode->next = NULL;
	if (*pc == NULL)
	{
		*pc = newnode;
	}
	else
	{
		//法1 
//		//找尾	
//		LB*cur=*pc;
//		while(cur->next!=NULL)
//		{
//			cur=cur->next;			
//		}
//		cur->next=newnode;	

		//法二 
		LB* cur = *pc;//因为改变的是地址,所以打印时首元素地址也变了,
		//所以要存一下,开头地址 
		while ((*pc)->next != NULL)
		{
			(*pc) = (*pc)->next;
		}

		(*pc)->next = newnode;
		*pc = cur;
	}
}
//头删
void toushan(LB** pc)
{
	if (*pc == NULL)
	{
		return;
	}
	else
	{
		LB* cur = *pc;
		*pc = cur->next;
		free(cur);
		cur = NULL;
	}

}
//尾 删 ,3种情况 
void weishan(LB** pc)
{
	if (*pc == NULL)//为空 
	{
		return;
	}


	if ((*pc)->next == NULL)//只有一个节点 
	{
		free(*pc);
		*pc = NULL;
	}

	else//多个节点 
	{
		LB* cur = *pc;

		LB* tail = NULL;
		while (cur->next != NULL)
		{
			tail = cur;
			cur = cur->next;
		}

		free(cur);
		cur = NULL;

		tail->next = NULL;//最重要一步,尾删后,要把删的前一个赋成NULL 
	}

}

//查找和修改 
LB* chazhao(LB* pc, int x)
{
	LB* cur = pc;
	while (cur)
	{
		if (cur->num == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

//在pos之前插入
void posqiancha(LB**pc,LB*pos,int x)
{
	if (pos==*pc)//当需要插入的位置在开头,直接头插
	{
		toucha(pc, x);//pc本来传过来的时候就是&tou,所以直接传pc
	}
	else
	{
		LB* newnode = (LB*)malloc(sizeof(LB));
		newnode->num = x;
		newnode->next = NULL;

		LB* cur = *pc;
		while (cur->next != pos)
		{
			cur = cur->next;
		}
		cur->next = newnode;
		newnode->next = pos;
	}
}
//pos位置删除
void posshan(LB** pc, LB* pos)
{
	if (*pc == pos)
	{
		LB* cur = *pc;
		*pc = cur->next;
		free(cur);
		cur = NULL;

		//头删toushan(pc,pos);
	}

	else
	{
		LB* cur = *pc;
		while (cur->next != pos)
		{
			cur = cur->next;
		}
		cur->next = pos->next;
		free(pos);
		cur = cur->next;
	}
	
}

//pos后面插入
void poshoucha(LB**pc,LB*pos,int x)
{
	LB* newnode = (LB*)malloc(sizeof(LB));
	newnode->num = x;
	newnode->next;

	if (*pc == NULL)
	{
		toucha(pc, x);
	}

	else 
	{
		LB* cur = *pc;
		while (cur != pos)
		{
			cur = cur->next;
		}
		newnode->next = pos->next;
		pos->next = newnode;
	}
}

//pos位置后面删除
void poshoushan(LB**pc,LB*pos )
{
	//cur->next = pos->next->next;直接这样写,中间节点会消失
	//存一下要删除的节点
	LB* del = pos->next;
	pos->next = del->next;
	free(del);
	del = NULL;
}

	int main()
	{
		LB* tou = NULL;
	 //   toucha(&tou,1);//头插 
	//	Print(tou);//打印 

	//weicha(&tou, 2);//尾插 
	//weicha(&tou, 2);
	//weicha(&tou, 2);
	weicha(&tou, 2);
	weicha(&tou, 7);
	weicha(&tou, 9);
	weicha(&tou, 9);
	weicha(&tou, 9);

	/*Print(tou);*/

	//头删
	//toushan(&tou);
	//toushan(&tou);
	//	Print(tou);	

		//尾删 
	/*weishan(&tou);
	weishan(&tou);*/
	//	Print(tou);

		//查找和修改 
	LB* ret = chazhao(tou, 2);
	//ret->num *= 2;
	//Print(tou);

	//在pos之前插入
	//posqiancha(&tou,ret,3);
	//Print(tou);


	//pos位置删除
	//posshan(&tou,ret);
	//Print(tou);

	//pos后面插入
	/*poshoucha(&tou,ret,9);
	Print(tou);*/

	//pos位置后面删除
	poshoushan(&tou,ret);
	Print(tou);
	return 0;
}
相关推荐
故事和你9121 分钟前
洛谷-算法2-2-常见优化技巧3
开发语言·数据结构·c++·算法·深度优先·动态规划·图论
菜鸟5555527 分钟前
2025江西省CCPC省赛暨全国邀请赛(南昌)
数据结构·c++·算法·acm·思维·ccpc·xcpc
꧁细听勿语情꧂1 小时前
用队列实现栈、用栈实现队列,树、二叉树、满二叉树、完全二叉树,堆、向下向上调整算法、出堆入堆、堆排序
c语言·开发语言·数据结构·算法
周末也要写八哥1 小时前
什么是快速选择及案例分析
数据结构
Felven1 小时前
B. Make Almost Equal With Mod
数据结构·算法
数智化精益手记局1 小时前
拆解红牌作战的步骤:掌握红牌作战的步骤,解决现场管理难题
大数据·数据结构·人工智能·制造·精益工程
喜欢吃燃面2 小时前
Linux 信号保存机制深度解析:从内核数据结构到进程状态管理
linux·运维·数据结构·学习
hi_ro_a2 小时前
C++ 手撕 STL 底层:红黑树封装 mymap/myset
数据结构·c++·算法
求学的小高2 小时前
数据结构Day9(图的遍历、图应用及相关算法)
数据结构·笔记·考研
电科一班林耿超2 小时前
第 14 课:动态规划(DP)—— 算法思想的巅峰,面试的终极分水岭
数据结构·算法·动态规划