鱼C论坛

 找回密码
 立即注册
查看: 2010|回复: 1

[技术交流] 用C语言简单实现一个单向链表:

[复制链接]
发表于 2022-10-21 22:25:25 | 显示全部楼层 |阅读模式

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

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

x


编写一个程序将一个头结点指针为pa的单链表A分解成两个单链表A和B,其头结点指针分别为pa和pb,使得A链表中含有原链表A中序号为奇数的元素,而链表B中含有原链表A中序号为偶数的元素,且保持原来的相对顺序。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-10-21 23:03:11 | 显示全部楼层
题目什么意思?
意思是一开始有一个链表a
执行了一段代码后又生成了一个链表b
链表a里面是奇数,链表b里面是偶数
是这样吗?

没有完完全全按照题目要求来写代码
如果你是为了交作业的话,你可以参考这个代码写一个你自己的版本
如果你是为了学习的话,这个代码中有好多你需要学习的东西

  1. $ ls
  2. list.c  list.h  main.c
  3. $ cat main.c
  4. #include "list.h"
  5. #include <stdio.h>

  6. void output(const list_t *list, size_t index) {
  7.     if(index >= list_size(list)) return;
  8.     size_t temp; list_get(list, index, &temp, sizeof(temp));
  9.     printf("%zd ", temp);
  10.     output(list, index + 1);
  11. }

  12. int main(void) {
  13.     list_t *a = list_init();
  14.     list_t *b = list_init();
  15.     for(size_t i = 0; i < 10; ++i) {
  16.         list_append(a, &i, sizeof(i));
  17.     }
  18.     printf("a: "); output(a, 0); puts("");
  19.     // 这里要在同一个循环中删除同一个链表中的很多元素
  20.     // 那就只能是倒着遍历这个链表了
  21.     for(ssize_t i = list_size(a) - 1; i >= 0; --i) {
  22.         if(i % 2 != 0) continue;
  23.         size_t temp; list_get(a, i, &temp, sizeof(temp));
  24.         list_delete(a, i);
  25.         list_insert(b, 0, &temp, sizeof(temp));
  26.     }
  27.     printf("a: "); output(a, 0); puts("");
  28.     printf("b: "); output(b, 0); puts("");
  29.     list_deinit(b);
  30.     list_deinit(a);
  31.     return 0;
  32. }
  33. $ cat list.h
  34. #ifndef _LIST_H_
  35. #define _LIST_H_

  36. #include <stddef.h>
  37. #include <stdbool.h>

  38. struct list_node_tag {
  39.     void *data; size_t size;
  40.     struct list_node_tag *next;
  41. };

  42. typedef struct {
  43.     struct list_node_tag *head;
  44.     size_t size;
  45. } list_t;

  46. list_t *list_init(void);
  47. void list_deinit(list_t *list);
  48. bool list_clean(list_t *list);
  49. bool list_insert(list_t *list, size_t index, const void *data, size_t size);
  50. bool list_delete(list_t *list, size_t index);
  51. bool list_get(const list_t *list, size_t index, void *data, size_t size);
  52. bool list_set(list_t *list, size_t index, const void *data, size_t size);
  53. bool list_append(list_t *list, const void *data, size_t size);
  54. size_t list_size(const list_t *list);
  55. bool list_empty(const list_t *list);

  56. #endif
  57. $ cat list.c
  58. #include "list.h"
  59. #include <stdlib.h>
  60. #include <memory.h>

  61. list_t *list_init(void) {
  62.     list_t *list = malloc(sizeof(*list));
  63.     if(!list) return NULL;
  64.     list->head = NULL;
  65.     list->size = 0;
  66.     return list;
  67. }

  68. void list_deinit(list_t *list) {
  69.     if(!list) return;
  70.     list_clean(list);
  71.     free(list);
  72. }

  73. bool list_clean(list_t *list) {
  74.     if(!list) return false;
  75.     while(!list_empty(list)) list_delete(list, 0);
  76.     return true;
  77. }

  78. bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
  79.     if(!list) return false;
  80.     if(list_size(list) < index) return false;
  81.     if(!data) return false;
  82.     struct list_node_tag **current = &list->head;
  83.     while(index--) current = &(*current)->next;
  84.     struct list_node_tag *node = malloc(sizeof(*node));
  85.     if(!node) return false;
  86.     node->data = malloc(size);
  87.     if(!node->data) {free(node); return false;}
  88.     memcpy(node->data, data, size);
  89.     node->size = size;
  90.     node->next = *current;
  91.     *current = node;
  92.     ++list->size;
  93.     return true;
  94. }

  95. bool list_delete(list_t *list, size_t index) {
  96.     if(!list) return false;
  97.     if(list_size(list) <= index) return false;
  98.     struct list_node_tag **current = &list->head;
  99.     while(index--) current = &(*current)->next;
  100.     struct list_node_tag *temp = *current;
  101.     *current = temp->next;
  102.     free(temp->data); free(temp);
  103.     --list->size;
  104.     return true;
  105. }

  106. bool list_get(const list_t *list, size_t index, void *data, size_t size) {
  107.     if(!list) return false;
  108.     if(list_size(list) <= index) return false;
  109.     if(!data) return false;
  110.     struct list_node_tag *const *current = &list->head;
  111.     while(index--) current = &(*current)->next;
  112.     struct list_node_tag *temp = *current;
  113.     if(temp->size > size) return false;
  114.     memcpy(data, temp->data, temp->size);
  115.     return true;
  116. }

  117. bool list_set(list_t *list, size_t index, const void *data, size_t size) {
  118.     bool res = list_delete(list, index);
  119.     return res ? list_insert(list, index, data, size) : res;
  120. }

  121. bool list_append(list_t *list, const void *data, size_t size) {
  122.     if(!list) return false;
  123.     return list_insert(list, list_size(list), data, size);
  124. }

  125. size_t list_size(const list_t *list) {
  126.     if(!list) return 0;
  127.     return list->size;
  128. }

  129. bool list_empty(const list_t *list) {
  130.     if(!list) return true;
  131.     return list_size(list) == 0;
  132. }
  133. $ gcc -g -Wall -o main main.c list.c
  134. $ ls
  135. list.c  list.h  main  main.c
  136. $ ./main
  137. a: 0 1 2 3 4 5 6 7 8 9
  138. a: 1 3 5 7 9
  139. b: 0 2 4 6 8
  140. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-4 22:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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