栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

单链表(C语言实现)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

单链表(C语言实现)

链表的概念及结构

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

注意:

    从上图可以看出,链式结构在逻辑上是连续的,但在物理上不一定连续。现实中的节点一般都是从堆上申请出来的从堆上申请的空间,是按照一定的策略来分配的,两次申请的空间可能连续也可能不连续。
链表的实现

无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等。

SList.h代码实现

#pragma once
#include 
#include 
#include 

typedef int SLTDataType;
typedef struct SListNode
{
	int data;//val
	struct SListNode* next;//存储下一个节点的地址
}SListNode;

SListNode* BuySListNode(SLTDataType x);//动态申请一个节点
void SListPrint(SListNode* plist);//单链表打印
void SListPushBack(SListNode** plist, SLTDataType x);//尾插
void SListPushFront(SListNode** plist, SLTDataType x);//头插
void SListPopBack(SListNode** plist);//尾删
void SListPopFront(SListNode** plist);//头删
SListNode* SListFind(SListNode* plist, SLTDataType x);//查找
void SListInsertAfter(SListNode* pos, SLTDataType x);//在pos位置之后插入x
void SListInsertBefore(SListNode** plist, SListNode* pos, SLTDataType x);//在pos位置之前插入x
void SListErase(SListNode** plist, SListNode* pos);//删除pos位置的值
void SListEraseAfter(SListNode* pos);//删除pos位置之后的值
void SListDestory(SListNode* plist);//单链表的销毁


SList.c代码实现

#include "SList.h"
SListNode* BuySListNode(SLTDataType x)//动态申请一个节点
{
	SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));

	if (newnode == NULL)
	{
		printf("mallloc failn");
		exit(-1);
	}
	else
	{
		newnode->data = x;
		newnode->next = NULL;
	}
	return newnode;
}
void SListPrint(SListNode* plist)//单链表打印
{
	SListNode* cur = plist;
	while (cur != NULL)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULLn");
}
void SListDestory(SListNode* plist)//销毁
{
	assert(plist);
	SListNode* tmpp = plist;
	SListNode* tmp = plist->next;
	for (SListNode* cur = tmp ; tmp;cur = tmp)
	{
		tmp = cur->next;
		free(cur);
	}
	plist = NULL;
	free(tmpp);
}

尾插的实现

 代码实现

void SListPushBack(SListNode** plist, SLTDataType x)//尾插
{
	SListNode* newnode = BuySListNode(x);
	if (*plist == NULL)
	{
		*plist = newnode;
	}
	else
	{
		SListNode* tail = *plist;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}

头插的实现

代码实现

void SListPushFront(SListNode** plist, SLTDataType x)//头插
{
	SListNode* newnode = BuySListNode(x);
	newnode->next = *plist;
	*plist = newnode;
}

 尾删的实现

代码实现:分三种情况

void SListPopBack(SListNode** plist)//尾删 //3种情况  空  1个节点  多个节点
{
	assert(plist);
	if (*plist ==NULL)
	{
		return;
	}
	
	SListNode* tail = *plist;
	SListNode* tmp = NULL;
	if ((*plist)->next == NULL)
	{
		tmp = (*plist);
		(*plist)= NULL;
		free(tmp);
	}
	else
	{
		while (tail->next->next != NULL)
		{
			tail = tail->next;
		}
		tmp = tail->next;
		tail->next = NULL;
		free(tmp);
	}
}

 头删的实现

 代码实现

void SListPopFront(SListNode** plist)//头删
{
	assert(plist);
	if (*plist == NULL)
	{
		return;
	}
	else
	{
		SListNode* head = *plist;
		*plist = (*plist)->next;
		free(head);
	}
}

查找的实现

SListNode* SListFind(SListNode* plist, SLTDataType x)//查找
{
	assert(plist);
	SListNode* tmp = plist;
	for (SListNode* tmp = plist; tmp; tmp = tmp->next)
	{
		if (tmp->data == x)
		{
			return tmp;
		}
	}
	return NULL;
}

在pos位置之后(之前)插入x

pos之后插入图

 代码实现

void SListInsertAfter(SListNode* pos, SLTDataType x)//在pos位置之后插入x
{
	assert(pos);
	SListNode* newnode = BuySListNode(x);
	newnode->next = pos->next;
	pos->next = newnode;	
}

pos之前插入图

 代码实现

void SListInsertBefore(SListNode** plist,SListNode* pos, SLTDataType x)//在pos位置之前插入x
{
	assert(plist);
	assert(pos);
	if (*plist == pos)
	{
		SListPushFront(plist,x);
	}
	else
	{
		SListNode* cur = *plist;
		while (cur->next != pos)
		{
			cur = cur->next;
		}
		SListNode* newnode = BuySListNode(x);
		newnode->next = pos;
		cur->next = newnode;
	}
}

删除pos位置(之后)的值

删除pos位置的图

 代码实现

void SListErase(SListNode** plist,SListNode* pos)//删除pos位置的值
{
	assert(plist);
	assert(pos);
	if (*plist == pos)
	{
		SListPopFront(plist);
	}
	else
	{
		SListNode* cur = *plist;
		while (cur->next != pos)
		{
			cur = cur->next;
		}
		cur->next = pos->next;
		free(pos);
		pos == NULL;
	}
}

删除pos位置之后的图

代码实现

void SListEraseAfter(SListNode* pos)//删除pos位置之后的值
{
	assert(pos);
	SListNode* tmp = pos->next;

	if (tmp != NULL)
	{
		pos->next = tmp->next;
		free(tmp);
	}
	else
	{
		return;
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/767375.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号