单链表的模拟实现

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

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

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;
}
相关推荐
风落青芜18 分钟前
数据结构:堆的概念与实现
c语言·开发语言·数据结构·算法
老师我太想进步了20262 天前
【数据结构-8】栈的实现
c语言·数据结构·算法
【心态好不摆烂】3 天前
数据结构基础精讲3
数据结构·算法
岑梓铭3 天前
《考研408数据结构》第七章(B树 和 B+树)复习笔记
数据结构·笔记·b树·考研·408
岑梓铭3 天前
《考研408数据结构》第八章(排序)复习笔记
数据结构·笔记
nice_lcj5204 天前
LRU 缓存入门:它是什么、怎么用、面试怎么手写
java·数据结构·缓存·面试·lru
꧁细听勿语情꧂4 天前
直接选择排序,堆排序,冒泡排序,递归快速排序、hoare版本、挖坑法、lomuto前后指针,非递归快速排序
c语言·开发语言·数据结构·算法
Pniubi5 天前
关于算法用现成数据结构和手撕
数据结构
老师我太想进步了20265 天前
【数据结构-6】算法美学之单链表(1)
数据结构·算法
希望我万事顺遂5 天前
力扣hot100-滑动窗口最大值
数据结构·算法·leetcode