Lesson2无头单向非循环链表(中)

1.链表

1.1链表的概念及结构

概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的 。

 1.2链表的分类

实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:

1. 单向或者双向

2. 带头或者不带头

3. 循环或者非循环

虽然有这么多的链表的结构,但是我们实际中最常用还是两种结构:

1. 无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。
2. 带头双向循环链表:结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都是带头双向循环链表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带来很多优势,实现反而简单了,后面我们代码实现了就知道了。 

2.链表的实现

#pragma once

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
//写入访问权限冲突 解决方法:#include <stdlib.h>

//无头+单向+非循环链表增删查改实现
typedef int SLTDateType;
typedef struct SListNode
{
	SLTDateType data;//数据域--存储数据
	struct SListNode* next;//指针域--存放下一个节点的地址
}SLTNode;

// 动态申请一个节点
SLTNode* BuyListNode(SLTDateType x);

// 单链表打印
void SListPrint(SLTNode* phead);

// 单链表尾插
void SListPushBack(SLTNode** pphead, SLTDateType x);

// 单链表的头插
void SListPushFront(SLTNode** pphead, SLTDateType x);

// 单链表的尾删
void SListPopBack(SLTNode** pphead);

// 单链表头删
void SListPopFront(SLTNode** pphead);

// 单链表查找
SLTNode* SListFind(SLTNode* phead, SLTDateType x);

//pos位置之前插入
void SListInsert(SLTNode**pphead, SLTNode* pos, SLTDateType x);

// 在pos的后面插入,这个更适合,也更简单
void SListInsertAfter(SLTNode* pos, SLTDateType x);

//单链表在pos位置上删除
void SListErase(SLTNode *pphead, SLTNode* pos);

//单链表在pos后面删除
void SListEraseAfter(SLTNode* pos)

//单链表的释放
void SListDestory(SLTNode** pphead);



传二级指针是为了改变头指针的指向 

2.1动态申请一个节点

SLTNode* BuyListNode(SLTDateType x)
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	if (newnode == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}

2.2单链表打印

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

2.3单链表尾插

void SListPushBack(SLTNode** pphead, SLTDateType x)
{
	assert(pphead);
	SLTNode* newnode = BuyListNode(x);
	//判断是否为空结点
	if (*pphead == NULL)
	{
		*pphead = newnode;//==*plist=newnode
	}
	else
	{
		//找到尾结点
		SLTNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}

2.4单链表的头插

void SListPushFront(SLTNode** pphead, SLTDateType x)
{
	assert(pphead);
	SLTNode* newnode = BuyListNode(x);
	//不用判空
	newnode->next = *pphead;
	*pphead = newnode;

}

2.5单链表的尾删

void SListPopBack(SLTNode** pphead)
{
	assert(pphead);
	//判断是否空链表--非法访问
	//a.温柔地方式
	if (*pphead == NULL)
	{
		return;
	}
	//b.暴力地方式
	///assert(*pphead != NULL);

	//1.一个节点
	//2.两个以上节点
	if ((*pphead)->next == NULL)
	{
		free(*pphead);//释放pphead指向的
		*pphead = NULL;
	}
	else
	{
	//方法一:
	SLTNode* tail = *pphead;
	SLTNode* prev = NULL;
	while (tail->next/*tail->next != NULL*/)
	{
		prev = tail;
		tail = tail->next;
	}
	free(tail);
	tail = NULL;
	prev->next = NULL;//一个节点非法访问

	//方法二:
	//SLTNode* tail = *pphead;
	//while (tail->next->next)//一个节点非法访问
	//{
	//	tail = tail->next;
	//}
	//free(tail->next);
	//tail->next = NULL;

	}
}

2.6单链表头删

void SListPopFront(SLTNode** pphead)
{
	assert(pphead);
	//判断是否空链表--非法访问
	//a.温柔地方式
	if (*pphead == NULL)
	{
		return;
	}
	//b.暴力地方式
	///assert(*pphead != NULL);

	SLTNode* next = (*pphead)->next;
	free(*pphead);
	*pphead = next;

}

 2.7单链表查找

SLTNode* SListFind(SLTNode* phead, SLTDateType x)
{
	SLTNode* cur = phead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		else
		{
			cur = cur->next;
		}
	}
	return NULL;
}

2.8pos位置之前插入

