文章目录
单链表
1.概念及结构
非连续、非顺序
的物理存储结构
通过指针
链接
2.分类
可以分为八种,三种的方式的结合
2.1单向或双向
2.2带头或不带头
2.3循环或不循环
虽然有这么多的链表的结构,但是我们实际中最常⽤还是两种结构:单链表和双向带头循环链表
1.
无头单向非循环链表
:结构简单,⼀般不会单独用来存数据。实际中更多是作为其他数据结
构的⼦结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。
2.
带头双向循环链表
:结构最复杂,⼀般⽤在单独存储数据。实际中使⽤的链表数据结构,都
是带头双向循环链表。另外这个结构虽然结构复杂,但是使⽤代码实现以后会发现结构会带
来很多优势,实现反⽽简单了,后⾯我们代码实现了就知道了。
3.单链表的功能
打印单链表的数据
在链表的头部插入数据(头插)
在链表的尾部插入数据(尾插)
从头部开始删除数据(头删)
从尾部开始删除数据(尾删)
在任意位置之前插入数据
在任意位置之后插入数据
删除某个位置的数据
销毁单链表
4.单链表的定义
需要定义数据域
和指针域
为了程序的健壮性,需要进行宏定义,当我们想保存的数据类型为字符型、浮点型或者其他⾃定义的类型时,方便更改
每个节点都存储了下一个节点的地址
typedef int SQDataType;
typedef struct SListNode
{
SQDataType data;
struct SListNode* next;
}SListNode;
补充说明
:
1、链式机构在逻辑上是连续的,在物理结构上不⼀定连续
2、节点⼀般是从堆上申请的
3、从堆上申请来的空间,是按照⼀定策略分配出来的,每次申请的空间可能连续,可能不连续
5.单链表的功能实现
5.1.打印单链表
头节点不能乱动,可能会找不到下一个节点的地址,所以需要创建一个新的节点,也就是虚拟头节点
只需要让虚拟头节点迭代往后走
代码实现
void SListPrint(SListNode* phead)//头节点不能随便乱动,所以要创建一个新的节点指向头结点的地址
{
SListNode* cur = phead;
while (cur != NULL)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
单链表每次插入都需要创建一个新节点,所以我们需要写一个函数接口,方便下次调用
创建新节点
代码实现
SListNode* BuyListNode(SQDataType x)
{
SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
return newnode;
}
5.2 尾插
需要
传结构体指针的地址
,因为你如果传的是结构体指针 那么只是传值调用,尾插需要二级指针,才能改变一级指针plist
如果头节点为空,让新的节点成为新的头,循环结束条件就是尾巴的下一个地址指向空
void SListPushBack(SListNode** pphead,SQDataType x)
{
SListNode* newnode = BuyListNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
SListNode* tail = *pphead;//tail是结构体地址要改变的是结构体的地址,才能把下一个的地址放在tail,所以要传二级指针
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
5.3 头插
要确保头不为空,需要对头节点断言,同样需要传二级指针,因为要改变的是结构体指针
代码实现
void SListPushfront(SListNode** pphead, SQDataType x)//头插
{
assert(pphead);
SListNode* newnode = BuyListNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
5.4 尾删
尾删同样需要传二级,需要考虑三种情况
删到最后可能链表为空,如果为空,什么都不用返回
剩一个节点,置空头节点
两个节点及以上,先找到尾,再free
代码实现
void SListPopback(SListNode** pphead)
{
if (*pphead == NULL)
{
return;
}
if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else {
while (tail->next->next)
{
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
}
}
5.5 头删
需要传二级指针,确保头节点不为空,链表不为空
代码实现
void SListPopFront(SListNode** pphead, SQDataType x)
{
assert(*pphead!=NULL);
SListNode* next = (*pphead)->next;
free(*pphead);
*pphead = next;
}
5.6 在任意位置之前插入数据
5.6.1找到任意位置的下标
代码实现
SListNode* SListFind(SListNode* phead,SQDataType x)
{
assert(phead);
SListNode* cur = phead;
while (cur)
{
if (cur->data == x)
return cur;
else
cur = cur->next;
}
return NULL;
}
5.6.2在pos位置之前插入
需要考虑头节点为空
和头节点刚好是pos位置
的情况,让新的节点成为新的头
另一种情况的循环的结束条件是pos的前一个迭代到pos的位置
代码实现
void SListInsert(SListNode** pphead, SListNode* pos, SQDataType x)//在pos位置之前插入
{
assert(pphead && pos);
SListNode* newNode = BuyListNode(x);
if (*pphead == NULL || *pphead == pos)
{
newNode->next = *pphead;
*pphead = newNode;
}
else
{
SListNode* posPrev = *pphead;
while (posPrev->next != pos)
{
posPrev = posPrev->next;
}
posPrev->next = newNode;
newNode->next = pos;
}
}
5.7 在任意位置之后插入数据
代码实现
void SListInsertAft(SListNode** pphead, SListNode* pos, SQDataType x)
{
assert(pphead && pos);
SListNode* newNode = BuyListNode(x);
newNode->next = pos->next;
pos->next = newNode;
}
删除某个位置的数据
同样需要考虑头刚好是pos的情况
代码实现
void SListErase(SListNode** pphead, SListNode* pos)
{
if (*pphead == pos)
{
*pphead = pos->next;//保存好头节点
free(pos);
pos = NULL;
}
else
{
SListNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
pos = NULL;
}
}
删除某个位置后的数据
代码实现
void SListEraseAft(SListNode* pos)
{
assert(pos->next);
SListNode* next = pos->next;
pos->next = next->next;
free(next);
}
销毁链表
代码实现
void SListDestory(SListNode** pphead)//销毁单链表
{
assert(pphead);
SListNode* cur = *pphead;//记录下头
while (cur)
{
*pphead = cur->next;
free(cur);
cur = *pphead;
}
}
源码
SList.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SQDataType;
typedef struct SListNode
{
SQDataType data;
struct SListNode* next;
}SListNode;
void SListPushpback(SListNode** pphead, SQDataType x);//尾插
void SListPushfront(SListNode** pphead, SQDataType x);//头插
void SListPopBack(SListNode** pphead);//尾删
void SListPopFront(SListNode** pphead);//头删
void SListPrint(SListNode* phead);//打印单链表
void SListDestory(SListNode** phead);//销毁单链表
SListNode* SListFind(SListNode* phead,SQDataType x);//查找之后返回下标
void SListInsert(SListNode** pphead,SListNode* pos,SQDataType x);//在pos位置之前插入一个节点
void SListInsertAft(SListNode** pphead, SListNode* pos, SQDataType x);//pos位置之后插入一个节点
void SListErase(SListNode** pphead, SListNode* pos);//删掉某位置的节点
void SListEraseAft(SListNode* pos);
void SlistModify(SListNode** pphead, SListNode* pos, SQDataType x);
SList.c
#define _CRT_SECURE_NO_WARNINGS
#include"SList.h"
SListNode* BuyListNode(SQDataType x)
{
SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
newnode->data = x;
newnode->next = NULL;
return newnode;
}
void SListPushBack(SListNode** pphead,SQDataType x)//尾插二级指针是因为要传值调用,解决如果你有第二个第一个节点,你再插入第二个节点就不用再用二级指针
{
SListNode* newnode = BuyListNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
SListNode* tail = *pphead;//tail是结构体地址要改变的是结构体的地址,才能把下一个的地址放在tail,所以要传二级指针
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SListPopback(SListNode** pphead)
{
if (*pphead == NULL)
{
return;
}
if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else {
//#if 0
// SListNode* tail = *pphead;
// SListNode* prev = NULL;
// //while (tail->next != NULL)//循环写的都是继续的条件,比较表达式是逻辑值,为NULL就是为假非NULL为真
// while (tail->next)//隐式类型转换,指针整形,转换成逻辑判断,0为假,非0为真,NULL也是真
// {
// prev = tail;
// tail = tail->next;
//
// }
// free(tail);
// tail = NULL;
// prev->next = NULL;
//#endif
SListNode* tail = *pphead;
while (tail->next->next)
{
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
}
}
void SListPopFront(SListNode** pphead, SQDataType x)//头删
{
/*SListNode* delNode = NULL;
assert(pphead);
if (*pphead == NULL)
{
return;
}
delNode = *pphead;
*pphead = delNode->next;
free(delNode);*/
#if 1
assert(*pphead!=NULL);
SListNode* next = (*pphead)->next;
free(*pphead);
*pphead = next;
#endif
}
void SListPushfront(SListNode** pphead, SQDataType x)//头插
{
assert(pphead);
SListNode* newnode = BuyListNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
void SListPrint(SListNode* phead)//头节点不能随便乱动,所以要创建一个新的节点指向头结点的地址
{
SListNode* cur = phead;
while (cur != NULL)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
void SListDestory(SListNode** pphead)//销毁单链表
{
assert(pphead);
SListNode* cur = *pphead;
while (cur)
{
*pphead = cur->next;
free(cur);
cur = *pphead;
}
}
SListNode* SListFind(SListNode* phead,SQDataType x)
{
assert(phead);
SListNode* cur = phead;
while (cur)
{
if (cur->data == x)
return cur;
else
cur = cur->next;
}
return NULL;
}
void SListModify(SListNode** pphead, SListNode* pos, SQDataType x)
{
assert(pphead&&pos);
SListNode* cur = *pphead;
while (cur)
{
if (cur == pos)
{
cur->data = x;
break;
}
else
{
cur = cur->next;
}
}
}
void SListInsert(SListNode** pphead, SListNode* pos, SQDataType x)//在pos位置之前插入
{
assert(pphead && pos);
/*SListNode* newNode = BuyListNode(x);*/
//if (*pphead == pos)
//{
// newNode->next = *pphead;
// *pphead = newNode;
//}
SListNode* newNode = BuyListNode(x);
if (*pphead == NULL || *pphead == pos)
{
newNode->next = *pphead;
*pphead = newNode;
}
else
{
SListNode* posPrev = *pphead;
while (posPrev->next != pos)
{
posPrev = posPrev->next;
}
posPrev->next = newNode;
newNode->next = pos;
}
}
void SListInsertAft(SListNode** pphead, SListNode* pos, SQDataType x)
{
assert(pphead && pos);
SListNode* newNode = BuyListNode(x);
newNode->next = pos->next;
pos->next = newNode;
}
void SListErase(SListNode** pphead, SListNode* pos)
{
if (*pphead == pos)
{
*pphead = pos->next;
free(pos);
pos = NULL;
}
else
{
SListNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
pos = NULL;
}
}
void SListEraseAft(SListNode* pos)
{
assert(pos->next);
SListNode* next = pos->next;
pos->next = next->next;
free(next);
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include"SList.h"
//void SListTest()
//{
// SListNode* plist = NULL;
// SListPushfront(&plist, 1);
// SListPushfront(&plist, 2);
// SListPushfront(&plist, 3);
// SListPushfront(&plist, 4);
// SListPushfront(&plist, 5);
// SListPushfront(&plist, 2);
// SListPushfront(&plist, 2);
// SListNode* pos = SListFind(plist, 2);
//
// //SListPrint(plist);
printf("下标是%p", pos);
// int i = 1;
// while (pos)
// {
// printf("第%d个pos节点:%p->%d\n", i++, pos, pos->data);
// pos =SListFind( pos->next,2);
// }
//}
void SListTest1()
{
SListNode* plist = NULL;
SListPushfront(&plist, 1);
SListPushfront(&plist, 2);
SListPushfront(&plist, 3);
SListPushfront(&plist, 4);
SListPushfront(&plist, 5);
SListPrint(plist);
SListNode* pos = SListFind(plist, 2);
if (pos)
{
SListInsert(&plist, pos, 30);
}
SListPrint(plist);
pos = SListFind(plist, 5);
if (pos)
{
SListInsert(&plist, pos, 20);
}
SListPrint(plist);
pos = SListFind(plist, 1);
if (pos)
{
SListInsert(&plist, pos, 20);
}
SListPrint(plist);
}
void SListTest2()
{
SListNode* plist = NULL;
SListPushfront(&plist, 1);
SListPushfront(&plist, 2);
SListPushfront(&plist, 3);
SListPushfront(&plist, 4);
SListPushfront(&plist, 5);
SListPrint(plist);
SListNode* pos = SListFind(plist, 2);
if (pos)
{
SListInsertAft(&plist, pos, 30);
}
SListPrint(plist);
pos = SListFind(plist, 1);
if (pos)
{
SListInsertAft(&plist, pos, 20);
}
SListPrint(plist);
pos = SListFind(plist, 1);
if (pos)
{
SListErase(&plist, pos, 20);
}
SListPrint(plist);//删的是pos位置
pos = SListFind(plist, 2);
if (pos)
{
SListEraseAft(pos);
}
SListPrint(plist);
}
int main()
{
SListNode* plist = NULL;
//SListPushBack(&plist, 1);
//SListPushBack(&plist, 2);
//SListPushBack(&plist, 3);
//SListPushBack(&plist, 4);
//SListPrint(plist);
/*SListPushfront(&plist, 1);
SListPushfront(&plist, 2);
SListPushfront(&plist, 3);
SListPushfront(&plist, 4);
SListPushfront(&plist, 5);
SListPrint(plist);*/
/*SListPopback(&plist);
SListPopback(&plist);
SListPopback(&plist);
SListPopback(&plist);
SListPopback(&plist);
SListPopback(&plist);
SListPrint(plist);*/
/*SListPopFront(&plist);
SListPrint(plist);
SListPopFront(&plist);
SListPrint(plist);
SListPopFront(&plist);
SListPrint(plist);
SListPopFront(&plist);
SListPrint(plist);
SListPopFront(&plist);
SListPrint(plist);*/
//SListTest();测试头插尾插
//SListTest1();//测试在pos前插入
SListTest2();//测试在pos后插入
return 0;
}