🚀 作者简介:一名在后端领域学习,并渴望能够学有所成的追梦人。
🐌 个人主页:蜗牛牛啊
🔥 系列专栏:🛹初出茅庐C语言、🛴数据结构
📕 学习格言:博观而约取,厚积而薄发
🌹 欢迎进来的小伙伴,如果小伙伴们在学习的过程中,发现有需要纠正的地方,烦请指正,希望能够与诸君一同成长! 🌹
通过对顺序表的学习,我们可以发现顺序表有以下几点缺陷:
1.空间不够时需要扩容,扩容尤其是用realloc
进行异地扩容时,是有一定代价的,其次还可能存在一定空间浪费。
2.头部或者中间插入删除,需要挪动数据,效率低下。
那我们可以对其所具有的缺陷进行优化:可以按需申请空间同时插入删除时不挪动数据。而单链表就符合这些优化所具有特点。但是我们要注意的是单链表具有这些优点并不代表链表就可以完全替代顺序表。
链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。
从上图可以看出,链式结构在逻辑上是连续的,但是在物理上不一定连续,物理结构中指针域存储的是下一个结点的地址;而且链式结构的结点一般都是从堆上申请出来的;从堆上申请的空间,是按照一定的策略来分配的,两次申请的空间可能连续,也可能不连续。链表存储数据的区域可分为数据域和指针域:
用代码表示链表中的数据域和指针域如下:
typedef int STLDataType;
typedef struct SListNode
{SLTDataType data;struct SListNode* next;
}STLNode;
思考:为什么在申请的时候需要申请堆上的空间?因为堆上申请的空间存储数据的时候不会因为函数销毁而销毁,堆上的空间需要的时候就申请,不需要的时候就释放;函数栈帧上开辟的空间存储的数据会因为函数栈帧的销毁而释放,后面在进行尾插和头插时候新的结点不能链接到表上。
SLTNode* BuySLTNode(SLTDataType x)
{SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));if (newnode == NULL){perror("BuySLTNode malloc");exit(-1);}newnode->val = x;newnode->next = NULL;return newnode;
}
SLTNode* CreateSList(int n)
{int i = 0;SLTNode* ptail = NULL,*phead = NULL;for (i = 0; i < n; i++){SLTNode* newnode = BuySLTNode(i);if (phead == NULL){ptail = phead = newnode;}else{ptail->next = newnode;ptail = newnode;}}return phead;
}
//void CreateSList(SLTNode** pphead, int n)
//{
// int i = 0;
// SLTNode* ptail = NULL;
// for (i = 0; i < n; i++)
// {
// SLTNode* newnode = BuySLTNode(i);
// if (*pphead == NULL)
// {
// ptail = *pphead = newnode;
// }
// else
// {
// ptail->next= newnode;
// ptail = newnode;
// }
// }
//}
上述代码中提供了两种实现方式,没有注释的是返回头指针的,注释内容是没有返回值,形参使用的是二级指针改变头指针指向的地址。
void SLTPrint(SLTNode* phead)
{SLTNode* tail = phead;while (tail){printf("%d ", tail->val);tail = tail->next;}printf("\n");
}
void SLTPushBack(SLTNode** pphead, SLTDataType x)//尾插
{SLTNode* tail = *pphead;SLTNode* newnode = BuySLTNode(x);if (*pphead == NULL){*pphead = newnode;}else{while (tail->next){tail = tail->next;}tail->next = newnode;}
}
尾插时,要注意当单链表的头结点为空的时候,要先将新结点作为头结点。因为当头结点为空时,需要改变头指针,所以传过来的为二级指针(也可以使用一级指针,不过此时要有返回值),要想改变
SLTNode*
的值,就要传递它的地址即SLTNode**
类型。
void SLTPopBack(SLTNode** pphead)//尾删
{assert(*pphead);if ((*pphead)->next==NULL){free(*pphead);*pphead = NULL;}else{SLTNode* tail = *pphead;SLTNode* prev = NULL;while (tail->next){prev = tail;tail = tail->next;}free(tail);prev->next = NULL;}
}
尾删时也要注意区分是否为头结点;同时注意删除后,要将其置空
prev->next = NULL
。
void SLTPushFront(SLTNode** pphead, SLTDataType x)//头插
{SLTNode* newnode = BuySLTNode(x);newnode->next = *pphead;*pphead = newnode;
}
void SLTPopFront(SLTNode** pphead)//头删
{assert(*pphead);SLTNode* nextnode = (*pphead)->next;free(*pphead);*pphead = nextnode;
}
头插和头删是单链表的优势,尾插和尾删是顺序表的优势。同时在尾删时注意将
*pphead
加上括号用来区分优先级,否则会报错。
SLTNode* SLTFind(SLTNode* phead, SLTDataType x)//查找某个数并返回所在位置
{SLTNode* tail = phead;while (tail){if (tail->val == x){return tail;}tail = tail->next;}return NULL;
}
注意循环条件,当循环条件写为
while(tail->next)
时,会有空指针的风险,同时还会漏掉最后一个结点。
void SLInsertAfter(SLTNode* pos, SLTDataType x)//在某个结点的后面插入某个数
{assert(pos);SLTNode* newnode = BuySLTNode(x);SLTNode* tail = pos->next;pos->next = newnode;newnode->next = tail;
}
void SLTInsert(SLTNode** pphead,SLTNode* pos, SLTDataType x)//在某个结点的前面插入某个数
{assert(pos);SLTNode* newnode = BuySLTNode(x);if (pos == *pphead){/*newnode->next = *pphead;*pphead = newnode;*/SLTPushBack(pphead, x);}else{SLTNode* tail = *pphead;while (tail->next != pos){tail = tail->next;}tail->next = newnode;newnode->next = pos;}
}
void SLTEraseAfter(SLTNode* pos)//删除pos位置之后的那个位置
{assert(pos);if (pos->next == NULL){return;}else{SLTNode* tail = pos->next;pos->next = tail->next;free(tail);}
}
void SLTErase(SLTNode** pphead, SLTNode* pos)//删除pos位置
{assert(pos);SLTNode* tail = *pphead;if (pos == *pphead){SLTNode* nextNode = (*pphead)->next;free(*pphead);*pphead = nextNode;}else{while (tail->next != pos){tail = tail->next;}tail->next = pos->next;free(pos);}
}
void SLTDestroy(SLTNode** pphead)//销毁
{assert(*pphead);SLTNode* tail = *pphead;while (tail){SLTNode* next = tail->next;free(tail);tail = next;}*pphead = NULL;
}
注意最后要将头结点置空。
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
typedef int SLTDataType;
typedef struct SList {struct Slist* next;SLTDataType val;
}SLTNode;
SLTNode* BuySLTNode(SLTDataType x)
{SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));if (newnode == NULL){perror("BuySLTNode malloc");exit(-1);}newnode->val = x;newnode->next = NULL;return newnode;
}
SLTNode* CreateSList(int n)
{int i = 0;SLTNode* ptail = NULL, * phead = NULL;for (i = 0; i < n; i++){SLTNode* newnode = BuySLTNode(i);if (phead == NULL){ptail = phead = newnode;}else{ptail->next = newnode;ptail = newnode;}}return phead;
}
//void CreateSList(SLTNode** pphead, int n)
//{
// int i = 0;
// SLTNode* ptail = NULL;
// for (i = 0; i < n; i++)
// {
// SLTNode* newnode = BuySLTNode(i);
// if (*pphead == NULL)
// {
// ptail = *pphead = newnode;
// }
// else
// {
// ptail->next= newnode;
// ptail = newnode;
// }
// }
//}
void SLTPrint(SLTNode* phead)
{SLTNode* tail = phead;while (tail){printf("%d ", tail->val);tail = tail->next;}printf("\n");
}
void SLTPushBack(SLTNode** pphead, SLTDataType x)//尾插
{SLTNode* tail = *pphead;SLTNode* newnode = BuySLTNode(x);if (*pphead == NULL){*pphead = newnode;}else{while (tail->next){tail = tail->next;}tail->next = newnode;}
}
void SLTPopBack(SLTNode** pphead)//尾删
{assert(*pphead);if ((*pphead)->next == NULL){free(*pphead);*pphead = NULL;}else{SLTNode* tail = *pphead;SLTNode* prev = NULL;while (tail->next){prev = tail;tail = tail->next;}free(tail);prev->next = NULL;}
}
void SLTPushFront(SLTNode** pphead, SLTDataType x)//头插
{SLTNode* newnode = BuySLTNode(x);newnode->next = *pphead;*pphead = newnode;
}
void SLTPopFront(SLTNode** pphead)//头删
{assert(*pphead);SLTNode* nextnode = (*pphead)->next;free(*pphead);*pphead = nextnode;
}
SLTNode* SLTFind(SLTNode* phead, SLTDataType x)//查找某个数并返回所在位置
{SLTNode* tail = phead;while (tail){if (tail->val == x){return tail;}tail = tail->next;}return NULL;
}
void SLInsertAfter(SLTNode* pos, SLTDataType x)//在某个结点的后面插入某个数
{assert(pos);SLTNode* newnode = BuySLTNode(x);SLTNode* tail = pos->next;pos->next = newnode;newnode->next = tail;
}
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)//在某个结点的前面插入某个数
{assert(pos);SLTNode* newnode = BuySLTNode(x);if (pos == *pphead){/*newnode->next = *pphead;*pphead = newnode;*/SLTPushBack(pphead, x);}else{SLTNode* tail = *pphead;while (tail->next != pos){tail = tail->next;}tail->next = newnode;newnode->next = pos;}
}
void SLTEraseAfter(SLTNode* pos)//删除pos位置之后的那个位置
{assert(pos);if (pos->next == NULL){return;}else{SLTNode* tail = pos->next;pos->next = tail->next;free(tail);}
}
void SLTErase(SLTNode** pphead, SLTNode* pos)//删除pos位置
{assert(pos);SLTNode* tail = *pphead;if (pos == *pphead){SLTNode* nextNode = (*pphead)->next;free(*pphead);*pphead = nextNode;}else{while (tail->next != pos){tail = tail->next;}tail->next = pos->next;free(pos);}
}
void SLTDestroy(SLTNode** pphead)//销毁
{assert(*pphead);SLTNode* tail = *pphead;while (tail){SLTNode* next = tail->next;free(tail);tail = next;}*pphead = NULL;
}
void TestSL()
{SLTNode* plist = CreateSList(2);/*SLTNode* plist = NULL;CreateSList(&plist,10);//没有返回值SLTPrint(plist);*/SLTPushBack(&plist, 600);SLTPushBack(&plist, 200);SLTPushBack(&plist, 300);SLTPushBack(&plist, 400);SLTPrint(plist);SLTPopBack(&plist);SLTPopBack(&plist);SLTPopBack(&plist);SLTPrint(plist);SLTPushFront(&plist, 100);SLTPushFront(&plist, 200);SLTPushFront(&plist, 300);SLTPushFront(&plist, 400);SLTPrint(plist);SLTPopFront(&plist);SLTPopFront(&plist);SLTPrint(plist);SLTNode* pos = SLTFind(plist, 600);if (pos){SLInsertAfter(pos, 700);SLTInsert(&plist, pos, 500);SLTEraseAfter(pos);//SLTErase(&plist, pos);}SLTPrint(plist);SLTDestroy(&plist);SLTPrint(plist);
}
int main()
{TestSL();return 0;
}