C语言单向链表的增删操作

打印 上一主题 下一主题

主题 871|帖子 871|积分 2623

  1. // 指的是单向链表中的结点有效数据类型,用户可以根据需要进行修改
  2. typedef int DataType_t;
  3. // 构造链表的结点,链表中所有结点的数据类型应该是相同的
  4. typedef struct LinkedList
  5. {
  6.     DataType_t data;         // 结点的数据域
  7.     struct LinkedList *next; // 结点的指针域
  8. } LList_t;
  9. // 创建一个空链表,空链表应该有一个头结点,对链表进行初始化
  10. LList_t *LList_Create(void)
  11. {
  12.     // 1.创建一个头结点并对头结点申请内存
  13.     LList_t *Head = (LList_t *)calloc(1, sizeof(LList_t));
  14.     if (NULL == Head)
  15.     {
  16.         perror("Calloc memory for Head is Failed");
  17.         exit(-1);
  18.     }
  19.     // 2.对头结点进行初始化,头结点是不存储有效内容的!!!
  20.     Head->next = NULL;
  21.     // 3.把头结点的地址返回即可
  22.     return Head;
  23. }
  24. // 创建新的结点,并对新结点进行初始化(数据域 + 指针域)
  25. LList_t *LList_NewNode(DataType_t data)
  26. {
  27.     // 1.创建一个新结点并对新结点申请内存
  28.     LList_t *New = (LList_t *)calloc(1, sizeof(LList_t));
  29.     if (NULL == New)
  30.     {
  31.         perror("Calloc memory for NewNode is Failed");
  32.         return NULL;
  33.     }
  34.     // 2.对新结点的数据域和指针域进行初始化
  35.     New->data = data;
  36.     New->next = NULL;
  37.     return New;
  38. }
  39. // 头插
  40. bool LList_HeadInsert(LList_t *Head, DataType_t data)
  41. {
  42.     // 1.创建新的结点,并对新结点进行初始化
  43.     LList_t *New = LList_NewNode(data);
  44.     if (NULL == New)
  45.     {
  46.         printf("can not insert new node\n");
  47.         return false;
  48.     }
  49.     // 2.判断链表是否为空,如果为空,则直接插入即可
  50.     if (NULL == Head->next)
  51.     {
  52.         Head->next = New;
  53.         return true;
  54.     }
  55.     // 3.如果链表为非空,则把新结点插入到链表的头部
  56.     New->next = Head->next;
  57.     Head->next = New;
  58.     return true;
  59. }
  60. // 尾插
  61. bool LList_TailInsert(LList_t *Head, DataType_t data)
  62. {
  63.     // 1.创建新的结点,并对新结点进行初始化
  64.     LList_t *New = LList_NewNode(data);
  65.     if (NULL == New)
  66.     {
  67.         printf("can not insert new node\n");
  68.         return false;
  69.     }
  70.     // 2.判断链表是否为空,如果为空,则直接插入即可
  71.     if (NULL == Head->next)
  72.     {
  73.         Head->next = New;
  74.         return true;
  75.     }
  76.     // 3.如果链表为非空,则把新结点插入到链表的尾部
  77.     LList_t *Last = Head;
  78.     while (Last->next != NULL)
  79.     {
  80.         Last = Last->next;
  81.     }
  82.     New->next = NULL;
  83.     Last->next = New;
  84.     return true;
  85. }
  86. // 指定插
  87. bool LList_DestInsert(LList_t *Head, DataType_t dest, DataType_t data)
  88. {
  89.     // 1.创建新的结点,并对新结点进行初始化
  90.     LList_t *New = LList_NewNode(data);
  91.     if (NULL == New)
  92.     {
  93.         printf("can not insert new node\n");
  94.         return false;
  95.     }
  96.     // 2.判断链表是否为空,如果为空,则直接插入即可
  97.     if (NULL == Head->next)
  98.     {
  99.         Head->next = New;
  100.         return true;
  101.     }
  102.     // 3.如果链表为非空,则把新结点插入到链表的指定位置
  103.     LList_t *Dest = Head;
  104.     while (Dest->data != dest)
  105.     {
  106.         Dest = Dest->next;
  107.     }
  108.     New->next = Dest->next;
  109.     Dest->next = New;
  110.     return true;
  111. }
  112. // 头删
  113. bool LList_HeadDel(LList_t *Head, DataType_t data)
  114. {
  115.     LList_t *Temp = Head->next;
  116.     Head->next = Temp->next;
  117.     free(Temp);
  118.     return true;
  119. }
  120. // 尾删
  121. bool LList_TailDel(LList_t *Head, DataType_t data)
  122. {
  123.     LList_t *SecondLast = Head;
  124.     while (SecondLast->next->next != NULL)
  125.     {
  126.         SecondLast = SecondLast->next;
  127.     }
  128.     free(SecondLast->next);
  129.     SecondLast->next = NULL;
  130.     return true;
  131. }
  132. // 指定删
  133. bool LList_DestDel(LList_t *Head, DataType_t dest, DataType_t data)
  134. {
  135.     LList_t *LDest = Head;
  136.     while ((LDest->next)->data != dest)
  137.     {
  138.         LDest = LDest->next;
  139.     }
  140.     LDest->next = LDest->next->next;
  141.     free(LDest->next);
  142.     return true;
  143. }
  144. // 设计一个算法删除单链表L(有头结点)中的一个最小值结点
  145. bool LList_MinDel(LList_t *Head, DataType_t data)
  146. {
  147.     LList_t *LDest = Head;
  148.     LList_t *LMin = LDest;
  149.     while (LDest->next != NULL)
  150.     {
  151.         if ((LDest->next)->data < (LMin->next)->data)
  152.         {
  153.             LMin->next = LDest->next;
  154.             LMin = LDest;
  155.         }
  156.         LDest = LDest->next;
  157.         LDest->next = LDest->next->next;
  158.     }
  159.     LMin->next = LMin->next->next;
  160.     free(LMin->next);
  161.     return true;
  162. }
  163. // 遍历
  164. void LList_Print(LList_t *Head)
  165. {
  166.     // 对链表的头文件的地址进行备份
  167.     LList_t *Phead = Head;
  168.     // 首结点
  169.     while (Phead->next)
  170.     {
  171.         // 把头的直接后继作为新的头结点
  172.         Phead = Phead->next;
  173.         // 输出头结点的直接后继的数据域
  174.         printf("data = %d\n", Phead->data);
  175.     }
  176. }
  177. int main(int argc, char const *argv[])
  178. {
  179.     return 0;
  180. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

篮之新喜

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表