基础知识
线性表
线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串…
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的.
线性表在物理上存储时,通常以数组(顺序表)和链式结构(链表)的形式存储。
顺序表
在之前的通讯录中我们使用了顺序表来存放每个人的信息。但发现顺序表会出现以下的问题:
- 中间或头部插入会导致时间复杂度变成O(N);
- 增容会重新开辟空间,拷贝数据,释放空间,会有一定的消耗
- 增容呈两倍扩容,在某些情况下,会造成极大的空间浪费
链表
链表:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的 。
注:
- 从上图所见,链表在逻辑结构上是连续的,但在物理结构上不一定
- 节点一般都是从堆上申请过来的
- 从堆上申请的空间,是按照一定的策略来分配的,两次申请空间可能连续可能不连续。
链表的分类
实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:
- 单向或者双向
- 带头或者不带头
- 循环或者非循环
常用的数据结构,也就只有两种:
无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。
带头双向循环链表:结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都是带头双向循环链表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带来很多优势,实现反而简单了,后面我们代码实现了就知道了。
链表的实现
无头+单向+非循环链表增删查改实现
typedef int SLTDateTybe;
typedef struct SListNode
{
SLTDateTybe data;
struct SListNode* next;
}SLTNode;
data 用来保存数据,next 用来储存下个节点的地址
创建新节点
SLTNode* Push(SLTDateTybe x)
{
SLTNode* tmp = (SLTNode*)malloc(sizeof(SLTNode));
if (tmp == NULL)
{
perror("tmp");
return;
}
tmp->data = x;
tmp->next = NULL;
return tmp;
}
尾插
void SLTPushBack(SLTNode** pphead, SLTDateTybe x)
{
assert(pphead);
if (*pphead == NULL)
{
*pphead = Push(x);
}
else
{
SLTNode* tmp = *pphead;
while (tmp->next!=NULL)
{
tmp = tmp->next;
}
tmp->next = Push(x);
}
}
头插
void SLTPushFront(SLTNode** pphead, SLTDateTybe x)
{
assert(pphead);
SLTNode* tmp = Push(x);
tmp->next = *pphead;
*pphead = tmp;
}
打印
void SLTPrint(SLTNode* phead)
{
while (phead)
{
printf("%d-> ", phead->data);
phead = phead->next;
}
printf("NULL\n");
}
头删
void SLTPopFront(SLTNode** pphead)
{
assert(*pphead);
SLTNode* del = *pphead;
*pphead = (*pphead)->next;
free(del);
}
尾删
void SLTPopBack(SLTNode** pphead)
{
assert(*pphead); //*pphead为空时
SLTNode* tail = *pphead;
if ((*pphead)->next == NULL) //*pphead只有一个节点时
{
free(*pphead);
*pphead = NULL;
}
else
{
while (tail->next->next)
{
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
}
}
查找
SLTNode* SLTFind(SLTNode* phead, SLTDateTybe x)
{
assert(phead);
SLTNode* tail = phead;
while (tail)
{
if (tail->data == x)
{
return tail;
}
tail = tail->next;
}
return NULL;
}
在pos位置之前插入 x
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDateTybe x)
{
assert(pphead);
assert(pos);
SLTNode* tail = *pphead;
if (*pphead == pos)
{
SLTNode* tmp = Push(x);
tmp -> next = *pphead;
*pphead = tmp;
}
else
{
while (tail->next != pos)
{
tail = tail->next;
}
SLTNode* tmp = Push(x);
tail->next = tmp;
tmp->next = pos;
}
}
删除pos位置的值
void SLTErase(SLTNode** pphead, SLTNode* pos)
{
assert(*pphead);
assert(pphead);
if (*pphead == pos)
{
SLTPopFront(pphead);
}
else
{
SLTNode* tail = *pphead;
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* tmp = tail->next;
free(tail);
tail = tmp;
}
*pphead = NULL;
}
运行实例
#include"SListNode.h"
int main()
{
SLTNode* s = NULL;
SLTPushBack(&s, 1);
SLTPushBack(&s, 2);
SLTPushBack(&s, 3);
SLTPushBack(&s, 4);
SLTPushBack(&s, 5);
SLTPrint(s);
SLTNode* tmp = SLTFind(s, 1);
if (tmp != NULL)
{
SLTErase(&s, tmp);
}
SLTPrint(s);
SLTDestroy(&s);
SLTPrint(s);
return 0;
}