鱼C论坛

 找回密码
 立即注册
查看: 1364|回复: 5

[已解决]我用的是c语言,哪位大神帮忙调试一下

[复制链接]
发表于 2021-3-16 18:48:05 | 显示全部楼层 |阅读模式

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

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

x
//写出将带头结点的线性单链表L就地逆置的算法
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define LEN sizeof(struct student)// student结构的大小
  struct student *L();//创建链表
  void nizhi(struct student *L);//逆置链表
  void print(struct student *L);//打印链表
  struct student{
        int num;
        struct student *next;
};
int n;//全局变量

struct student *L()
{
        struct student *head,*p1,*p2;
p1=p2 = (struct student*)malloc(LEN);
        if (NULL == p1)
                return NULL;
        else {
                p1->next = NULL;
                p1->num = 0;
        }
        printf("请输入数字:\n");
        scanf("%d",&p1->num );
        int n=0;
        head=NULL;

        while(p1->num )//只要输入的数字不是0,链表会一直创建下去
        {
                n++;
                if(n==1)
                {
                        head->next =p1;
                }
                else
                {
                        p2->next =p1;
                       
                }
                p2=p1;
                p1=(struct student *)malloc(LEN);
        printf("请输入数字:\n");
        scanf("%d",&p1->num );
        }
        p2->next =NULL;}
       
void nizhi(struct student *L)//构造逆置算法
{
        struct student *p,*p1,*p2;
        p1=head->next;
        p2=p1->next ;
        p1->next=p ;
while(p2){
        p=p1;
        p1=p2;
        p2=p2->next ;
        p1->next =p;
         }
         p2->next =p1;
         head->next =p2;
return(L);
}
void print(struct student *L) //构造打印链表算法
{
        while(head)
        {
                printf("%d\t",head->num );
                head=head->next ;
               
        }
        return (L);
       
}
void main()//主函数
{
        struct student *L();
        void nizhi(struct student *L);
         void print(struct student *L);
         return 0;
}
最佳答案
2021-3-17 00:28:46
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct student{
  4.         int num                                                                          ;
  5.         struct student * next                                                            ;
  6. }                                                                                        ;

  7. int n                                                                                    ;

  8. void print(struct student * head)
  9. {
  10.         printf("\n")                                                                     ;
  11.         if(head) {
  12.                 printf("%d" , head -> num )                                              ;
  13.                 head = head -> next                                                      ;
  14.                 for(; head ; head = head -> next) printf("\t%d" , head -> num )          ;
  15.                 printf("\n")                                                             ;
  16.         }
  17. }

  18. struct student * create()
  19. {
  20.         struct student * head , * p1 , * p2                                               ;
  21.         int num                                                                           ;
  22.         printf("\n")                                                                      ;
  23.         for(n = 0 , p1 = p2 = head = NULL ; ; p2 = p1) {
  24.                 printf("请输入数字 : ") ;
  25.                 scanf("%d",& num )      ;
  26.                 if(num > 0) {
  27.                         if((p1 = (struct student *) malloc(sizeof(struct student)))) {
  28.                                 p1 -> num = num                                           ;
  29.                                 p1 -> next = NULL                                         ;
  30.                                 if(head) p2 -> next = p1                                  ;
  31.                                 else head = p1                                            ;
  32.                                 n ++                                                      ;
  33.                         } else {
  34.                                 for(p1 = head ; p1 ; p1 = p2) {
  35.                                         p2 = p1 -> next                                   ;
  36.                                         free(p1)                                          ;
  37.                                 }
  38.                                 fprintf(stderr , "\n")                                    ;
  39.                                 fprintf(stderr , "Error : malloc()\n")                    ;
  40.                                 fprintf(stderr , "\n")                                    ;
  41.                                 break                                                     ;
  42.                         }
  43.                 } else {
  44.                         break                                                             ;
  45.                 }
  46.         }
  47.         return head                                                                       ;
  48. }        

  49. struct student * nizhi(struct student * head)
  50. {
  51.         struct student * p1 , * p2 , * p3                                                 ;
  52.         for(p1 = head , p2 = NULL ; p1 ; p1 = p3) {
  53.                 p3 = p1 -> next                                                           ;
  54.                 if(p1 == head) p1 -> next = NULL                                          ;
  55.                 else p1 -> next = p2                                                      ;
  56.                 if(! p3) head = p1                                                        ;
  57.                 p2 = p1                                                                   ;
  58.         }
  59.         return head                                                                       ;
  60. }

  61. int main(void)
  62. {
  63.         struct student * h = NULL , * p1 , * p2                                          ;
  64.         h = create()                                                                     ;
  65.         print(h)                                                                         ;
  66.         h = nizhi(h)                                                                     ;
  67.         print(h)                                                                         ;
  68.         for(p1 = h ; p1 ; p1 = p2) {
  69.                 p2 = p1 -> next                                                          ;
  70.                 free(p1)                                                                 ;
  71.         }                 
  72. }
