单链表的模拟实现

实现了不带哨兵位的单链表

注意:凡是涉及到头节点需要改变的一定要传二级指针,代码如下,仅供参考

SList.h

cpp 复制代码
#pragma once

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


typedef int SLTDataType;
typedef struct SListNode
{
	SLTDataType data;
	struct SListNode* next;
	//
}SLTNode;


void SLTPrint(SLTNode* phead);

//头部插入删除/尾部插入删除
void SLTPushBack(SLTNode** pphead, SLTDataType x);
void SLTPushFront(SLTNode** pphead, SLTDataType x);
void SLTPopBack(SLTNode** pphead);
void SLTPopFront(SLTNode** pphead);

//查找
SLTNode* SLTFind(SLTNode* phead, SLTDataType x);
//在指定位置之前插入数据
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);
//删除pos节点
void SLTErase(SLTNode** pphead, SLTNode* pos);
//在指定位置之后插入数据
void SLTInsertAfter(SLTNode* pos, SLTDataType x);
//删除pos之后的节点
void SLTEraseAfter(SLTNode* pos);
//销毁链表
void SListDesTroy(SLTNode** pphead);

SList.c

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1


#include "SList.h"


void SLTPrint(SLTNode* phead)
{
	SLTNode* pcur = phead;
	while (pcur)
	{
		printf("%d->", pcur->data);
		pcur = pcur->next;
	}
	printf("NULL\n");
}

SLTNode* BuyNode(SLTDataType x)
{
	SLTNode* tmp = (SLTNode*)malloc(sizeof(SLTNode));
	if (tmp == NULL)
	{
		perror("malloc");
		exit(1);
	}
	tmp->data = x;
	tmp->next = NULL;
	return tmp;
}

//头部插入删除/尾部插入删除
void SLTPushBack(SLTNode** pphead, SLTDataType x)
{
	assert(pphead);
	SLTNode* pcur = *pphead;
	if (pcur == NULL)
	{
		*pphead = BuyNode(x);
	}
	else
	{
		while (pcur->next)
		{
			pcur = pcur->next;
		}
		pcur->next = BuyNode(x);
	}
	

}
void SLTPushFront(SLTNode** pphead, SLTDataType x)
{
	assert(pphead);
	SLTNode* pcur = *pphead;
	SLTNode* newnode= BuyNode(x);
	newnode->next = *pphead;
	*pphead = newnode;
}
void SLTPopBack(SLTNode** pphead)
{
	assert(pphead && *pphead);
	SLTNode* pcur = *pphead;
	if (pcur->next == NULL)
	{
		free(pcur);
		pcur = NULL;
		*pphead = NULL;
	}
	else
	{
		while (pcur->next->next)
		{
			pcur = pcur->next;
		}
		SLTNode* del = pcur->next;
		free(del);
		del = NULL;
		pcur->next = NULL;
	}
	

}
void SLTPopFront(SLTNode** pphead)
{
	assert(pphead && *pphead);
	SLTNode* newhead = (*pphead)->next;
	free(*pphead);
	*pphead = newhead;
}

//查找
SLTNode* SLTFind(SLTNode* phead, SLTDataType x)
{
	assert(phead);
	SLTNode* pcur = phead;
	while (pcur)
	{
		if (pcur->data == x)
		{
			return pcur;
		}
		pcur = pcur->next;
	}
	printf("没找到%d\n", x);
	return NULL;
}

//在指定位置之前插入数据
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
	assert(pphead && *pphead);
	assert(pos);
	SLTNode* pcur = *pphead;
	SLTNode* prev = *pphead;
	while (pcur != pos)
	{
		prev = pcur;
		pcur = pcur->next;
	}
	SLTNode* newnode = BuyNode(x);
	if (pcur == prev)
	{
		newnode->next = pos;
		*pphead = newnode;
	}
	else
	{
		newnode->next = pos;
		prev->next = newnode;
	}
	
}

