【数据结构与算法】链表(上)

记录自己所学,无详细讲解

无头单链表实现

1.项目目录文件

2.头文件 Slist.h

复制代码
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
struct Slist
{
	int data;
	struct Slist* next;
};
typedef struct Slist Slist;
//初始化
void SlistInit(Slist** phead);
//新建一个节点
Slist* Buynode(int n);
//尾插
void SlistPushback(Slist** phead, int n);
//头插
void SlistPushfront(Slist** phead, int n);
//尾删
void SlistPopback(Slist** phead);
//头删
void SlistPopfront(Slist** phead);
//查询是否存在
Slist* IsFind(Slist** phead, int n);
// 修改某个节点
void SlistModify(Slist* pos, int n);
// 删除某个节点
void SlistDel(Slist**phead, Slist* pos);
//某个节点前插入
void SlistInsertfront(Slist** phead, Slist* pos, int n);
//某个节点后插入
void SlistInsertback(Slist** phead, Slist* pos, int n);
//销毁单链表
void SlistDestory(Slist** phead);
//打印
void SlistPrint(Slist** phead);

3.函数定义源文件 Slist.c

复制代码
#include "Slist.h"
void SlistInit(Slist** phead)
{
	*phead = NULL;
   //(*phead)->next = NULL;
}
Slist* Buynode(int n)
{
	Slist* newnode = (Slist*)malloc(sizeof(Slist));
	assert(newnode);
	newnode->next = NULL;
	newnode->data = n;
	return newnode;
}
void SlistPushback(Slist** phead, int n)
{
	Slist* newnode = Buynode(n);
	if (*phead == NULL)
	{
		*phead = Buynode(n);
		printf("%d尾插成功\n", (*phead)->data);
	}
	else
	{
		Slist* cur = *phead;
		while (cur->next!=NULL)
		{
			cur = cur->next;
		}
		cur->next = newnode;
		printf("%d尾插成功\n", (newnode)->data);
	}
}
void SlistPushfront(Slist** phead, int n)
{
	Slist* newnode = Buynode(n);
	if (*phead == NULL)
	{
		*phead = newnode;
		printf("%d头插成功\n", (*phead)->data);
	}
	else
	{
		newnode->next = *phead;
		*phead = newnode;
		printf("%d头插成功\n", (*phead)->data);

	}
}
void SlistPopback(Slist** phead)
{
	assert(*phead);
	Slist* cur = *phead;
	if ((*phead)->next == NULL)
	{
		printf("%d尾删成功\n",(*phead)->data);
		free(*phead);
		*phead = NULL;
	}
	else
	{
		while ((cur->next)->next != NULL)
		{
			cur = cur->next;
		}
		printf("%d尾删成功\n", (cur->next)->data);
		free(cur->next);
		cur->next = NULL;
	}
}
void SlistPopfront(Slist** phead)
{
	assert(*phead);
	printf("%d头删成功\n", (*phead)->data);
	Slist* cur = *phead;
	*phead = (*phead)->next;
	free(cur);
	cur = NULL;
}
Slist* IsFind(Slist** phead,int n)
{
	assert(*phead);
	Slist* cur = *phead;
	while (cur != NULL)
	{
		if (cur->data == n)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
void SlistModify(Slist* pos, int n)
{
	if (pos == NULL)
	{
		printf("修改失败,未找到节点\n");
	}
	else
	{
		pos->data = n;
		printf("修改成功\n");
	}
}
void SlistDel(Slist** phead,Slist* pos)
{
	assert(*phead);
	if (pos == NULL)
	{
		printf("删除失败,未找到节点\n");
	}
	else if (*phead == pos)
	{
		Slist* cur = *phead;
		*phead = (*phead)->next;
		free(cur);
		cur = NULL;
		printf("删除节点成功\n");
	}
	else
	{
		Slist* cur = *phead;
		while (cur->next!= pos)
		{
			cur = cur->next;
		}
		cur->next = pos->next;
		free(pos);
		pos = NULL;
		printf("删除节点成功\n");
	}
}
void SlistInsertfront(Slist** phead, Slist* pos, int n)
{
	assert(*phead);
	Slist* newnode = Buynode(n);
	Slist* cur = *phead;
	if (pos == NULL)
	{
		printf("插入失败,未找到节点\n");
	}
	else if (cur->next == NULL)
	{
		newnode->next = cur;
		*phead = newnode;
		printf("插入成功\n");
	}
	else if (cur == pos)
	{
		*phead = newnode;
		newnode->next = cur;
		printf("插入成功\n");
	}
	else
	{
		while (cur->next != pos)
		{
			cur = cur->next;
		}
		cur->next = newnode;
		newnode->next = pos;
		printf("插入成功\n");
	}
}
void SlistInsertback(Slist** phead, Slist* pos,int n)
{
	assert(*phead);
	Slist* newnode = Buynode(n);
	Slist* cur = *phead;
	if (pos == NULL)
	{
		printf("插入失败,未找到节点\n");
	}
	else
	{
		while (cur != pos)
		{
			cur = cur->next;
		}
		Slist* ccur = cur->next;
		cur->next = newnode;
		newnode->next = ccur;
		printf("插入成功\n");
	}
}
void SlistDestory(Slist** phead)
{
	Slist* cur = *phead;
	Slist* ccur = cur;
	while (cur != NULL)
	{
		ccur = cur;
		cur = cur->next;
		free(ccur);
		ccur = NULL;
	}
	*phead = NULL;
}
void SlistPrint(Slist** phead)
{
	assert(*phead);
	Slist* cur = *phead;
	while (cur != NULL)
	{
		printf("%3d", cur->data);
		cur = cur->next;
	}
}

4.函数调用测试源文件test.c

复制代码
#include "Slist.h"
int main()
{
	Slist * list;
	SlistInit(&list);
	SlistPushback(&list, 5);
	SlistPushback(&list, 4);
	SlistPushback(&list, 3);
	SlistPushback(&list, 2);
	SlistPushback(&list, 1);
	SlistPushfront(&list,6);
	SlistPushfront(&list,7);
	//SlistPopback(&list);
	//SlistPopback(&list);
	//SlistPopfront(&list);
	//SlistDel(&list,IsFind(&list, 1));
	//SlistModify(IsFind(&list, 10), 9);
	SlistInsertfront(&list, IsFind(&list, 7), 8);
	SlistInsertback(&list, IsFind(&list, 8), 10);
	//SlistPrint(&list);
	SlistDestory(&list);
	SlistPrint(&list);
}
相关推荐
患得患失9497 分钟前
【前端WebSocket】心跳功能,心跳重置策略、双向确认(Ping-Pong) 以及 指数退避算法(Exponential Backoff)
前端·websocket·算法
海砥装备HardAus10 分钟前
飞控算法中双环串级PID深度解析:角度环与角速度环的协同机制
stm32·算法·无人机·飞控·串级pid
宵时待雨11 分钟前
优选算法专题1:双指针
数据结构·c++·笔记·算法·leetcode
zsc_11813 分钟前
pvz3解码小游戏求解算法
算法
汀、人工智能15 分钟前
[特殊字符] 第107课:LRU缓存(最后一课[特殊字符])
数据结构·算法·链表·数据库架构·哈希表·lru缓存
数据知道16 分钟前
claw-code 源码分析:大型移植的测试哲学——如何用 unittest 门禁守住「诚实未完成」的口碑?
开发语言·python·ai·claude code·claw code
数据知道21 分钟前
claw-code 源码分析:结构化输出与重试——`structured_output` 一类开关如何改变「可解析性」与失败语义?
算法·ai·claude code·claw code
tankeven21 分钟前
HJ172 小红的矩阵染色
c++·算法
小堃学编程24 分钟前
【项目实战】基于protobuf的发布订阅式消息队列(2)—— 线程池
java·开发语言
2301_8227032025 分钟前
Flutter 框架跨平台鸿蒙开发 - 智能植物生长记录应用
算法·flutter·华为·harmonyos·鸿蒙