单链表的模拟实现

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

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

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;
}
相关推荐
爱吃苹果的日记本3 小时前
数据结构第四课—线性表Linear List
数据结构·学习
Lyyaoo.4 小时前
【动态规划】【待更新】
java·数据结构·算法
我不会起名字3224 小时前
一天一道算法题(35):电话号码的字母组合
java·数据结构·后端·python·leetcode·go·回溯
鹿角片ljp5 小时前
LeetCode 78:子集|回溯、选与不选、递归和path快照
java·数据结构·算法
YSL0701245 小时前
顺序表小补充
数据结构
wabs6666 小时前
关于二叉树【力扣101.对称二叉树的思考】
数据结构·c++·算法·leetcode·二叉树
LuminousCPP7 小时前
数据结构-排序(五):计数排序与排序专题阶段总结|从外部归并到线性排序,再看算法边界与选型
c语言·数据结构·笔记·算法·排序算法
linx2957 小时前
第七章 · 标准库容器、算法与 ranges
c语言·开发语言·数据结构·c++·算法
Logic1018 小时前
C语言/数据结构位运算题解:异或XOR找出独特数字的索引位置——成对数字在两侧
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质