【数据结构之单链表】

本文详细介绍了链表的数据结构及其优势,特别是针对顺序表的不足之处。文章通过C语言实现了一个单链表,包括头插、尾插、头删、尾删、查找、插入和删除等基本操作,并提供了测试用例验证了这些操作的正确性。此外,还解释了在执行删除操作时为何需要使用两个指针来避免野指针的问题。
摘要由CSDN通过智能技术生成

前言

上一篇我们已经写完顺序表了,那么顺序表有什么缺点呢,如何解决?
在这里插入图片描述

链表的概念及结构

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

在这里插入图片描述

单链表实现

SList.h

#define _CRT_SECURE_NO_WARNINGS

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

typedef int SLTDataType;
struct SListNode//定义结点
{
	SLTDataType data;//数据
	struct SListNode* next;//指针
};
typedef struct SListNode SLTNode;

void SListPrint(SLTNode* phead);
void SListPushBack(SLTNode** pphead, SLTDataType x);
void SListPushFront(SLTNode** pphead, SLTDataType x);
void SListPopFront(SLTNode** pphead);
void SListPopBack(SLTNode** pphead);
SLTNode* SListFind(SLTNode* phead, SLTDataType x);
void SListInsert(SLTNode** pphead,SListNode*pos,SLTDataType x);
void SListErase(SLTNode** pphead, SListNode* pos);

SList.cpp

#define _CRT_SECURE_NO_WARNINGS

#include"SList.h"

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

SLTNode* BuySListNode(SLTDataType x)//扩容
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	newnode->data = x;
	newnode->next = NULL;

	return newnode;
}
void SListPushBack(SLTNode** pphead, SLTDataType x)//尾插
{
	SLTNode* newnode = BuySListNode(x);
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		//找尾节点的指针
		SLTNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		//尾节点,链接新节点
		tail->next = newnode;
	}
}

在这里插入图片描述

void SListPushFront(SLTNode** pphead, SLTDataType x)//头插
{
	SLTNode* newnode = BuySListNode(x);

	newnode->next = *pphead;
	*pphead = newnode;
}

在这里插入图片描述

void SListPopBack(SLTNode** pphead)//尾删
{
	if (*pphead == NULL)
	{
		return;
	}
	else if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		SLTNode* prev = NULL;
		SLTNode* tail = *pphead;
		while (tail->next != NULL)
		{
			prev = tail;
			tail = tail->next;
		}
		free(tail);
		prev->next = NULL;
	}
}

这里需要用到两个指针,原因如图,一个指针会导致野指针的出现
在这里插入图片描述
一个指针记录删去节点的指针,一个记录删去节点前的节点的指针
在这里插入图片描述

void SListPopFront(SLTNode** pphead)//头删
{
	SLTNode* next = (*pphead)->next;
	free(*pphead);
	*pphead = next;
}

在这里插入图片描述

SLTNode* SListFind(SLTNode*phead, SLTDataType x)//查找
{
	SListNode* cur = phead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
void SListInsert(SLTNode** pphead, SListNode* pos, SLTDataType x)//插入
{
	if (pos == *pphead)
	{
		SListPushFront(pphead, x);
	}
	else
	{
		SLTNode* newnode = BuySListNode(x);
		SLTNode* prev = *pphead;
		while (prev->next != pos)
		{
			prev = prev->next;
		}
			prev->next=newnode;
			newnode->next = pos;
	}
}

在pos前插入x
在这里插入图片描述

void SListErase(SLTNode** pphead, SListNode* pos)//删除
{
	if (pos == *pphead) 
	{
		SListPopFront(pphead);
	}
	else
	{
		SLTNode* prev = *pphead;
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		prev->next = pos->next;
		free(pos);
	}
}

删除pos位
在这里插入图片描述

测试代码

#define _CRT_SECURE_NO_WARNINGS

#include"SList.h"

void TestSList1()
{
	SLTNode* plist = NULL;
	SListPushBack(&plist, 1);
	SListPushBack(&plist, 2);
	SListPushBack(&plist, 3);
	SListPushBack(&plist, 4);
	SListPushFront(&plist, 0);
	SListPrint(plist);
}

在这里插入图片描述

尾插头插成功

void TestSList2()
{
	SLTNode* plist = NULL;
	SListPushBack(&plist, 1);
	SListPushBack(&plist, 2);
	SListPushBack(&plist, 3);
	SListPushBack(&plist, 4);
	SListPrint(plist);

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

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

在这里插入图片描述

头删尾删成功

void TestSList3()
{
	SLTNode* plist = NULL;
	SListPushBack(&plist, 1);
	SListPushBack(&plist, 2);
	SListPushBack(&plist, 3);
	SListPushBack(&plist, 4);
	SListPrint(plist);

	//在3前面插入一个25
	SLTNode* pos = SListFind(plist, 1);
	if (pos)
	{
		SListInsert(&plist,  pos, 10);
	}
	SListPrint(plist);

	pos = SListFind(plist, 3);
	if (pos)
	{
		SListInsert(&plist, pos, 25);
	}
	SListPrint(plist);
}

在这里插入图片描述

插入,查找成功

void TestSList4()
{
	SLTNode* plist = NULL;
	SListPushBack(&plist, 1);
	SListPushBack(&plist, 2);
	SListPushBack(&plist, 3);
	SListPushBack(&plist, 4);
	SListPrint(plist);

	SLTNode* pos = SListFind(plist, 1);
	if (pos)
	{
		SListErase(&plist, pos);
	}
	SListPrint(plist);

	pos = SListFind(plist, 3);
	if (pos)
	{
		SListErase(&plist, pos);
	}
	SListPrint(plist);

	pos = SListFind(plist, 2);
	if (pos)
	{
		SListErase(&plist, pos);
	}
	SListPrint(plist);

	pos = SListFind(plist, 4);
	if (pos)
	{
		SListErase(&plist, pos);
	}
	SListPrint(plist);

}

在这里插入图片描述

删除成功
该单链表成功

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值