当前位置:   article > 正文

数据结构之——链表_数据结构链表

数据结构链表

目录

一、链表的概念及结构

二、单链表的实现(无头+单向+非循环链表)

1.单链表节点定义

2.单链表的接口实现

(1)动态申请一个节点

(2)单链表打印

(3)单链表的销毁 

(4)单链表尾插

(5)单链表的头插

(6)单链表的尾删

(7)单链表头删

(8)单链表查找

(9)单链表在pos位置之前插入x

(10)单链表删除pos位置的值

3.单链表测试代码

三、双向链表的实现(带头+双向+循环链表)

1.双向链表节点定义

2.双向链表的接口实现

(1)创建返回链表的头节点

(2)动态申请一个节点

(3)双向链表销毁

(4)双向链表打印

(5)双向链表尾插

(6)双向链表尾删

(7)双向链表头插

(8)双向链表头删

(9)双向链表查找

(10)双向链表在pos的前面进行插入

(11)双向链表删除pos位置的节点

3.双向链表测试代码


一、链表的概念及结构

概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表
中的指针链接次序实现的 。
单链表的逻辑结构:

 单链表的物理结构:

实际中要实现的链表的结构非常多样,以下情况组合起来就有8种链表结构:
1. 单向、双向
2. 带头、不带头
3. 循环、非循环
1.单链表、双向链表

 2.不带头单链表、带头单链表

3.单链表、循环单链表

虽然有这么多的链表的结构,但是我们实际中最常用还是两种结构:

无头单向非循环链表:

带头双向循环链表:

二、单链表的实现(无头+单向+非循环链表)

1.单链表节点定义

  1. typedef int SLTDataType;
  2. //定义一个链表节点
  3. typedef struct SListNode {
  4. SLTDataType data; //数据
  5. struct SListNode* next; //指针,指向下一个节点
  6. }SLTNode;

2.单链表的接口实现

(1)动态申请一个节点

  1. //动态申请一个节点
  2. SLTNode* BuyListNode(SLTDataType x) {
  3. SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
  4. //判断节点是否申请成功
  5. if (newnode == NULL) {
  6. printf("malloc fail\n");
  7. exit(-1);
  8. }
  9. newnode->data = x;
  10. newnode->next = NULL;
  11. return newnode;
  12. }

(2)单链表打印

  1. //打印链表
  2. void SListPrint(SLTNode* phead) {
  3. SLTNode* cur = phead;
  4. while (cur != NULL) {
  5. printf("%d-> ", cur->data);
  6. cur = cur->next;
  7. }
  8. printf("NULL\n");
  9. }

(3)单链表的销毁 

  1. //单链表的销毁 
  2. void SListDestroy(SLTNode** phead) {
  3. assert(*phead); //断言,不满足条件会报错
  4. SLTNode* cur = *phead;
  5. while (cur) {
  6. SLTNode* next = cur->next;
  7. free(cur);
  8. cur = next;
  9. }
  10. *phead = NULL;
  11. }

(4)单链表尾插

有两种情况:1.若链表为空链表,直接将该节点作为首节点;

                      2.若链表不为空,让最后一个节点指向该节点。

  1. //单链表尾插
  2. void SListPushBack(SLTNode** phead, SLTDataType x) {
  3. SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
  4. newnode->data = x;
  5. newnode->next = NULL;
  6. if (*phead == NULL)
  7. *phead = newnode;
  8. else {
  9. SLTNode* tail = *phead;
  10. while (tail->next != NULL) {
  11. tail = tail->next;
  12. }
  13. tail->next = newnode;
  14. }
  15. }

(5)单链表的头插

先让该节点指向链表的首节点,再让首节点指向该节点。

   

  1. //单链表的头插
  2. void SListPushFront(SLTNode** phead, SLTDataType x){
  3. SLTNode* newnode = BuyListNode(x);
  4. newnode->next = *phead;
  5. *phead = newnode;
  6. }

(6)单链表的尾删

1.若链表只有一个节点,直接将该节点删除。

 2.若链表不只有一个节点,先找到尾结点的前一个节点,然后删除尾结点,再让尾结点的前一个节点指向NULL。

  1. //单链表的尾删
  2. void SListPopBack(SLTNode** phead) {
  3. assert(*phead != NULL); //断言,不满足条件会报错
  4. if ((*phead)->next == NULL) {
  5. free(*phead);
  6. *phead = NULL;
  7. }
  8. else {
  9. SLTNode* tail = *phead;
  10. while (tail->next->next) {
  11. tail = tail->next;
  12. }
  13. free(tail->next);
  14. tail->next = NULL;
  15. }
  16. }

(7)单链表头删

先记录第二个节点的地址,再删除首节点,最后让首节点指向第二个节点。

  1. //单链表头删
  2. void SListPopFornt(SLTNode** phead) {
  3. assert(*phead != NULL);
  4. SLTNode* next = (*phead)->next;
  5. free(*phead);
  6. *phead = next;
  7. }