void SListInsert(SLTNode** pphead, SLTNode* pos, SLTDateType x)
{
	assert(pphead);
	assert(pos);
	SLTNode* newnode = BuyListNode(x);
	if (*pphead == pos)
	{
		newnode->next = *pphead;
		*pphead = newnode;
	}
	else
	{
		//找到pos的前一个位置
		SLTNode* posPrev = *pphead;//pos为第一个节点时,posPrev找不到
		while (posPrev->next != pos)
		{
            posPrev=posPrev->next;
		}
			posPrev->next = newnode;
			newnode->next = pos;
	}
}

2.9在pos的后面插入,这个更适合,也更简单

void SListInsertAfter(SLTNode* pos, SLTDateType x)
{
	assert(pos);
	SLTNode* newnode = BuyListNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}

2.10单链表在pos位置上删除

void SListErase(SLTNode** pphead, SLTNode* pos)
{
	assert(pphead);
	assert(pos);
	if (*pphead == pos)
	{
		*pphead = pos->next;
		free(pos);
		pos = NULL;
		//SListPopFront(pphead);
	}
	else
	{
		SLTNode* prev = *pphead;
		while(prev->next != pos)
		{
			prev = prev->next;
		}
		prev->next = pos->next;
		free(pos);
		pos = NULL;//置空与不置空都可以
		//pos不置空也无所谓,毕竟在函数中是形参,形参的改变不会影响实参,处于好习惯,还是置空比较好
	}
}

2.11 单链表在pos后面删除

void SListEraseAfter(SLTNode* pos)
{
	assert(pos);
	assert(pos->next);
	SLTNode* next = pos->next;
	pos->next = next->next;
	free(next);
	next = NULL;
}

2.12单链表的释放

void SListDestory(SLTNode** pphead)
{
	assert(pphead);

	SLTNode* cur = *pphead;
	while (cur)
	{
		SLTNode* next = cur->next;
		free(cur);
		cur = next;
	}

	*pphead = NULL;
}

3.代码

SList.h

#pragma once

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
//写入访问权限冲突 解决方法:#include <stdlib.h>

//无头+单向+非循环链表增删查改实现
typedef int SLTDateType;
typedef struct SListNode
{
	SLTDateType data;
	struct SListNode* next;
}SLTNode;

// 动态申请一个节点
SLTNode* BuyListNode(SLTDateType x);

// 单链表打印
void SListPrint(SLTNode* phead);

// 单链表尾插
void SListPushBack(SLTNode** pphead, SLTDateType x);

// 单链表的头插
void SListPushFront(SLTNode** pphead, SLTDateType x);

// 单链表的尾删
void SListPopBack(SLTNode** pphead);

// 单链表头删
void SListPopFront(SLTNode** pphead);

// 单链表查找
SLTNode* SListFind(SLTNode* phead, SLTDateType x);

//pos位置之前插入
void SListInsert(SLTNode**pphead, SLTNode* pos, SLTDateType x);

// 在pos的后面插入,这个更适合,也更简单
void SListInsertAfter(SLTNode* pos, SLTDateType x);

//单链表在pos位置上删除
void SListErase(SLTNode *ppphead, SLTNode* pos);

//单链表在pos后面删除
void SListEraseAfter(SLTNode* pos)

//单链表的释放
void SListDestory(SLTNode** pphead);



 SList.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"

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

SLTNode* BuyListNode(SLTDateType x)
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	if (newnode == NULL)
	{
		printf("malloc fail]\n");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}

void SListPushBack(SLTNode** pphead, SLTDateType x)
{
	assert(pphead);
	SLTNode* newnode = BuyListNode(x);
	//判断是否为空结点
	if (*pphead == NULL)
	{
		*pphead = newnode;//==*plist=newnode
	}
	else
	{
		//找到尾结点
		SLTNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}

void SListPopBack(SLTNode** pphead)
{
	assert(pphead);
	//判断是否空链表--非法访问
	//a.温柔地方式
	if (*pphead == NULL)
	{
		return;
	}
	//b.暴力地方式
	///assert(*pphead != NULL);

	//1.一个节点
	//2.两个以上节点
	if ((*pphead)->next == NULL)
	{
		free(*pphead);//释放pphead指向的
		*pphead = NULL;
	}
	else
	{
	//方法一:
	SLTNode* tail = *pphead;
	SLTNode* prev = NULL;
	while (tail->next/*tail->next != NULL*/)
	{
		prev = tail;
		tail = tail->next;
	}
	free(tail);
	tail = NULL;
	prev->next = NULL;//一个节点非法访问

	//方法二:
	//SLTNode* tail = *pphead;
	//while (tail->next->next)//一个节点非法访问
	//{
	//	tail = tail->next;
	//}
	//free(tail->next);
	//tail->next = NULL;

	}
}

void SListPushFront(SLTNode** pphead, SLTDateType x)
{
	assert(pphead);
	SLTNode* newnode = BuyListNode(x);
	//不用判空
	newnode->next = *pphead;
	*pphead = newnode;

}

void SListPopFront(SLTNode** pphead)
{
	assert(pphead);
	//判断是否空链表--非法访问
	//a.温柔地方式
	if (*pphead == NULL)
	{
		return;
	}
	//b.暴力地方式
	///assert(*pphead != NULL);

	SLTNode* next = (*pphead)->next;
	free(*pphead);
	*pphead = next;

}

SLTNode* SListFind(SLTNode* phead, SLTDateType x)
{
	SLTNode* cur = phead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		else
		{
			cur = cur->next;
		}
	}
	return NULL;
}

