数据结构与算法----单链表

数据结构与算法–单链表


完整代码见文末

创建节点

struct SListNode
{
	SListDataType data;
	struct SListNode* next;
};

为了方便表示,再给结构体定义新的类型名

typedef struct SListNode
{
	SListDataType data;
	struct SListNode* next;
}SListNode;

上一个结点存着下一个结点的地址

在这里插入图片描述

为了方便理解,下面先从链表的打印进行理解

链表的打印

基本思路:

  1. 定义一个新的指针指向头结点

    SListNode* cur = phead;
    

    在这里插入图片描述

  2. 打印当前cur指向结点的数值

    printf("%d->", cur->data);
    
  3. 此时cur需要打印下个结点的数据,那么就需要得到下个结点的地址

    cur = cur->next;
    

    在这里插入图片描述

链表打印函数完整代码如下

void SListPrint(SListNode* phead)
{
	SListNode* cur = phead;			//再定义一个指针指向头结点
	while (cur != NULL)
	{
		printf("%d->", cur->data);	//cur指向结构体中的数字域
		cur = cur->next;			//cur指向下一个结点
	}
    printf("NULL");
}

链表的插入

尾插:在最后一个结点的后面插入一个新的结点

在这里插入图片描述

基本思路:

  1. 找到链表中最后一个结点的地址

​ 当tail 指向的下一个结点的地址不为NULL时,则当前结点不是尾结点,当tail 指向的下一个结点的地址为NULL时,找到尾结点 ,此时的tail存有尾结点的地址

在这里插入图片描述

​ 该部分代码

    SListNode* tail = phead;
	while (tail->next != NULL)
	{
		tail = tail->next;
	}
  1. 插入新的数据

    1. 申请一个新的结点

      	SListNode* newNode = (SListNode*)malloc(sizeof(SListNode));
      	if (newNode == NULL)
      	{
      		printf("申请结点失败\n");
      		exit(-1);
      	}
      
      	newNode->data = x;
      	newNode->next = NULL;
      
    2. 插入

      tail->next = newNode;
      

      在这里插入图片描述

尾插法完整代码如下

调用该函数时,由于值传递无法改变实参的值, 因此要进行址传递,因此传入的是指针的地址,要用二级指针**p进行接收,对二级指针解引用就可得到原指针。
在这里插入图片描述

void SListPushBack(SListNode* *pphead, SListDataType x)
{
	SListNode* tail = *pphead;

	if (*pphead == NULL)    //头结点为空的情况
	{
		SListNode* newNode = (SListNode*)malloc(sizeof(SListNode));
		if (newNode == NULL)
		{
			printf("申请结点失败\n");
			exit(-1);
		}
		newNode->data = x;
		newNode->next = NULL;

		*pphead = newNode;
	}
	else				//头结点不为空的情况
	{
		while (tail->next != NULL)
		{
			tail = tail->next;
		}

		SListNode* newNode = (SListNode*)malloc(sizeof(SListNode));
		if (newNode == NULL)
		{
			printf("申请结点失败\n");
			exit(-1);
		}
		newNode->data = x;
		newNode->next = NULL;

		tail->next = newNode;
	}
}

实际上,if和else中申请结点的代码段是相同的,由于后面还会用到相同的代码段,因此可将这段代码做成函数

//新结点申请函数
SListNode* BuySListNode(SListDataType x)
{
	SListNode* newNode = (SListNode*)malloc(sizeof(SListNode));
	if (newNode == NULL)
	{
		printf("申请结点失败\n");
		exit(-1);
	}
	newNode->data = x;
	newNode->next = NULL;  //新结点指向NULL

	return newNode;
}

则尾插法的代码简化为

void SListPushBack(SListNode* *pphead, SListDataType x)//传入指针的地址
{
	SListNode* newNode = BuySListNode(x);  //申请结点

	if (*pphead == NULL)
	{
		*pphead = newNode;     //头结点即为新结点
	}
	else
	{
		//找尾
		SListNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}

		tail->next = newNode;
	}
}
尾删

分三种情况

  1. 链表为空
  2. 只有一个节点
  3. 有一个以上结点

