单链表的介绍(如果大家有疑问或者觉得博主代码有问题欢迎大家留言)

1.单链表的介绍

链表是一种物理存储结构上连续,但是缺不连续的存储结构,数据元素的逻辑结构是通过链表中的指针链接次序实现的
在这里插入图片描述
就类似于每个车厢间的链接轨道
在这里插入图片描述

``2.单链表的实现(带哨兵位(即为带上了单链表的实现(带哨兵位(即为带上了头节点))

//先创建一个头文件
//SList.h
#pragma once
#include <stdlib.h>//malloc函数需要使用
#include <stdio.h>//基本库函数
#include <assert.h>//断言的函数
typedef int SListDataType;
typedef struct SListNode
{
	SListDataType val;
	struct SListNode* next;
}SListNode;//单链表的基本结构
//现在实现基本的接口的
SListNode* SListInit();//初始化,创建一个头结点
SListNode* CreateNode(SListDataType x);//在后面会创建多个节点,这里就用函数封装
void SListNodePrint(SListNode* phead);//打印函数
void SListNodePushBack(SListNode* phead, SListDataType x);//尾插函数
void SListDestroy(SListNode* phead);//销毁空间函数
void SListNodePushFront(SListNode* phead, SListDataType x);//头插函数
void SListNodePopBack(SListNode* phead);//尾删函数
void SListNodePopFront(SListNode* phead);//头删函数
SListNode* SListFind(SListNode* phead, SListDataType x);//寻找,这里用一个具有返回值的函数接受
void SListInsertAfter(SListNode* pos, SListDataType x);//插入节点后的数
void SListEraseAfter(SListNode* pos);//删除节点后的函数


下面是具体的实现逻辑(在代码中有注释出打击可以多看看,是博主自己在手搓过程中感觉需要注意的)

#include "SList.h"
SListNode* SListInit()//初始化
{
	SListNode* head = (SListNode*)malloc(sizeof(SListNode));
	head->next = NULL;
	return head;
}
SListNode* CreateNode(SListDataType x)
{
	//产生一个头节点,这样就可以不使用二级指针
	SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return NULL;
	}
	newnode->val = x;
	newnode->next = NULL;
	return newnode;
}
void SListNodePrint(SListNode* phead)
{
	assert(phead);
	SListNode* cur = phead->next;
	while (cur)
	{
		printf("%d ", cur->val);
		cur = cur->next;
	}
	printf("NULL\n");
}    
void SListNodePushBack(SListNode* phead,SListDataType x)
{
	SListNode* newnode = CreateNode(x);
	
		SListNode* tail = phead;
		while (tail->next)
		{
			tail = tail->next;

		}
		tail->next = newnode;
}
void SListNodePushFront(SListNode* phead, SListDataType x)
{
	SListNode* newnode = CreateNode(x);
	newnode->next = phead->next;
	phead->next = newnode;
}
void SListNodePopBack(SListNode* phead)
{
	assert(phead);
	SListNode* tail = phead;
	while (tail->next->next)//这里要注意要,大家可以再智商多画图
	{
		tail = tail->next;
	}
	free(tail->next);
	tail->next= NULL;

}
void SListNodePopFront(SListNode* phead)
{
	assert(phead);
	SListNode* del = phead->next;//这里要记录一下,不然删除后,就无法找到下一个节点
	phead->next = phead->next->next;
	free(del);
	del == NULL;

}
SListNode* SListFind(SListNode* phead,SListDataType x)
{
	assert(phead);
	SListNode* cur = phead->next;
	while (cur)
	{
		if (cur->val == x)
		{
			return cur;//如果找到了就返回该节点
		}
		cur = cur->next;
	}
	return NULL;

}

void SListInsertAfter(SListNode* pos, SListDataType x)
{
	assert(pos);
	SListNode* newnode = CreateNode(x);
	newnode->next = pos->next;//注意这里需要先从后面链接,不然pos的指向就不对了
	pos->next = newnode;

	

}
void SListEraseAfter(SListNode* pos)
{
	assert(pos);
	SListNode* del = pos->next;
	pos->next = pos->next->next;
	free(del);
	del = NULL;
}
void SListDestroy(SListNode* phead)
{
	SListNode* cur = phead->next;
	while (cur)
	{
		SListNode* del = cur->next;
		free(cur);
		cur = del;
		
	}
	free(phead);
	phead = NULL;
}

主函数

#include "SList.h"
int main()
{
	SListNode* phead = SListInit();
	SListNodePushBack(phead,1);
	SListNodePushBack(phead, 2);
	SListNodePushBack(phead, 3);
	SListNodePushBack(phead, 4);
	SListNodePushBack(phead, 5);
	SListNodePushBack(phead, 6);
	SListNodePushBack(phead, 8);
	SListNodePushFront(phead,1);
	SListNodePopBack(phead);
	SListNodePopFront(phead);
	SListNode* pos1=SListFind(phead,4);
	SListInsertAfter(pos1, 10);
	SListNode* pos2 = SListFind(phead, 2);
	SListEraseAfter(pos2);
	SListNodePrint(phead);

	//SListNodePrint(phead);

	return 0;
}

在这里插入图片描述
这是最终这个代码的实现效果哦

3.纸上得来终觉浅,大家要好好敲代码哦

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值