void SListInsert(SLTNode** pphead, SLTNode* pos, SLTDateType x)
{
	assert(pphead);
	assert(pos);
	SLTNode* newnode = BuyListNode(x);
	if (*pphead == pos)
	{
		newnode->next = *pphead;
		*pphead = newnode;
	}
	else
	{
		//找到pos的前一个位置
		SLTNode* posPrev = *pphead;//pos为第一个节点时,posPrev找不到
		while (posPrev->next != pos)
		{
			posPrev->next = newnode;
			newnode->next = pos;
		}
	}
}
// 在pos的后面插入,这个更适合,也更简单
void SListInsertAfter(SLTNode* pos, SLTDateType x)
{
	assert(pos);
	assert(pos);
	SLTNode* newnode = BuyListNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}

void SListErase(SLTNode** pphead, SLTNode* pos)
{
	assert(pphead);
	assert(pos);
	if (*pphead == pos)
	{
		*pphead = pos->next;
		free(pos);
		pos = NULL;
		//SListPopFront(pphead);
	}
	else
	{
		SLTNode* prev = *pphead;
		while(prev->next != pos)
		{
			prev = prev->next;
		}
		prev->next = pos->next;
		free(pos);
		pos = NULL;//置空与不置空都可以
		//pos不置空也无所谓,毕竟在函数中是形参,形参的改变不会影响实参,处于好习惯,还是置空比较好
	}
}

void SListEraseAfter(SLTNode* pos)
{
	assert(pos);
	assert(pos->next);
	SLTNode* next = pos->next;
	pos->next = next->next;
	free(next);
	next = NULL;
}

void SListDestory(SLTNode** pphead)
{
	assert(pphead);

	SLTNode* cur = *pphead;
	while (cur)
	{
		SLTNode* next = cur->next;
		free(cur);
		cur = next;
	}

	*pphead = NULL;
}

Test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"SList.h"

void test()
{
	SLTNode* plist = NULL;
	//*plist指向的是NULL
	SListPushBack(&plist, 1);
	SListPushBack(&plist, 1);
	SListPushBack(&plist, 1);
	SListPushBack(&plist, 1);
	SListPrint(plist);

	SListPushFront(&plist, 2);
	SListPushFront(&plist, 2);
	SListPushFront(&plist, 2);
	SListPushFront(&plist, 2);
	SListPushFront(&plist, 2);
	SListPrint(plist);

	SListPopBack(&plist);
	SListPopBack(&plist);
	SListPrint(plist);

	SListPopFront(&plist);
	SListPopFront(&plist);
	SListPopFront(&plist);
	SListPrint(plist);

	SLTNode* pos = SListFind(plist, 2);
	int i = 1;
	while (pos)
	{
		printf("第%d个pos节点:%->%d\n", i++, pos, pos->data);
		pos = SListFind(pos->next, 2);
	}
	//修改值 2-20
	pos = SListFind(plist, 2);
	if (pos)
	{
		pos->data = 20;
	}
	SListPrint(plist);
	pos = SListFind(plist, 20);
	if (pos)
	{
		pos->data = 30;
	}
	SListPrint(plist);
	
	pos = SListFind(plist, 3);
	if (pos)
	{
		SListInsert(&plist, pos, 30);
	}
	SListPrint(plist);

	pos = SListFind(plist, 30);
	if (pos)
	{
		SListInsert(&plist, pos, 300);
	}
	SListPrint(plist);

	pos = SListFind(plist, 300);
	if (pos)
	{
		SListInsertAfter(pos, 3000);
	}
	SListPrint(plist);
	pos = SListFind(plist, 3000);
	if (pos)
	{
		SListErase(&plist, pos);
	}
	SListPrint(plist);

	pos = SListFind(plist, 300);
	if (pos)
	{
		SListEraseAfter(pos);
	}
	SListPrint(plist);
	SListDestory(&plist);

}	
int main()

{
	test();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值