最详细的链表的“增删查改“(C语言实现)

这篇博客介绍了如何使用C语言实现单链表的基本操作,包括创建节点、尾插、头插、尾删、头删、查找、插入和删除节点。还提供了一个简单的main函数来测试这些操作。博客通过源代码详细阐述了单链表的数据结构和操作方法。
摘要由CSDN通过智能技术生成



小伙伴可以自行调试哦

一、头文件(STlist.h)

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef int SListDataType;//定义int类型

typedef struct SListNode
{
	SListDataType data;
	struct SListNode* next;//存储下一个节点的信息
}SListNode;
//打印节点信息
void SListPrint(SListNode* phead);
//创建一个节点
SListNode* BuySListNode(SListDataType x);
//尾插一个节点
void SListPushBack(SListNode** pphead, SListDataType x);
//头插一个节点
void SListPushFront(SListNode** pphead, SListDataType x);
//尾删一个节点
void SListPopBack(SListNode** pphead);
//头删一个节点
void SListPopFront(SListNode** pphead);
//单链表查找
SListNode* SListFind(SListNode* phead, SListDataType x);
//在单链表pos之前插入x
void SListInsertBefore(SListNode** pphead, SListNode* pos, SListDataType x);
//在单链表pos之后插入x
void SListInsertAfter(SListNode* pos, SListDataType x);
//单链表删除pos位置之前的值
void SListEraseBefore(SListNode** pphead, SListNode* pos);
//单链表删除pos位置之后的值
void SListEraseAfter(SListNode* pos);
//销毁单链表
void SListDestory(SListNode** pphead);

二、实现函数(STlist.c)

assert:断言

#include "STlist.h"

void SListPrint(SListNode* phead)
{
	SListNode* cur = phead;
	while (cur != NULL)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

//创建一个新节点
SListNode* BuySListNode(SListDataType x)
{
	SListNode* newnode = malloc(sizeof(SListNode));
	if (newnode == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	else
	{
		newnode->data = x;
		newnode->next = NULL;
	}
	return newnode;
}

void SListPushBack(SListNode** pphead, SListDataType x)
{
	assert(pphead);
	SListNode* newnode = BuySListNode(x);
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		SListNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}

void SListPushFront(SListNode** pphead, SListDataType x)
{
	assert(pphead);
	SListNode* newnode = BuySListNode(x);
	SListNode* cur = *pphead;
	newnode->next = cur;
	*pphead = newnode;
}

void SListPopBack(SListNode** pphead)
{
	assert(pphead);
	if (*pphead == NULL)
	{
		return;
	}
	else if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		SListNode* tail = *pphead;
		SListNode* prev = NULL;
		while (tail->next != NULL)
		{
			prev = tail;
			tail = tail->next;
		}
		free(tail);
		tail = NULL;
		prev->next = NULL;
	}
}

void SListPopFront(SListNode** pphead)
{
	assert(pphead);
	if (*pphead == NULL)
	{
		return;
	}
	else
	{
		SListNode* cur = *pphead;
		*pphead = cur->next;
		free(cur);
		cur = NULL;
	}
}

SListNode* SListFind(SListNode* phead, SListDataType x)
{
	assert(phead);
	SListNode* cur = phead;
	while (cur->next != NULL)
	{
		if (x == cur->data)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

void SListInsertBefore(SListNode** pphead, SListNode* pos, SListDataType x)
{
	assert(pphead);
	assert(pos);
	if (*pphead == pos)
	{
		SListPushFront(pphead, x);
	}
	else
	{
		SListNode* newnode = BuySListNode(x);
		SListNode* prev = *pphead;
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		newnode->next = pos;
		prev->next = newnode;
	}
}

void SListInsertAfter(SListNode* pos, SListDataType x)
{
	//推荐方法1
	assert(pos);
	SListNode* next = pos->next;
	SListNode* newnode = BuySListNode(x);
	pos->next = newnode;
	newnode->next = next;
}
//方法2
//void SListEraseAfter(SListNode* pos)
//{
//	assert(pos);
//	SListNode* next = pos->next;
//	pos->next = pos->next->next;
//	free(next);
//}

void SListEraseAfter(SListNode* pos)
{
	assert(pos);
	SListNode* next = pos->next;
	if (next != NULL)
	{
		SListNode* nextnext = next->next;
		free(next);
		next = NULL;
		pos->next = nextnext;
	}
}

void SListEraseBefore(SListNode** pphead, SListNode* pos)
{
	assert(pphead);
	assert(pos);
	if (*pphead == pos)
	{
		SListPopFront(pphead);
	}
	else
	{
		SListNode* prev = *pphead;
		SListNode* cur = *pphead;
		while (prev->next != pos)
		{
			cur = prev;
			prev = prev->next;
		}
		free(prev);
		prev = NULL;
		cur->next = pos;
	}
}

void SListDestory(SListNode** pphead)
{
	assert(pphead);
	SListNode* cur = *pphead;
	while (cur)
	{
		SListNode* next = cur->next;
		free(cur);
		cur = next;
	}
	*pphead = NULL;
}

三、main函数

#include "STlist.h"

void TestSList1()
{
	SListNode* slist = NULL;
	SListNode* n1 = malloc(sizeof(SListNode));
	SListNode* n2 = malloc(sizeof(SListNode));
	SListNode* n3 = malloc(sizeof(SListNode));
	slist = n1;
	n1->data = 1;
	n2->data = 2;
	n3->data = 3;
	n1->next = n2;
	n2->next = n3;
	n3->next = NULL;
	SListPrint(slist);
	SListPushBack(&slist, 4);
	SListPrint(slist);
	SListPushFront(&slist, 0);
	SListPrint(slist);
	SListPopBack(&slist);
	SListPrint(slist);
	SListPopFront(&slist);
	SListPrint(slist);
	SListNode* find =  SListFind(slist, 2);
	if (find == NULL)
	{
		printf("没找到\n");
	}
	else
	{
		SListInsertAfter(find, 6);
		SListEraseAfter(find);
		SListInsertBefore(&slist, find, 30);
		SListEraseBefore(&slist, find);
	}
	SListPrint(slist);
	SListDestory(&slist);
}

int main()
{
	TestSList1();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值