鱼C论坛

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

指针逆序

[复制链接]
发表于 2022-11-23 19:33:58 | 显示全部楼层 |阅读模式

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

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

x
[img]%5Burl=https://imgse.com/i/z8I8XV%5D

                               
登录/注册后可看大图
[/url][/img]
  1. typedef int datatype;
  2. typedef struct node
  3.         {
  4.                 datatype data;
  5.                 struct node *next;
  6. }linklist;

  7. INVERT(linklist *head)
  8. {
  9.         linklist *p,*q;
  10.         p=head->next;
  11.         if(p!=NULL)
  12.         {
  13.                 head->next=NULL;
  14.                 do
  15.                 {
  16.                         q=p->next;
  17.                         p->next=head->next;
  18.                         head->next=p;
  19.                         p=q;
  20.                 }
  21.                 while(p!=NULL);
  22.         }
  23. }
复制代码

这个逆序怎么实现的啊,没咋看懂
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-11-23 19:49:19 | 显示全部楼层
本帖最后由 jackz007 于 2022-11-23 19:55 编辑
  1. void INVERT(linklist ** head)                // head 头结点指针肯定会被修改,所以,这里应该传入二级指针
  2. {
  3.         linklist * p , * q , * next ;
  4.         for(p = * head , q = NULL ; p ;) {
  5.                 next = p -> next             // 保存下一个节点信息,准备改变 p -> next 指针,
  6.                 p -> next = q       ;        // 让 p -> next 指向上一个遍历的节点,如果是原来的头结点,那么,q = NULL,成为新的尾节点
  7.                 q = p               ;        // 让 p 成为前一个节点,准备进入下一个节点
  8.                 p = next            ;        // 进入下一个节点,如果是原来的尾节点,那么,p = NULL,q 就是新的头结点
  9.         }
  10.         * head = q                  ;        // 循环结束后,q 自然就是新的头结点,必须把新的头结点指针返回主程序
  11. }
复制代码

        完整程序请参考此帖:https://fishc.com.cn/forum.php?m ... 095&pid=6058652
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-24 19:32:29 | 显示全部楼层
小甲鱼老师的课后作业答案
这是代码
  1. struct Node *reversed(struct Node *head)
  2. {
  3.         struct Node *pre;
  4.         struct Node *cur;
  5.    
  6.         pre = NULL;
  7.         cur = head;
  8.         while(cur)
  9.         {
  10.                 struct Node *next = cur->next;
  11.                 cur->next = pre;
  12.                 pre = cur;
  13.                 cur = next;
  14.         }
  15.    
  16.         return pre;
  17. }
复制代码

链接:
https://fishc.com.cn/forum.php?m ... peid%26typeid%3D570
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-24 21:04:04 | 显示全部楼层
最简单的方法,对链表逆序遍历,然后把每一个元素都添加到另一个新链表里面
这很简单,但是效率不高

  1. sh-5.1$ ls
  2. list.c        list.h        main.c
  3. sh-5.1$ cat main.c
  4. #include "list.h"
  5. #include <stdio.h>
  6. #include <stdint.h>

  7. void output(const list_t *list) {
  8.     for(size_t i = 0; i < list_size(list); ++i) {
  9.         size_t temp; list_get(list, i, &temp, sizeof(temp));
  10.         printf("%zu ", temp);
  11.     }
  12.     puts("");
  13. }

  14. list_t *list_reverse(const list_t *list, size_t elem_size) {
  15.     list_t *result = list_init();
  16.     for(size_t i = list_size(list) - 1; i != -1; --i) {
  17.         uint8_t buff[elem_size];
  18.         list_get(list, i, buff, elem_size);
  19.         list_append(result, buff, elem_size);
  20.     }
  21.     return result;
  22. }

  23. int main(void) {
  24.     list_t *list = list_init();
  25.     for(size_t i = 1; i <= 10; ++i) {
  26.         list_append(list, &i, sizeof(i));
  27.     }
  28.     list_t *lr = list_reverse(list, sizeof(size_t));
  29.     output(list);
  30.     output(lr);
  31.     list_deinit(lr);
  32.     list_deinit(list);
  33.     return 0;
  34. }
  35. sh-5.1$ cat list.h
  36. #ifndef _LIST_H_
  37. #define _LIST_H_

  38. #include <stddef.h>
  39. #include <stdbool.h>

  40. struct list_node_tag {
  41.     void *data; size_t size;
  42.     struct list_node_tag *next;
  43. };

  44. typedef struct {
  45.     struct list_node_tag *head;
  46.     size_t size;
  47. } list_t;

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

  58. #endif
  59. sh-5.1$ cat list.c
  60. #include "list.h"
  61. #include <stdlib.h>
  62. #include <memory.h>

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

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

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

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

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

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

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

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

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

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

使用道具 举报

