简单不先于复杂,而是在复杂之后。
文章目录
1. 链表
1.1 链表的概念及结构
1.2 链表的分类
1.3 无头单向非循环链表的实现
Slist.h
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef int SLTDataType;
typedef struct SlistNode
{
	SLTDataType data;
	struct SListNode* next;
}SLTNode,*PSLTNode;
//以下三种形式等价
//SLTNode*
//PSLTNode
//struct SlistNode
//动态申请一个结点
SLTNode* BuySLTNode(SLTDataType x);
//打印链表
void SListPrint(SLTNode* phead);
//void SListPrint(PSLTNode phead);
//销毁链表
//不及时销毁链表是一种内存泄露
void SListDestory(SLTNode** pphead);
//头插
void SListPushFront(SLTNode** pphead, SLTDataType x);
//尾插
void SListPushBack(SLTNode** pphead, SLTDataType x);
//尾删
void SListPopBack(SLTNode** pphead);
//头删
void SListPopFront(SLTNode** pphead);
//查找
//可以充当修改
SLTNode* SlistFind(SLTNode* phead, SLTDataType x);
//在pos之前插入
void SlistInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);
//在pos之后插入
void SlistInsertAfter(SLTNode* pos, SLTDataType x);
//删除pos位置
void SlistErase(SLTNode** pphead, SLTNode* pos);
//删除pos后面位置
void SlistEraseAfter(SLTNode* pos, SLTDataType x);
Slist.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Slist.h"
void SListPrint(SLTNode* phead)
{
	//不能断言,因为phead为空是正常情况,表示空链表
	SLTNode* cur = phead;
	while (cur != NULL)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}
SLTNode* BuySLTNode(SLTDataType x)
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}
void SListPushFront(SLTNode** pphead, SLTDataType x)
{
	assert(pphead);
	SLTNode* newnode = BuySLTNode(x);
	newnode->next = *pphead;
	*pphead = newnode;
}
void SListPushBack(SLTNode** pphead, SLTDataType x)
{
	assert(pphead);
	SLTNode* newnode = BuySLTNode(x);
	//1. 链表为空
	//2. 链表非空
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		//找尾
		SLTNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
		//尾插改变的是结构体成员,所以不用二级指针(结构体指针的指针),用结构体指针
	}
}
void SListPopBack(SLTNode** pphead)
{
	assert(pphead);
	//1.多个节点
	//2.一个节点
	if (*pphead == NULL)
	{
		return;
	}
	if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		SLTNode* prev = NULL;
		SLTNode* tail = *pphead;
		while (tail->next != NULL)
		{
			prev = tail;
			tail = tail->next;
		}
		prev->next = NULL;
		free(tail);
		tail = NULL;
	}
}
void SListPopFront(SLTNode** pphead)
{
	assert(pphead); 
	//温柔的检查
	if (*pphead == NULL)
	{
		return;
	}
	暴力检查
	//assert(*pphead != NULL);
	SLTNode* del = *pphead;
	*pphead = (*pphead)->next;
	free(del);
	del = NULL;
}
void SListDestory(SLTNode** pphead)
{
	assert(pphead);
	SLTNode* cur = *pphead;
	while (cur)
	{
		SLTNode* next = cur->next;
		free(cur);
		cur = next;
	}
	*pphead = NULL;
}
SLTNode* SlistFind(SLTNode* phead, SLTDataType x)
{
	SLTNode* cur = phead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
void SlistInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
	assert(pphead);
	assert(pos);
	if (pos == *pphead)
	{
		SListPushFront(pphead, x);
	}
	else
	{
		SLTNode* prev = *pphead;
		while (prev->next != pos)
		{
			prev = prev->next;
			//暴力检查,pos不在链表中,prev为空还没有找到pos说明pos传错了
			assert(prev);
		}
		SLTNode* newnode = BuySLTNode(x);
		prev->next = newnode;
		newnode->next = pos;
	}
}
void SlistInsertAfter(SLTNode* pos, SLTDataType x)
{
	assert(pos);
	SLTNode* newnode = BuySLTNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}
void SlistErase(SLTNode** pphead, SLTNode* pos)
{
	assert(pphead);
	assert(pos);
	if (*pphead == pos)
	{
		SListPopFront(pphead);
	}
	else
	{
		SLTNode* prev = *pphead;
		while (prev->next != pos)
		{
			prev = prev->next;
			//检查pos不是链表中节点,参数传错了
			assert(prev);
		}
		prev->next = pos->next;
		free(pos);
		
	}
}
void SlistInsertAfter(SLTNode* pos, SLTDataType x)
{
	assert(pos);
	if (pos->next == NULL)
	{
		return;
	}
	else
	{
		SLTNode* next = pos->next;
		pos->next = next->next;
		free(next);
	}
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Slist.h"
void TestSlist1()
{
	SLTNode* plist = NULL;
	SListPushFront(&plist, 1);
	SListPushFront(&plist, 2);
	SListPushFront(&plist, 3);
	SListPushFront(&plist, 4);
	SListPrint(plist);
}
void TestSlist2()
{
	SLTNode* plist = NULL;
	SListPushBack(&plist, 1);
	SListPushBack(&plist, 2);
	SListPushBack(&plist, 3);
	SListPushBack(&plist, 4);
	SListPrint(plist);
	//SListPopBack(&plist);
	//SListPopBack(&plist);
	//SListPopBack(&plist);
	//SListPopBack(&plist);
	//SListPopBack(&plist);
	//SListDestory(&plist);
	SLTNode* pos = SlistFind(plist, 3);
	if (pos)
	{	
		//修改
		//SlistInsert(&plist, pos, 20);
		SlistErase(&plist, pos);
		printf("找到了\n");
	}
	else
	{
		printf("没找到\n");
	}
	SListPrint(plist);
}
int main()
{
	//TestSlist1();
	TestSlist2();
	return 0;
}
(next);
 }
 }
`test.c`
```c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Slist.h"
void TestSlist1()
{
	SLTNode* plist = NULL;
	SListPushFront(&plist, 1);
	SListPushFront(&plist, 2);
	SListPushFront(&plist, 3);
	SListPushFront(&plist, 4);
	SListPrint(plist);
}
void TestSlist2()
{
	SLTNode* plist = NULL;
	SListPushBack(&plist, 1);
	SListPushBack(&plist, 2);
	SListPushBack(&plist, 3);
	SListPushBack(&plist, 4);
	SListPrint(plist);
	//SListPopBack(&plist);
	//SListPopBack(&plist);
	//SListPopBack(&plist);
	//SListPopBack(&plist);
	//SListPopBack(&plist);
	//SListDestory(&plist);
	SLTNode* pos = SlistFind(plist, 3);
	if (pos)
	{	
		//修改
		//SlistInsert(&plist, pos, 20);
		SlistErase(&plist, pos);
		printf("找到了\n");
	}
	else
	{
		printf("没找到\n");
	}
	SListPrint(plist);
}
int main()
{
	//TestSlist1();
	TestSlist2();
	return 0;
}










