鱼C论坛

 找回密码
 立即注册
查看: 1170|回复: 2

[已解决]链表问题

[复制链接]
发表于 2021-1-4 21:25:19 | 显示全部楼层 |阅读模式

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

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

x
输入一列整数,以0结束,把这些整数(不含0)顺序存放一单链表中,再把链表按照输入相反顺序处理并输出。

请问上面这道题该怎么写?
最佳答案
2021-1-4 22:32:30
本帖最后由 jackz007 于 2021-1-4 22:35 编辑
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct node {
  4.         int d                                      ;
  5.         struct node * next                         ;
  6. }                                                  ;

  7. // 反序链表所有节点
  8. struct node *  reverse(struct node * head)
  9. {
  10.         struct node * h , * p , * q , * r          ;
  11.         for(h = NULL , p = head ; p ; r = p , p = q) {
  12.                 q = p -> next                      ;
  13.                 if(p == head) p -> next = NULL     ;
  14.                 else p -> next = r                 ;
  15.                 if(! q) h = p                      ;
  16.         }
  17.         return h                                   ;
  18. }

  19. // 释放链表所有节点占用的内存
  20. void release(struct node * head)
  21. {
  22.         struct node * p , * q                      ;
  23.         for(p = head ; p ; p = q) {
  24.                 q = p -> next                      ;
  25.                 free(p)                            ;
  26.         }
  27. }

  28. int main(void)
  29. {
  30.        struct node * h , * p , * q                                                 ;
  31.        int d , f , i , n                                                           ;
  32.        for(f = 1 , h = p = q = NULL ; f ; q = p) {
  33.                scanf("%d" , & d)                                                   ;
  34.                if(d) {
  35.                        if((p = (struct node *)malloc(sizeof(struct node)))) {
  36.                                p -> d = d                                          ;
  37.                                p -> next = NULL                                    ;
  38.                                if(h) q -> next = p                                 ;
  39.                                else h = p                                          ;
  40.                        } else {
  41.                                fprintf(stderr , "\n")                              ;
  42.                                fprintf(stderr , "*** error in alocate memory .\n") ;
  43.                                fprintf(stderr , "\n")                              ;
  44.                                f = 0                                               ;
  45.                                break                                               ;
  46.                        }        
  47.                } else break                                                        ;
  48.        }
  49.        if(h && f) {
  50.                printf("%d" , h -> d)                                               ;
  51.                for(p = h -> next ; p ; p = p -> next) printf(" %d" , p -> d)       ;
  52.                printf("\n")                                                        ;
  53.                h = reverse(h)                                                      ;
  54.                printf("after reverse .\n")                                         ;
  55.                printf("%d" , h -> d)                                               ;
  56.                for(p = h -> next ; p ; p = p -> next) printf(" %d" , p -> d)       ;
  57.                printf("\n")                                                        ;
  58.                release(h)                                                          ;
  59.         }
  60. }
复制代码

        编译、运行实况
  1. D:\00.Excise\C>g++ -o x x.c

  2. D:\00.Excise\C>x
  3. 1 2 3 4 5 6 7 8 9 0
  4. 1 2 3 4 5 6 7 8 9
  5. after reverse .
  6. 9 8 7 6 5 4 3 2 1

  7. D:\00.Excise\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-4 22:32:30 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2021-1-4 22:35 编辑
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct node {
  4.         int d                                      ;
  5.         struct node * next                         ;
  6. }                                                  ;

  7. // 反序链表所有节点
  8. struct node *  reverse(struct node * head)
  9. {
  10.         struct node * h , * p , * q , * r          ;
  11.         for(h = NULL , p = head ; p ; r = p , p = q) {
  12.                 q = p -> next                      ;
  13.                 if(p == head) p -> next = NULL     ;
  14.                 else p -> next = r                 ;
  15.                 if(! q) h = p                      ;
  16.         }
  17.         return h                                   ;
  18. }

  19. // 释放链表所有节点占用的内存
  20. void release(struct node * head)
  21. {
  22.         struct node * p , * q                      ;
  23.         for(p = head ; p ; p = q) {
  24.                 q = p -> next                      ;
  25.                 free(p)                            ;
  26.         }
  27. }

  28. int main(void)
  29. {
  30.        struct node * h , * p , * q                                                 ;
  31.        int d , f , i , n                                                           ;
  32.        for(f = 1 , h = p = q = NULL ; f ; q = p) {
  33.                scanf("%d" , & d)                                                   ;
  34.                if(d) {
  35.                        if((p = (struct node *)malloc(sizeof(struct node)))) {
  36.                                p -> d = d                                          ;
  37.                                p -> next = NULL                                    ;
  38.                                if(h) q -> next = p                                 ;
  39.                                else h = p                                          ;
  40.                        } else {
  41.                                fprintf(stderr , "\n")                              ;
  42.                                fprintf(stderr , "*** error in alocate memory .\n") ;
  43.                                fprintf(stderr , "\n")                              ;
  44.                                f = 0                                               ;
  45.                                break                                               ;
  46.                        }        
  47.                } else break                                                        ;
  48.        }
  49.        if(h && f) {
  50.                printf("%d" , h -> d)                                               ;
  51.                for(p = h -> next ; p ; p = p -> next) printf(" %d" , p -> d)       ;
  52.                printf("\n")                                                        ;
  53.                h = reverse(h)                                                      ;
  54.                printf("after reverse .\n")                                         ;
  55.                printf("%d" , h -> d)                                               ;
  56.                for(p = h -> next ; p ; p = p -> next) printf(" %d" , p -> d)       ;
  57.                printf("\n")                                                        ;
  58.                release(h)                                                          ;
  59.         }
  60. }
复制代码

        编译、运行实况
  1. D:\00.Excise\C>g++ -o x x.c

  2. D:\00.Excise\C>x
  3. 1 2 3 4 5 6 7 8 9 0
  4. 1 2 3 4 5 6 7 8 9
  5. after reverse .
  6. 9 8 7 6 5 4 3 2 1

  7. D:\00.Excise\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-4 22:41:01 | 显示全部楼层
jackz007 发表于 2021-1-4 22:32
编译、运行实况

谢谢!虽然暂时还看不懂,只是先应付下作业
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-3 05:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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