网站数据库安装教程,中国纪检监察报记者电话,东莞seo广告宣传,建设网站后期人员薪酬3.链表
3.1 链表的概念及结构
概念#xff1a;链表是一种物理存储结构上非连续、非顺序的存储结构#xff0c;数据元素的逻辑顺序是通过链表中的指针链接次序实现的 链表的物理结构 1.从上图可看出#xff0c;链式结构在逻辑上是连续的#xff0c;但是在物理上不一定连续…3.链表
3.1 链表的概念及结构
概念链表是一种物理存储结构上非连续、非顺序的存储结构数据元素的逻辑顺序是通过链表中的指针链接次序实现的 链表的物理结构 1.从上图可看出链式结构在逻辑上是连续的但是在物理上不一定连续 2.现实中的结点一般都是从堆上申请出来的 3.从堆上申请的空间是按照一定的策略来分配的两次申请的空间可能连续也可能不连续
3.2 链表的分类
实际中链表的结构非常多样以下情况组合起来就有8种链表结构
单向或者双向 3. 带头或者不带头 4. 循环或者非循环
虽然有这么多的链表的结构但是我们实际中最常用还是两种结构
无头单向非循环链表结构简单一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。带头双向循环链表结构最复杂一般用在单独存储数据。实际中使用的链表数据结构都是带头双向循环链表。另外这个结构虽然结构复杂但是使用代码实现以后会发现结构会带来很多优势实现反而简单了。
3.3 无头单向非循环链表增删查改接口实现 typedef int SLTDateType; typedef struct SListNode { SLTDateType data; struct SListNode* next; }SListNode; // 动态申请一个节点 SListNode* BuySListNode(SLTDateType x); // 单链表打印 void SListPrint(SListNode* plist); // 单链表尾插 void SListPushBack(SListNode** pplist, SLTDateType x); // 单链表的头插 void SListPushFront(SListNode** pplist, SLTDateType x); // 单链表的尾删 void SListPopBack(SListNode** pplist); // 单链表头删 void SListPopFront(SListNode** pplist); // 单链表查找 SListNode* SListFind(SListNode* plist, SLTDateType x); // 单链表在pos位置之后插入x void SListInsertAfter(SListNode* pos, SLTDateType x); // 单链表删除pos位置之后的值 void SListEraseAfter(SListNode* pos); 3.3.1 动态申请一个节点
// 动态申请一个节点
SListNode* BuySListNode(SLTDateType x)
{SListNode* newnode (SListNode*)malloc(sizeof(SListNode));if (newnode NULL){perror(BuySListNode::mallo fail!);return NULL;}newnode-data x;newnode-next NULL;return newnode;
}3.3.2 单链表打印
void SListPrint(SListNode* phead)
{SListNode *cur phead;while (cur){printf(%d-, cur-data);cur cur-next;}printf(NULL\n);
}3.3.3 单链表尾插 思路 void SListPushBack(SListNode** pphead, SLTDateType x)
{SListNode* newnode BuySListNode(x);if (*pphead NULL){*pphead newnode;}else{//找尾SListNode* tail *pphead;while (tail-next ! NULL){tail tail-next;}tail-next newnode;}
}3.3.4 单链表的头插 思路比较简单 void SListPushFront(SListNode** pphead, SLTDateType x)
{SListNode* newnode BuySListNode(x);newnode-next *pphead;*pphead newnode;
} 3.3.5 单链表的尾删 思路 void SListPopBack(SListNode** pphead)
{assert(*pphead);// 1、只有一个节点if ((*pphead)-next NULL){free(*pphead);*pphead NULL;}else{// 2、多个节点//找尾SListNode* prev NULL;SListNode* tail *pphead;while (tail-next ! NULL){prev tail;tail tail-next;}free(tail);tail NULL;prev-next NULL;}
}3.3.6 单链表头删 思路 void SListPopFront(SListNode** pphead)
{assert(*pphead);SListNode* first *pphead;*pphead first-next;free(first);first NULL;
}3.3.7 单链表查找 思路比较简单如果找到就返回找到的结点没找到就返回NULL SListNode* SListFind(SListNode* phead, SLTDateType x)
{SListNode* cur phead;while (cur){if (cur-data x){return cur;}cur cur-next;}return NULL;
}3.3.8 单链表在pos位置之后插入x 思路,比较简单。 void SListInsertAfter(SListNode* pos, SLTDateType x)
{assert(pos);SListNode* newnode BuySListNode(x);newnode-next pos-next;pos-next newnode;
}3.3.9 单链表删除pos位置之后的值
void SListEraseAfter(SListNode* pos)
{assert(pos);assert(pos-next);SListNode* del pos-next;pos-next del-next;free(del);del NULL;
}