(8)单链表查找

  1. //单链表查找
  2. SLTNode* SListFind(SLTNode* phead, SLTDataType x) {
  3. SLTNode* cur = phead;
  4. while (cur) {
  5. if (cur->data == x) {
  6. return cur;
  7. }
  8. else {
  9. cur = cur->next;
  10. }
  11. }
  12. return NULL;
  13. }

(9)单链表在pos位置之前插入x

1.若pos指向首节点,相当于头插。

2.若pos没有指向首节点,先找到pos位置的前一个节点,再让该节点指向新节点,最后让新节点指向pos位置的节点。

  1. //单链表在pos位置之前插入x
  2. void SListInsert(SLTNode** phead, SLTNode* pos, SLTDataType x) {
  3. SLTNode* newnode = BuyListNode(x);
  4. if (*phead == pos) {
  5. newnode->next = *phead;
  6. *phead = newnode;
  7. }
  8. else {
  9. //找到pos的前一个位置
  10. SLTNode* posPrev = *phead;
  11. while (posPrev->next != pos) {
  12. posPrev = posPrev->next;
  13. }
  14. posPrev->next = newnode;
  15. newnode->next = pos;
  16. }
  17. }

(10)单链表删除pos位置的值

1.若pos指向首节点,先让首节点指向下一个节点,再删除pos位置的节点。

 2.若pos没有指向首节点,先找到pos位置的前一个节点,让该节点指向pos位置的后一个节点,最后再删除pos位置的节点。

  1. //单链表删除pos位置的值
  2. void SListErase(SLTNode** phead, SLTNode* pos) {
  3. if (*phead == pos) {
  4. *phead = pos->next;
  5. free(pos);
  6. }
  7. else {
  8. SLTNode* prev = *phead;
  9. while (prev->next != pos) {
  10. prev = prev->next;
  11. }
  12. prev->next = pos->next;
  13. free(pos);
  14. }
  15. }

3.单链表测试代码

  1. void test() {
  2. SLTNode* phead = NULL;
  3. printf("尾插:");
  4. SListPushBack(&phead, 1);
  5. SListPushBack(&phead, 2);
  6. SListPushBack(&phead, 3);
  7. SListPrint(phead);
  8. printf("头插:");
  9. SListPushFront(&phead, 4);
  10. SListPushFront(&phead, 5);
  11. SListPrint(phead);
  12. printf("尾删:");
  13. SListPopBack(&phead);
  14. SListPrint(phead);
  15. printf("头删:");
  16. SListPopFornt(&phead);
  17. SListPrint(phead);
  18. printf("在1前插入6:");
  19. SLTNode* find = SListFind(phead, 1);
  20. SListInsert(&phead, find, 6);
  21. SListPrint(phead);
  22. printf("删除1:");
  23. find = SListFind(phead, 1);
  24. SListErase(&phead, find);
  25. SListPrint(phead);
  26. }
  27. int main()
  28. {
  29. test();
  30. return 0;
  31. }