复制代码

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

  2. D:\00.Excise\C>x

  3. 请输入数字 : 100
  4. 请输入数字 : 101
  5. 请输入数字 : 102
  6. 请输入数字 : 103
  7. 请输入数字 : 104
  8. 请输入数字 : 105
  9. 请输入数字 : 106
  10. 请输入数字 : 107
  11. 请输入数字 : 108
  12. 请输入数字 : 109
  13. 请输入数字 : 0

  14. 100     101     102     103     104     105     106     107     108     109

  15. 109     108     107     106     105     104     103     102     101     100

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

使用道具 举报

发表于 2021-3-16 19:33:45 | 显示全部楼层
本帖最后由 jackz007 于 2021-3-16 19:36 编辑

       楼主有数十个求助,别人辛辛苦苦帮了你,无论问题是否得到解决,你都不选 "最佳答案" 进行回应,像这样,没有人会愿意帮助你的。建议楼主先把前面的求助做个了结。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-16 19:39:11 | 显示全部楼层
而且有好多不应该犯的错误,回炉重造吧

写着 void main,然后 return 0;
写着 void print,然后 return (L);

顺便再说一下,void main 吗?你看的哪本教材?换一个新的吧,void main 的教材太老了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-16 20:19:06 From FishC Mobile | 显示全部楼层
jackz007 发表于 2021-3-16 19:33
楼主有数十个求助,别人辛辛苦苦帮了你,无论问题是否得到解决,你都不选 "最佳答案" 进行回应,像 ...

已解决,请回答我的问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-17 00:28:46 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct student{
  4.         int num                                                                          ;
  5.         struct student * next                                                            ;
  6. }                                                                                        ;

  7. int n                                                                                    ;

  8. void print(struct student * head)
  9. {
  10.         printf("\n")                                                                     ;
  11.         if(head) {
  12.                 printf("%d" , head -> num )                                              ;
  13.                 head = head -> next                                                      ;
  14.                 for(; head ; head = head -> next) printf("\t%d" , head -> num )          ;
  15.                 printf("\n")                                                             ;
  16.         }
  17. }

  18. struct student * create()
  19. {
  20.         struct student * head , * p1 , * p2                                               ;
  21.         int num                                                                           ;
  22.         printf("\n")                                                                      ;
  23.         for(n = 0 , p1 = p2 = head = NULL ; ; p2 = p1) {
  24.                 printf("请输入数字 : ") ;
  25.                 scanf("%d",& num )      ;
  26.                 if(num > 0) {
  27.                         if((p1 = (struct student *) malloc(sizeof(struct student)))) {
  28.                                 p1 -> num = num                                           ;
  29.                                 p1 -> next = NULL                                         ;
  30.                                 if(head) p2 -> next = p1                                  ;
  31.                                 else head = p1                                            ;
  32.                                 n ++                                                      ;
  33.                         } else {
  34.                                 for(p1 = head ; p1 ; p1 = p2) {
  35.                                         p2 = p1 -> next                                   ;
  36.                                         free(p1)                                          ;
  37.                                 }
  38.                                 fprintf(stderr , "\n")                                    ;
  39.                                 fprintf(stderr , "Error : malloc()\n")                    ;
  40.                                 fprintf(stderr , "\n")                                    ;
  41.                                 break                                                     ;
  42.                         }
  43.                 } else {
  44.                         break                                                             ;
  45.                 }
  46.         }
  47.         return head                                                                       ;
  48. }        

  49. struct student * nizhi(struct student * head)
  50. {
  51.         struct student * p1 , * p2 , * p3                                                 ;
  52.         for(p1 = head , p2 = NULL ; p1 ; p1 = p3) {
  53.                 p3 = p1 -> next                                                           ;
  54.                 if(p1 == head) p1 -> next = NULL                                          ;
  55.                 else p1 -> next = p2                                                      ;
  56.                 if(! p3) head = p1                                                        ;
  57.                 p2 = p1                                                                   ;
  58.         }
  59.         return head                                                                       ;
  60. }

  61. int main(void)
  62. {
  63.         struct student * h = NULL , * p1 , * p2                                          ;
  64.         h = create()                                                                     ;
  65.         print(h)                                                                         ;
  66.         h = nizhi(h)                                                                     ;
  67.         print(h)                                                                         ;
  68.         for(p1 = h ; p1 ; p1 = p2) {
  69.                 p2 = p1 -> next                                                          ;
  70.                 free(p1)                                                                 ;
  71.         }                 
  72. }
复制代码

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

  2. D:\00.Excise\C>x

  3. 请输入数字 : 100
  4. 请输入数字 : 101
  5. 请输入数字 : 102
  6. 请输入数字 : 103
  7. 请输入数字 : 104
  8. 请输入数字 : 105
  9. 请输入数字 : 106
  10. 请输入数字 : 107
  11. 请输入数字 : 108
  12. 请输入数字 : 109
  13. 请输入数字 : 0

  14. 100     101     102     103     104     105     106     107     108     109

  15. 109     108     107     106     105     104     103     102     101     100

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

使用道具 举报

 楼主| 发表于 2021-3-17 08:15:52 | 显示全部楼层

有两下子,加个好友吧,qq:1452273922
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-4 16:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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