对于第三种情况,当把结点free掉后,该结点的前一个节点未能指向NULL,因此该节点还能被访问,造成错误,要解决这种情况,不仅要找到尾结点,还要找到尾结点的前一个结点,那就要再定义多一个指针,在对尾结点进行删除之前,用于保存tail的的前一个结点

在这里插入图片描述

尾删完整代码如下
void SListPopBack(SListNode** pphead)
{
	if (*pphead == NULL)	//链表为空
	{
		return;
	}
	else if((*pphead)->next==NULL)	//只有一个结点
	{
		free(*pphead);		//删除结点
		*pphead = NULL;		//头指针置空
	}
	else	//有一个以上结点
	{
		SListNode* prev = NULL;
		SListNode* tail = *pphead; //找尾
		while (tail->next != NULL)
		{
			prev = tail;       //用于保存tail指向的上一个结点
			tail = tail->next;
		}

		free(tail);
		prev->next = NULL;
	}
}
头插

基本思路:

  1. 开辟一个新节点并指向原来头节点的地址

  2. 再让头指针指向新的头结点

    在这里插入图片描述

头删

分类

  1. 链表空

  2. 只有一个结点

  3. 一个以上结点

    当第一个结点的内存释放后,第二个结点的地址也就找不到了,此时头指针就无法指向第二个结点,因此要定义一个新的指针来事先保存第二个结点的地址

    在这里插入图片描述

头删完整代码如下
void SListPopFront(SListNode** pphead)
{
	//链表空
	if (*pphead == NULL)
	{
		return;
	}
	//只有一个结点和一个以上结点方法相同
	else
	{
		SListNode* next = (*pphead)->next;
		free(*pphead);
		*pphead=next;
	}
}

查找&修改

SListNode* SListFind(SListNode* phead, SListDataType x)
{
	SListNode* cur = phead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}

		cur = cur->next;
	}

	return NULL;
}

查找到后,可对该值进行更改

SListNode* pList = NULL;

	SListPushBack(&pList, 1);
	SListPushBack(&pList, 2);
	SListPushBack(&pList, 3);
	SListPushBack(&pList, 4);
	SListPrint(pList);

SListNode* pos = SListFind(pList, 3);
	if (pos)
	{
		pos->data = 30;
	}
	SListPrint(pList);

在指定位置结点后面插入

例如在第二个结点后插入(pos=2)
基本思路

  1. 找到选定位置的结点

  2. 创建新结点

  3. 新结点先保存选定位置节点的下个结点的地址,选定位置结点再保存新结点的地址,顺序不能调换,否则新结点无法保存后面一个结点点的地址

    正确代码:

    newNode->next=pos->next;
    pos->next=newNode;
    

    在这里插入图片描述

完整代码:
void SListInsertAfter(SListNode* pos, SListDataType x)
{
	assert(pos); //断言

	SListNode* newNode = BuySListNode(x);
	newNode->next = pos->next; //新结点指向后一个结点
	pos->next = newNode;		//前一个结点指向新结点
}

删除指定位置结点后面的一个结点

例如删除第二个结点(pos=1):
基本思路

  1. 找到选定位置的结点

  2. 当前结点保存将要删除的结点后一个结点的地址

  3. 删除结点

    在这里插入图片描述

完整代码:
void SListEraseAfter(SListNode* pos)
{
	assert(pos);
	if (pos->next)  //要删除结点不为空
	{
		SListNode* next = pos->next; //pos的下一个结点
		SListNode* nextnext = next->next; //pos的下一个的下一个结点
		pos->next = nextnext;  //上面的定义只是为了方便理解,其实也可以直接写pos->next = pos->next->next
		free(next);
	}
}

单链表完整代码

SList.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

typedef int SListDataType;

//结点
typedef struct SListNode
{
	SListDataType data;
	struct SListNode* next;
}SListNode;

void SListPushBack(SListNode** pphead, SListDataType x);
void SListPopBack(SListNode** pphead);
void SListPushFront(SListNode** pphead, SListDataType x);
void SListPopFront(SListNode** pphead);
void SListInsertAfter(SListNode* pos, SListDataType x);
SListNode* SListFind(SListNode* phead, SListDataType x);
void SListPrint(SListNode* phead);
SListNode* BuySListNode(SListDataType x);

