单链表的模拟实现

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

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

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;
}
相关推荐
INGNIGHT7 小时前
91 · 最小调整代价(序列dp)
数据结构·算法·leetcode
从小白到软工11 小时前
5-2 从查表到控件:造按钮
c语言·开发语言·数据结构·经验分享·ai编程
David猪大卫13 小时前
【C++修炼】STL容器—stack与queue
c语言·开发语言·数据结构·c++·经验分享·笔记·学习
瑶池酒剑仙13 小时前
C++变量初始化五法完全指南:默认/拷贝/直接/列表/值初始化与窄化转换陷阱(青云之志)
c语言·开发语言·数据结构·c++·算法·visual studio
秋天的怀念_David15 小时前
听故事学语言 · 数据结构篇 · 序章一 算法之问
数据结构·程序人生·算法
-dzk-15 小时前
【链表】LC 25.K 个一组翻转链表
数据结构·链表
careathers16 小时前
【数据结构】基于比较的排序算法 - 插入排序
java·数据结构·排序算法
MicrosoftCloud16 小时前
数据结构 07(上)|查找:顺序、折半、分块,ASL 到底怎么
数据结构·查找·顺序查找·折半查找·分块查找·平均查找长度·asl
Mortalbreeze16 小时前
0 - 1 BFS 算法详解:从基本原理到经典题目建模
数据结构·c++·算法·图论·宽度优先
shehuiyuelaiyuehao1 天前
算法56,重排链表
数据结构·算法·链表