鱼C论坛

 找回密码
 立即注册
查看: 2963|回复: 4

链表的一些基础应用

[复制链接]
发表于 2021-6-21 09:41:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdlib.h>

#include "LinkedList.h"

typedef struct linkedlist {
    struct linkedlistnode *head;
} linkedlist;

typedef struct linkedlistnode {
    int data;
    struct linkedlistnode *next;
} linkedlistnode;

ll LLCreate(void)
{
    // TODO: replace this with your code
    // make the compiler happy
    return NULL;
}

void LLDestroy(ll list)
{
    // TODO: replace this with your code
    // make the compiler happy
    (void) list;
}

/**
* Add an element to the end of the link-list
*/
ll LLAppend(ll list, int item)
{
    // TODO: replace this with your code
    // make the compiler happy
    (void) list;
    (void) item;
    return NULL;
}

/**
* Add an element to the start of the link-list
*/
ll LLPrepend(ll list, int item)
{
    // TODO: replace this with your code
    // make the compiler happy
    (void) list;
    (void) item;
    return NULL;
}

/**
* Remove and return the last element from the link-list
* Or 0 if the list is empty
*/
int LLShrink(ll list)
{
    // TODO: replace this with your code
    // make the compiler happy
    (void) list;
    return 0;
}

/**
* Remove and return the first element from the link-list
* Or 0 if the list is empty
*/
int LLShift(ll list)
{
    // TODO: replace this with your code
    // make the compiler happy
    (void) list;
    return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-21 09:43:51 | 显示全部楼层

                               
登录/注册后可看大图
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-6-21 10:07:55 | 显示全部楼层
有偿!!!方便的话加个好友救救孩子
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-6-21 12:20:16 | 显示全部楼层
Hugo888 发表于 2021-6-21 10:07
有偿!!!方便的话加个好友救救孩子

你得等大佬们有时间回答你的问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-6-21 17:45:09 | 显示全部楼层
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>

  4. typedef struct linkedlist {
  5.     struct linkedlistnode* head;
  6. } linkedlist;

  7. typedef struct linkedlistnode {
  8.     int data;
  9.     struct linkedlistnode* next;
  10. } linkedlistnode;

  11. //你没给 ll 的定义, 我猜的
  12. typedef linkedlist* ll;

  13. ll LLCreate(void)
  14. {
  15.     // TODO: replace this with your code
  16.     // make the compiler happy
  17.     ll list = (ll)malloc(sizeof(linkedlist));   //申请内存
  18.     if (list == NULL)                           //申请失败退出
  19.         return NULL;

  20.     list->head = NULL;                          //设置初始值

  21.     return list;
  22. }

  23. void LLDestroy(ll list)
  24. {
  25.     // TODO: replace this with your code
  26.     // make the compiler happy
  27.     //(void)list;
  28.     linkedlistnode* cur = list->head;   //当前对象指针
  29.     linkedlistnode* next;               //下个对象指针

  30.     while (cur != NULL)
  31.     {
  32.         next = cur->next;               //下个对象指针 = 当前对象指针的next
  33.         free(cur);                      //释放当前对象
  34.         cur = next;                     //进入下一个对象
  35.     }

  36.     free(list);                         //释放玩所有节点后,释放链表对象
  37. }

  38. /**
  39. * Add an element to the end of the link-list
  40. */
  41. ll LLAppend(ll list, int item)
  42. {
  43.     // TODO: replace this with your code
  44.     // make the compiler happy
  45.     //(void)list;
  46.     //(void)item;
  47.     if (list->head == NULL)     //如果头节点为空,则初始化头节点就可以了
  48.     {
  49.         list->head = (linkedlistnode*)malloc(sizeof(linkedlistnode));
  50.         if (list->head == NULL)
  51.             return list;

  52.         list->head->data = item;
  53.         list->head->next = NULL;
  54.         return list;
  55.     }

  56.     linkedlistnode* tail = list->head;      //尾节点
  57.     linkedlistnode* next = list->head;      //下个节点
  58.     while (next != NULL)                    //循环找到最后一个节点
  59.     {
  60.         tail = next;                        //当前的next就是当前的尾
  61.         next = next->next;                  //next往下遍历
  62.     }
  63.     //找到尾节点后,在尾节点后添加新的节点
  64.     tail->next = (linkedlistnode*)malloc(sizeof(linkedlistnode));
  65.     if (tail->next == NULL)
  66.         return list;
  67.     //初始化
  68.     tail->next->data = item;
  69.     tail->next->next = NULL;

  70.     return list;
  71. }

  72. /**
  73. * Add an element to the start of the link-list
  74. */
  75. ll LLPrepend(ll list, int item)
  76. {
  77.     // TODO: replace this with your code
  78.     // make the compiler happy
  79.     //(void)list;
  80.     //(void)item;
  81.     //创建新节点
  82.     linkedlistnode* head = (linkedlistnode*)malloc(sizeof(linkedlistnode));
  83.     if (head == NULL)
  84.         return list;
  85.    
  86.     head->data = item;          //初始化数据
  87.     head->next = list->head;    //新节点的下个节点 设为 之前的头节点
  88.     list->head = head;          //让链表的头节点指向新节点

  89.     return list;
  90. }

  91. /**
  92. * Remove and return the last element from the link-list
  93. * Or 0 if the list is empty
  94. */
  95. int LLShrink(ll list)
  96. {
  97.     // TODO: replace this with your code
  98.     // make the compiler happy
  99.     //(void)list;
  100.     if (list == NULL || list->head == NULL)     //链表为空直接返回
  101.         return 0;

  102.     int index = 0;                              //记录序号
  103.     linkedlistnode* next = list->head;          //下个节点
  104.     linkedlistnode* tail = list->head;          //尾节点
  105.     while (next->next != NULL)                  //遍历找到尾节点的前一个节点
  106.     {
  107.         tail = next;
  108.         next = next->next;
  109.         index++;                                //遍历时,找到尾节点的序号
  110.     }

  111.     tail->next = NULL;          //断开最后一个节点的连接
  112.     free(next);                 //释放空间

  113.     return index;
  114. }

  115. /**
  116. * Remove and return the first element from the link-list
  117. * Or 0 if the list is empty
  118. */
  119. int LLShift(ll list)
  120. {
  121.     // TODO: replace this with your code
  122.     // make the compiler happy
  123.     //(void)list;
  124.     if (list == NULL || list->head == NULL) //空链表直接返回
  125.         return 0;
  126.     else if (list->head->next == NULL)      //链表只有头节点,直接删除头节点然后返回
  127.     {
  128.         free(list->head);
  129.         list->head = NULL;
  130.         return 0;
  131.     }

  132.     linkedlistnode* head = list->head->next;    //记录链表头节点的下个节点(链表的第2个节点)
  133.     free(list->head);                           //删除头节点
  134.     list->head = head;                          //链表头变为之前的第2个节点

  135.     return 1;
  136. }

  137. void ShowList(ll list)      //遍历输出链表内容,测试用....
  138. {
  139.     linkedlistnode* cur = list->head;           //
  140.     while (cur != NULL)
  141.     {
  142.         printf_s("%d ", cur->data);
  143.         cur = cur->next;
  144.     }
  145.     printf_s("\n");
  146. }

  147. int main()
  148. {
  149.     ll list = LLCreate();
  150.     LLAppend(list, 1);
  151.     ShowList(list);
  152.     LLAppend(list, 2);
  153.     ShowList(list);
  154.     LLAppend(list, 3);
  155.     ShowList(list);
  156.     LLAppend(list, 4);
  157.     ShowList(list);
  158.     LLAppend(list, 5);
  159.     ShowList(list);

  160.     LLPrepend(list, 1);
  161.     ShowList(list);
  162.     LLPrepend(list, 2);
  163.     ShowList(list);
  164.     LLPrepend(list, 3);
  165.     ShowList(list);
  166.     LLPrepend(list, 4);
  167.     ShowList(list);
  168.     LLPrepend(list, 5);
  169.     ShowList(list);

  170.     LLShrink(list);
  171.     ShowList(list);
  172.     LLShrink(list);
  173.     ShowList(list);
  174.     LLShrink(list);
  175.     ShowList(list);

  176.     LLShift(list);
  177.     ShowList(list);
  178.     LLShift(list);
  179.     ShowList(list);
  180.     LLShift(list);
  181.     ShowList(list);

  182.     return 0;
  183. }
复制代码


QQ图片20210621173144.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-27 15:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表