发表于 2022-11-25 12:07:50 | 显示全部楼层
这次用递归的方式逆序,效率也不高
其实我是想把逆序链表的函数写到list.c里面的,这样可以直接操作底层结构,效率更高,但是最终还是没有这样做,因为这违背了我写这个代码的初衷,我写这个代码的目的并不是为了效率
因为做了这样的尝试,所以list.c内部的逻辑也发生了有点大的变化,不过对外没有改变,外面是看不到这个变化的,这就是定义良好的接口带来的好处

  1. sh-5.1$ ls
  2. list.c        list.h        main.c
  3. sh-5.1$ 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("%zu ", temp);
  10.     output(list, index + 1);
  11. }

  12. list_t *list_reverse(const list_t *list, size_t index) {
  13.     if(index >= list_size(list)) return list_init();
  14.     list_t *result = list_reverse(list, index + 1);
  15.     size_t temp; list_get(list, index, &temp, sizeof(temp));
  16.     list_append(result, &temp, sizeof(temp));
  17.     return result;
  18. }

  19. int main(void) {
  20.     list_t *list = list_init();
  21.     for(size_t i = 0; i < 10; ++i) {
  22.         list_append(list, &i, sizeof(i));
  23.     }
  24.     list_t *lr = list_reverse(list, 0);
  25.     output(list, 0); puts("");
  26.     output(lr, 0); puts("");
  27.     list_deinit(lr);
  28.     list_deinit(list);
  29.     return 0;
  30. }
  31. sh-5.1$ cat list.h
  32. #ifndef _LIST_H_
  33. #define _LIST_H_

  34. #include <stddef.h>
  35. #include <stdbool.h>

  36. struct list_node_tag {
  37.     void *data; size_t size;
  38.     struct list_node_tag *next;
  39. };

  40. typedef struct {
  41.     struct list_node_tag *head;
  42.     size_t size;
  43. } list_t;

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

  54. #endif
  55. sh-5.1$ cat list.c
  56. #include "list.h"
  57. #include <stdlib.h>
  58. #include <string.h>

  59. static struct list_node_tag *new_node(const void *data, size_t size) {
  60.     struct list_node_tag *result = malloc(sizeof(*result));
  61.     if(!result) return NULL;
  62.     result->data = malloc(size);
  63.     if(!result->data) {free(result); return NULL;}
  64.     memcpy(result->data, data, size);
  65.     result->size = size;
  66.     result->next = NULL;
  67.     return result;
  68. }

  69. static void free_node(struct list_node_tag *n) {
  70.     free(n->data);
  71.     free(n);
  72. }

  73. static struct list_node_tag *const *locate_node_const(const list_t *list, size_t index) {
  74.     struct list_node_tag *const *result = &list->head;
  75.     while(index--) result = &(*result)->next;
  76.     return result;
  77. }

  78. static struct list_node_tag **locate_node(list_t *list, size_t index) {
  79.     return (struct list_node_tag **)locate_node_const(list, index);
  80. }

  81. list_t *list_init(void) {
  82.     list_t *list = malloc(sizeof(*list));
  83.     if(!list) return NULL;
  84.     list->head = NULL;
  85.     list->size = 0;
  86.     return list;
  87. }

  88. void list_deinit(list_t *list) {
  89.     if(!list) return;
  90.     list_clean(list);
  91.     free(list);
  92. }

  93. bool list_clean(list_t *list) {
  94.     if(!list) return false;
  95.     while(!list_empty(list)) if(!list_delete(list, 0)) return false;
  96.     return true;
  97. }

  98. bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
  99.     if(!list) return false;
  100.     if(list_size(list) < index) return false;
  101.     if(!data) return false;
  102.     struct list_node_tag **current = locate_node(list, index);
  103.     struct list_node_tag *node = new_node(data, size);
  104.     if(!node) return false;
  105.     node->next = *current;
  106.     *current = node;
  107.     ++list->size;
  108.     return true;
  109. }

  110. bool list_delete(list_t *list, size_t index) {
  111.     if(!list) return false;
  112.     if(list_size(list) <= index) return false;
  113.     struct list_node_tag **current = locate_node(list, index);
  114.     struct list_node_tag *temp = *current;
  115.     *current = temp->next;
  116.     free_node(temp);
  117.     --list->size;
  118.     return true;
  119. }

  120. bool list_get(const list_t *list, size_t index, void *data, size_t size) {
  121.     if(!list) return false;
  122.     if(list_size(list) <= index) return false;
  123.     if(!data) return false;
  124.     struct list_node_tag *current = *locate_node_const(list, index);
  125.     if(current->size > size) return false;
  126.     memcpy(data, current->data, current->size);
  127.     return true;
  128. }

  129. bool list_set(list_t *list, size_t index, const void *data, size_t size) {
  130.     bool res = list_delete(list, index);
  131.     return res ? list_insert(list, index, data, size) : res;
  132. }

  133. bool list_append(list_t *list, const void *data, size_t size) {
  134.     if(!list) return false;
  135.     return list_insert(list, list_size(list), data, size);
  136. }

  137. size_t list_size(const list_t *list) {
  138.     if(!list) return 0;
  139.     return list->size;
  140. }

  141. bool list_empty(const list_t *list) {
  142.     if(!list) return true;
  143.     return list_size(list) == 0;
  144. }
  145. sh-5.1$ gcc -g -Wall -o main main.c list.c
  146. sh-5.1$ ./main
  147. 0 1 2 3 4 5 6 7 8 9
  148. 9 8 7 6 5 4 3 2 1 0
  149. sh-5.1$
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 04:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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