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;
}
这是最终这个代码的实现效果哦