三、双向链表的实现(带头+双向+循环链表

1.双向链表节点定义

  1. typedef int LTDataType;
  2. //定义一个链表节点
  3. typedef struct LTNode
  4. {
  5. LTDataType data;
  6. struct LTNode* next;
  7. struct LTNode* prev;
  8. }LTNode;

2.双向链表的接口实现

(1)创建返回链表的头节点

  1. //创建返回链表的头节点
  2. LTNode* ListInit() {
  3. LTNode* phead = (LTNode*)malloc(sizeof(LTNode));
  4. phead->next = phead;
  5. phead->prev = phead;
  6. return phead;
  7. }

(2)动态申请一个节点

  1. // 动态申请一个节点
  2. LTNode* BuyListNode(LTDataType x)
  3. {
  4. LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
  5. newnode->data = x;
  6. newnode->next = NULL;
  7. newnode->prev = NULL;
  8. return newnode;
  9. }

(3)双向链表销毁

  1. // 双向链表销毁
  2. void ListDestory(LTNode* phead) {
  3. assert(phead);
  4. LTNode* cur = phead->next;
  5. while (cur != phead) {
  6. LTNode* next = cur->next;
  7. free(cur);
  8. cur = next;
  9. }
  10. free(phead);
  11. phead = NULL;
  12. }

(4)双向链表打印

  1. void ListPrint(LTNode* phead) {
  2. assert(phead);
  3. LTNode* cur = phead->next;
  4. while (cur != phead) {
  5. printf("%d ", cur->data);
  6. cur = cur->next;
  7. }
  8. printf("\n");
  9. }

(5)双向链表尾插

先找到尾节点(头节点的前指针指向的就是尾节点),先让尾节点的后指针指向新节点,新节点的前指针指向尾节点,后指针指向头节点,最后再让头节点的前指针指向新节点。

  1. // 双向链表尾插
  2. void ListPushBack(LTNode* phead, LTDataType x) {
  3. assert(phead);
  4. LTNode* tail = phead->prev;
  5. LTNode* newnode = BuyListNode(x);
  6. tail->next = newnode;
  7. newnode->prev = tail;
  8. newnode->next = phead;
  9. phead->prev = newnode;
  10. }

(6)双向链表尾删

先找到尾节点的前一个节点(tailPrev),再删除尾节点,让tailPrev的后指针指向头节点,头节点的前指针指向tailPrev。

  1. // 双向链表尾删
  2. void ListPopBack(LTNode* phead) {
  3. assert(phead);
  4. assert(phead->next != phead); //链表为空
  5. LTNode* tail = phead->prev;
  6. LTNode* tailPrev = tail->prev;
  7. free(tail);
  8. tail = NULL;
  9. tailPrev->next = phead;
  10. phead->prev = tailPrev;
  11. }

(7)双向链表头插

先找到第一个节点(next),再让头节点的后指针指向新节点,新节点的前指针指向头节点,最后再让新节点的后指针指向next,next的前指针指向新节点。

  1. // 双向链表头插
  2. void ListPushFront(LTNode* phead, LTDataType x) {
  3. assert(phead);
  4. LTNode* newnode = BuyListNode(x);
  5. LTNode* next = phead->next;
  6. phead->next = newnode;
  7. newnode->prev = phead;
  8. newnode->next = next;
  9. next->prev = newnode;
  10. }

(8)双向链表头删

先找到第一个节点(next)和第二个节点(nextNext),再删除next节点,让头节点的后指针指向nextNext,nextNext的前指针指向头节点。

  1. // 双向链表头删
  2. void ListPopFront(LTNode* phead) {
  3. assert(phead);
  4. assert(phead->next != phead); //链表为空
  5. LTNode* next = phead->next;
  6. LTNode* nextNext = next->next;
  7. free(next);
  8. next = NULL;
  9. phead->next = nextNext;
  10. nextNext->prev = phead;
  11. }

(9)双向链表查找

  1. // 双向链表查找
  2. LTNode* ListFind(LTNode* phead, LTDataType x)
  3. {
  4. assert(phead);
  5. LTNode* cur = phead->next;
  6. while (cur != phead) {
  7. if (cur->data == x)
  8. return cur;
  9. cur = cur->next;
  10. }
  11. return NULL;
  12. }

(10)双向链表在pos的前面进行插入

先找到pos的前一个节点(posPrev),让posPrev的后指针指向新节点,新节点的前指针指向posPrev,最后让新节点的后指针指向pos,pos的前指针指向新节点。

  1. // 双向链表在pos的前面进行插入
  2. void ListInsert(LTNode* pos, LTDataType x) {
  3. assert(pos);
  4. LTNode* posPrev = pos->prev;
  5. LTNode* newnode = BuyListNode(x);
  6. posPrev->next = newnode;
  7. newnode->prev = posPrev;
  8. newnode->next = pos;
  9. pos->prev = newnode;
  10. }

(11)双向链表删除pos位置的节点

先找到pos的前一个节点(posPrev)和后一个节点(posNext),再删除pos节点,让posPrev的后指针指向posNext,posNext的前指针指向posPrev。

  1. // 双向链表删除pos位置的节点
  2. void ListErase(LTNode* pos) {
  3. assert(pos);
  4. LTNode* posPrev = pos->prev;
  5. LTNode* posNext = pos->next;
  6. free(pos);
  7. pos = NULL;
  8. posPrev->next = posNext;
  9. posNext->prev = posPrev;
  10. }

3.双向链表测试代码

  1. void test() {
  2. LTNode* head = ListInit();
  3. printf("尾插:");
  4. ListPushBack(head, 1);
  5. ListPushBack(head, 2);
  6. ListPushBack(head, 3);
  7. ListPrint(head);
  8. printf("尾删:");
  9. ListPopBack(head);
  10. ListPrint(head);
  11. printf("头插:");
  12. ListPushFront(head, 4);
  13. ListPushFront(head, 5);
  14. ListPrint(head);
  15. printf("头删:");
  16. ListPopFront(head);
  17. ListPrint(head);
  18. printf("头删:");
  19. ListPopFront(head);
  20. ListPrint(head);
  21. printf("在2之前插入8、9:");
  22. LTNode* find = ListFind(head, 2);
  23. ListInsert(find, 8);
  24. ListInsert(find, 9);
  25. ListPrint(head);
  26. printf("删除8:");
  27. find = ListFind(head, 8);
  28. ListErase(find);
  29. ListPrint(head);
  30. }
  31. int main()
  32. {
  33. test();
  34. printf("\n");
  35. return 0;
  36. }

当你足够努力,幸运总会与你不期而遇!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/429773
推荐阅读
相关标签
  

闽ICP备14008679号