//删除pos节点
void SLTErase(SLTNode** pphead, SLTNode* pos)
{
	assert(pphead && *pphead);
	assert(pos);
	SLTNode* pcur = *pphead;
	SLTNode* prev = *pphead;
	while (pcur != pos)
	{
		prev = pcur;
		pcur = pcur->next;
	}
	if (prev == pcur)
	{
		*pphead = pcur->next;
		free(pcur);
		pcur = prev = NULL;
	}
	else
	{
		prev->next = pcur->next;
		free(pos);
		pos = NULL;
		pcur = NULL;
	}
	

}

//在指定位置之后插入数据
void SLTInsertAfter(SLTNode* pos, SLTDataType x)
{
	assert(pos);
	SLTNode* newnode = BuyNode(x);
	SLTNode* new_next = pos->next;
	pos->next = newnode;
	newnode->next = new_next;
}

//删除pos之后的节点
void SLTEraseAfter(SLTNode* pos)
{
	assert(pos && pos->next);
	SLTNode* new_next = pos->next->next;
	free(pos->next);
	pos->next = new_next;

}

//销毁链表
void SListDesTroy(SLTNode** pphead)
{
	assert(pphead);
	SLTNode* pcur = *pphead;
	while (pcur)
	{
		SLTNode* del = pcur;
		pcur = pcur->next;
		free(del);
		del = NULL;
		
	}
	*pphead = NULL;
}

test.c

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1


#include "SList.h"


void test01()
{

	//SLTNode sl;
	SLTNode* psl =NULL;

	SLTPushBack(&psl, 1);
	SLTPrint(psl);
	/*SLTErase(&psl, SLTFind(psl, 1));
	SLTPrint(psl);*/
	SLTPushBack(&psl, 2);
	SLTPrint(psl);
	SLTPushBack(&psl, 3);
	SLTPrint(psl);
	SLTPushBack(&psl, 4);
	SLTPrint(psl);
	SLTPushFront(&psl, 5);
	SLTPrint(psl);
	SLTPushFront(&psl, 6);
	SLTPrint(psl);
	SLTPushFront(&psl, 7);
	SLTPrint(psl);
	SLTPopBack(&psl);
	SLTPrint(psl);
	SLTPopBack(&psl);
	SLTPrint(psl);
	SLTPopBack(&psl);
	SLTPrint(psl);
	SLTPopBack(&psl);
	SLTPrint(psl);
	SLTInsert(&psl, SLTFind(psl,7), 8);
	SLTPrint(psl);
	SLTErase(&psl, SLTFind(psl, 5));
	SLTPrint(psl);
	SLTInsertAfter(SLTFind(psl, 8), 9);
	SLTPrint(psl);
	SLTEraseAfter(SLTFind(psl, 7));
	SLTPrint(psl);
	SListDesTroy(&psl);
	/*SLTPopFront(&psl);
	SLTPrint(psl);
	SLTPopFront(&psl);
	SLTPrint(psl);
	SLTPopFront(&psl);
	SLTPrint(psl);*/
}


int main()
{

	test01();

	return 0;
}
相关推荐
写后端的胖头鱼10 小时前
一文详解B+树原理(附场景+示例)
数据结构·b树
All for pursuit.13 小时前
【位运算-1】338.比特位计数
数据结构·c++·算法·leetcode
xxwl58515 小时前
数据结构知识点和代码实现总结(C语言实现)
c语言·开发语言·数据结构
mokongJ20 小时前
17 份异构销售数据的合并清洗方案:30 万行处理耗时 9.64 秒
数据结构
蛋蛋的就会顺顺的20 小时前
hot100——二叉树
java·数据结构·算法·leetcode·力扣
List<String> error_P1 天前
freertos数据结构--链表
数据结构·链表
longlongzihan1 天前
LeetCode 128. 最长连续序列 —— 从排序到哈希集合
数据结构·c++·算法·leetcode·排序算法·哈希表
mmmmath_31 天前
二叉树の一些知识
数据结构·算法
程序员清风1 天前
数据类型转换与日期时间处理
数据结构·pandas
Logic1011 天前
C语言/数据结构位运算题解:异或XOR找出英雄队伍中的“独特战斗力“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质