SList.c

#include "SList.h"

//申请结点
SListNode* BuySListNode(SListDataType x)
{
	SListNode* newNode = (SListNode*)malloc(sizeof(SListNode));
	if (newNode == NULL)
	{
		printf("申请结点失败\n");
		exit(-1);
	}
	newNode->data = x;
	newNode->next = NULL;  //新结点指向NULL

	return newNode;
}

//尾插
void SListPushBack(SListNode* *pphead, SListDataType x)//传入指针的地址
{
	SListNode* newNode = BuySListNode(x);  //申请结点

	if (*pphead == NULL)
	{
		*pphead = newNode;     //头结点即为新结点
	}
	else
	{
		//找尾
		SListNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}

		tail->next = newNode;
	}
}

//尾删
void SListPopBack(SListNode** pphead)
{
	if (*pphead == NULL)	//链表为空
	{
		return;
	}
	else if((*pphead)->next==NULL)	//只有一个结点
	{
		free(*pphead);		//删除结点
		*pphead = NULL;		//头指针置空
	}
	else	//有一个以上结点
	{
		SListNode* prev = NULL;
		SListNode* tail = *pphead; //找尾
		while (tail->next != NULL)
		{
			prev = tail;       //用于保存tail指向的上一个结点
			tail = tail->next;
		}

		free(tail);
		prev->next = NULL;
	}
}

//头插
void SListPushFront(SListNode** pphead,SListDataType x)
{
	SListNode* newNode = BuySListNode(x);
	newNode->next = *pphead;
	*pphead = newNode;	
}

//头删
void SListPopFront(SListNode** pphead)
{
	//链表空
	if (*pphead == NULL)
	{
		return;
	}
	//只有一个结点和一个以上结点方法相同
	else
	{
		SListNode* next = (*pphead)->next;
		free(*pphead);
		*pphead=next;
	}
}

//查找
SListNode* SListFind(SListNode* phead, SListDataType x)
{
	SListNode* cur = phead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}

		cur = cur->next;
	}

	return NULL;
}

//在指定位置结点后面插入
void SListInsertAfter(SListNode* pos, SListDataType x)
{
	assert(pos);

	SListNode* newNode = BuySListNode(x);
	newNode->next = pos->next;
	pos->next = newNode;
}

//删除指定位置结点后面的一个结点
void SListEraseAfter(SListNode* pos)
{
	assert(pos);
	if (pos->next)  //要删除结点不为空
	{
		SListNode* next = pos->next; //pos的下一个结点
		SListNode* nextnext = next->next; //pos的下一个的下一个结点
		pos->next = nextnext;  //上面的定义只是为了方便理解,其实也可以直接写pos->next = pos->next->next
		free(next);
	}
}

//打印
void SListPrint(SListNode* phead)
{
	SListNode* cur = phead;			//再定义一个指针指向头结点
	while (cur != NULL)
	{
		printf("%d->", cur->data);	//cur指向结构体中的数字域
		cur = cur->next;			//cur指向下一个结点
	}
	printf("NULL");
	printf("\n");
}

text.c

#include "SList.h"
void TestSList1()
{
	SListNode* 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);
	SListPrint(pList);

	SListPushFront(&pList, 1);
	SListPushFront(&pList, 2);
	SListPushFront(&pList, 3);
	SListPushFront(&pList, 4);
	SListPushFront(&pList, 5);
	SListPrint(pList);

	SListPopFront(&pList);
	SListPopFront(&pList);
	SListPopFront(&pList);
	SListPopFront(&pList);
	SListPopFront(&pList);
	SListPopFront(&pList);
	SListPrint(pList);

}

void TestSList2()
{
	SListNode* pList = NULL;

	SListPushBack(&pList, 1);
	SListPushBack(&pList, 2);
	SListPushBack(&pList, 3);
	SListPushBack(&pList, 4);
	SListPrint(pList);

	SListNode* pos = SListFind(pList, 3);
	if (pos)
	{
		pos->data = 30;
	}
	SListPrint(pList);
}

int main()
{
	TestSList1();
	TestSList2();

	return 0;
}
  • 30
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 29
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 29
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大拨鼠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值