数据结构------链表初阶

一.链表的定义

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

二.链表的优缺点

优点:
(1)插入和删除速度快,保留原有的物理顺序,在插入或者删除一个元素的时候,只需要改变指针指向即可。
(2)没有空间限制,存储元素无上限,只与内存空间大小有关。
(3)动态分配内存空间,不用事先开辟内存
缺点:
(1)占用额外的空间以存储指针,比较浪费空间,不连续存储,malloc函数开辟空间碎片比较多)
(2) 查找速度比较慢,因为在查找时,只能顺序查找,需要循环链表

三.链表结构

单链表:分为指针域和数据域
在这里插入图片描述

typedef int SLI;
typedef struct SListNode
{
	SLI data;
	struct SListNode* next;
}SL;

四.链表常用操作函数(单向不循环无头链表)

对于无头链表,必须要创造头节点

1.创造哨兵头节点

SL* CreateNode(SLI x)
{
	SL* newnode = (SL*)malloc(sizeof(SL));
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}

2.遍历链表并打印链表中的元素

void SListPrint(SL* phead)
{
	SL* cur = phead;
	while (cur != NULL)
	{
		printf("%d->", cur->data);
		cur = cur->next;//迭代
	}
	//若链表为空则返回空
	if (cur == NULL)
	{
		printf("NULL");
	}
}

3.尾插元素

void SListPushBack(SL** pphead, SLI x)
{
	//创建新节点
	SL* newnode = CreateNode(x);
	//若头节点为空
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}

	//通过头节点找尾节点
	else {
		SL* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}

		tail->next = newnode;
	}
}

4.尾删元素

void SListPopBack(SL** pphead)
{
	//只有1个节点
	if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else 
	{
	//有多个节点
		SL* tail = *pphead;
		while (tail->next->next)
		{
		//迭代找尾
			tail = tail->next;
		}
		//释放内存
		free(tail->next);
		tail->next = NULL;
	}
}

5.头插元素

void SListPushFront(SL** pphead, SLI x)
{
	//数据直接插入头节点前面,并更新头节点位置
	SL* newnode = CreateNode(x);
	newnode->next = *pphead;
	*pphead = newnode;
}

6.头删元素

void SListPopFront(SL** pphead)
{
	
	assert(*pphead);
	SL* next = (*pphead)->next;
	free(*pphead);//删除头节点
	*pphead = next;//更新头节点
}

7.查找元素(返回节点地址值)

SL* SListFind(SL* phead, SLI x)
{
	SL* find = phead;
	//查找返回地址值
	while (find!= NULL)
	{
		if (find->data == x)
		{
			return find;
		}
		else
		{
			find = find->next;
		}
	} 
	return NULL;
}

8.插入到指定位置指定位置(头插进pos的前面)

void SListInsert(SL** pphead, SL* pos, SLI x)
{
	SL* newnode = CreateNode(x);
	//采取插入位置的前面
	//头插
	if (pos==*pphead)
	{
		newnode->next = *pphead;
		*pphead = newnode;
	}
	else 
	{
		//中间插
		SL* cur = *pphead;
		while (cur->next != pos)
		{
			cur = cur->next;
		}
		cur->next = newnode;
		newnode->next = pos;
	}
}

9.删除指定位置的节点

void SListErase(SL** pphead, SLI x)
{
	SL* pos = SListFind(*pphead, x);
	//删除头节点要更新头节点位置
	if (pos == *pphead)
	{
		//SL* newpphead = *pphead;
		//free(*pphead);//此时*pphead 的内存为空了
		//*pphead = newpphead->next;

		assert(*pphead);
		SL* next = (*pphead)->next;
		free(*pphead);
		*pphead = next;
	}
	else {
	//删除中间数据要让中间数据两边节点连接
		SL* cur = *pphead;
		SL* precur = NULL;
		while (cur != pos)
		{
			precur = cur;
			cur = cur->next;
		}
		precur->next = cur->next;
		free(cur);
		cur = NULL;
	}
}

10.删除整个链表

void SListDestory(SL** pphead)
{
	//释放各个节点的内存
	while (*pphead)
	{
		assert(*pphead);
		SL* next = (*pphead)->next;
		free(*pphead);
		*pphead = next;
	}
}

为什么函数里接受的是二级指针?

因为没有头节点,我们每次调用函数都需要获得头节点的地址值,每次调用都需要更新头节点的地址值,如果传一级指针则更新不了头节点的地址,所以传二级指针来更新

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值