鱼C论坛

 找回密码
 立即注册
查看: 1121|回复: 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
#include <stdio.h>
#include <stdlib.h>

struct student{
        int num                                                                          ;
        struct student * next                                                            ;
}                                                                                        ;

int n                                                                                    ;

void print(struct student * head)
{
        printf("\n")                                                                     ;
        if(head) {
                printf("%d" , head -> num )                                              ;
                head = head -> next                                                      ;
                for(; head ; head = head -> next) printf("\t%d" , head -> num )          ;
                printf("\n")                                                             ; 
        }
}

struct student * create()
{
        struct student * head , * p1 , * p2                                               ;
        int num                                                                           ;
        printf("\n")                                                                      ;
        for(n = 0 , p1 = p2 = head = NULL ; ; p2 = p1) {
                printf("请输入数字 : ") ;
                scanf("%d",& num )      ;
                if(num > 0) {
                        if((p1 = (struct student *) malloc(sizeof(struct student)))) {
                                p1 -> num = num                                           ;
                                p1 -> next = NULL                                         ;
                                if(head) p2 -> next = p1                                  ;
                                else head = p1                                            ;
                                n ++                                                      ;
                        } else {
                                for(p1 = head ; p1 ; p1 = p2) {
                                        p2 = p1 -> next                                   ;
                                        free(p1)                                          ; 
                                }
                                fprintf(stderr , "\n")                                    ;
                                fprintf(stderr , "Error : malloc()\n")                    ;
                                fprintf(stderr , "\n")                                    ;
                                break                                                     ;
                        }
                } else {
                        break                                                             ;
                }
        }
        return head                                                                       ;
}        

struct student * nizhi(struct student * head)
{
        struct student * p1 , * p2 , * p3                                                 ;
        for(p1 = head , p2 = NULL ; p1 ; p1 = p3) {
                p3 = p1 -> next                                                           ;
                if(p1 == head) p1 -> next = NULL                                          ;
                else p1 -> next = p2                                                      ;
                if(! p3) head = p1                                                        ;
                p2 = p1                                                                   ;
        }
        return head                                                                       ;
}

int main(void)
{
        struct student * h = NULL , * p1 , * p2                                          ;
        h = create()                                                                     ;
        print(h)                                                                         ;
        h = nizhi(h)                                                                     ;
        print(h)                                                                         ;
        for(p1 = h ; p1 ; p1 = p2) {
                p2 = p1 -> next                                                          ;
                free(p1)                                                                 ;
        }                 
}
        编译、运行实况
D:\00.Excise\C>g++ -o x x.c

D:\00.Excise\C>x

请输入数字 : 100
请输入数字 : 101
请输入数字 : 102
请输入数字 : 103
请输入数字 : 104
请输入数字 : 105
请输入数字 : 106
请输入数字 : 107
请输入数字 : 108
请输入数字 : 109
请输入数字 : 0

100     101     102     103     104     105     106     107     108     109

109     108     107     106     105     104     103     102     101     100

D:\00.Excise\C>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

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

使用道具 举报

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

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

顺便再说一下,void main 吗?你看的哪本教材?换一个新的吧,void main 的教材太老了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

已解决,请回答我的问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

struct student{
        int num                                                                          ;
        struct student * next                                                            ;
}                                                                                        ;

int n                                                                                    ;

void print(struct student * head)
{
        printf("\n")                                                                     ;
        if(head) {
                printf("%d" , head -> num )                                              ;
                head = head -> next                                                      ;
                for(; head ; head = head -> next) printf("\t%d" , head -> num )          ;
                printf("\n")                                                             ; 
        }
}

struct student * create()
{
        struct student * head , * p1 , * p2                                               ;
        int num                                                                           ;
        printf("\n")                                                                      ;
        for(n = 0 , p1 = p2 = head = NULL ; ; p2 = p1) {
                printf("请输入数字 : ") ;
                scanf("%d",& num )      ;
                if(num > 0) {
                        if((p1 = (struct student *) malloc(sizeof(struct student)))) {
                                p1 -> num = num                                           ;
                                p1 -> next = NULL                                         ;
                                if(head) p2 -> next = p1                                  ;
                                else head = p1                                            ;
                                n ++                                                      ;
                        } else {
                                for(p1 = head ; p1 ; p1 = p2) {
                                        p2 = p1 -> next                                   ;
                                        free(p1)                                          ; 
                                }
                                fprintf(stderr , "\n")                                    ;
                                fprintf(stderr , "Error : malloc()\n")                    ;
                                fprintf(stderr , "\n")                                    ;
                                break                                                     ;
                        }
                } else {
                        break                                                             ;
                }
        }
        return head                                                                       ;
}        

struct student * nizhi(struct student * head)
{
        struct student * p1 , * p2 , * p3                                                 ;
        for(p1 = head , p2 = NULL ; p1 ; p1 = p3) {
                p3 = p1 -> next                                                           ;
                if(p1 == head) p1 -> next = NULL                                          ;
                else p1 -> next = p2                                                      ;
                if(! p3) head = p1                                                        ;
                p2 = p1                                                                   ;
        }
        return head                                                                       ;
}

int main(void)
{
        struct student * h = NULL , * p1 , * p2                                          ;
        h = create()                                                                     ;
        print(h)                                                                         ;
        h = nizhi(h)                                                                     ;
        print(h)                                                                         ;
        for(p1 = h ; p1 ; p1 = p2) {
                p2 = p1 -> next                                                          ;
                free(p1)                                                                 ;
        }                 
}
        编译、运行实况
D:\00.Excise\C>g++ -o x x.c

D:\00.Excise\C>x

请输入数字 : 100
请输入数字 : 101
请输入数字 : 102
请输入数字 : 103
请输入数字 : 104
请输入数字 : 105
请输入数字 : 106
请输入数字 : 107
请输入数字 : 108
请输入数字 : 109
请输入数字 : 0

100     101     102     103     104     105     106     107     108     109

109     108     107     106     105     104     103     102     101     100

D:\00.Excise\C>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

有两下子,加个好友吧,qq:1452273922
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-22 05:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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