鱼C论坛

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

[已解决]我用malloc申请空间,编译不过去

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

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

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

x
/写出将带头结点的线性单链表L就地逆置的算法
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define LEN sizeof(struct student)
  
  struct student{
        int num;
        struct student *next;
}*L;

struct student *L()
{
int *L=NULL;
L =(int *)malloc(sizeof(int)*LEN);
if(NULL!=L)
exit(1);
else { L->next  =LEN;
L->num =0};
};
最佳答案
2021-3-15 18:04:09
本帖最后由 jackz007 于 2021-3-15 18:07 编辑
  1. #include <stdio.h>
  2. #include <stdlib.h>

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

  7. struct student * foo(struct student * head , int d)
  8. {
  9.         struct student * p1 , * p2                                        ;
  10.         if((p1 = (struct student *) malloc(sizeof(struct student)))) {
  11.                 p1 -> next = NULL                                         ;
  12.                 p1 -> num = d                                             ;
  13.                 for(p2 = head ; p2 && p2 -> next ; p2 = p2 -> next)       ;
  14.                 if(p2) p2 -> next = p1                                    ;
  15.                 else head = p1                                            ;
  16.         } else {
  17.                 fprintf(stderr , "\n")                                    ;
  18.                 fprintf(stderr , "Error : malloc()\n")                    ;
  19.                 fprintf(stderr , "\n")                                    ;
  20.         }
  21.         return head                                                       ;
  22. }

  23. int main(void)
  24. {
  25.         struct student * L , * P                                          ;
  26.         int i , m                                                         ;
  27.         for(L = NULL , i = 0 ; i < 10 ; i ++) L = foo(L , 100 + i)        ;
  28.         printf("%d" , L -> num)                                           ;
  29.         for(P = L -> next ; P ; P = P -> next) printf(" , %d" , P -> num) ;
  30.         printf("\n")                                                      ;
  31.         for(P = L ; P ; L = P) {
  32.                 P = L -> next                                             ;
  33.                 free(L)                                                   ;
  34.         }   
  35. }
复制代码

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

  2. D:\0002.Exercise\C>x
  3. 100 , 101 , 102 , 103 , 104 , 105 , 106 , 107 , 108 , 109

  4. D:\0002.Exercise\C>
复制代码

        
        楼主似乎格外偏爱 L,定义个结构名叫 L,定义个函数,名字还叫 L,这是为什么呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-15 18:04:09 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2021-3-15 18:07 编辑
  1. #include <stdio.h>
  2. #include <stdlib.h>

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

  7. struct student * foo(struct student * head , int d)
  8. {
  9.         struct student * p1 , * p2                                        ;
  10.         if((p1 = (struct student *) malloc(sizeof(struct student)))) {
  11.                 p1 -> next = NULL                                         ;
  12.                 p1 -> num = d                                             ;
  13.                 for(p2 = head ; p2 && p2 -> next ; p2 = p2 -> next)       ;
  14.                 if(p2) p2 -> next = p1                                    ;
  15.                 else head = p1                                            ;
  16.         } else {
  17.                 fprintf(stderr , "\n")                                    ;
  18.                 fprintf(stderr , "Error : malloc()\n")                    ;
  19.                 fprintf(stderr , "\n")                                    ;
  20.         }
  21.         return head                                                       ;
  22. }

  23. int main(void)
  24. {
  25.         struct student * L , * P                                          ;
  26.         int i , m                                                         ;
  27.         for(L = NULL , i = 0 ; i < 10 ; i ++) L = foo(L , 100 + i)        ;
  28.         printf("%d" , L -> num)                                           ;
  29.         for(P = L -> next ; P ; P = P -> next) printf(" , %d" , P -> num) ;
  30.         printf("\n")                                                      ;
  31.         for(P = L ; P ; L = P) {
  32.                 P = L -> next                                             ;
  33.                 free(L)                                                   ;
  34.         }   
  35. }
复制代码

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

  2. D:\0002.Exercise\C>x
  3. 100 , 101 , 102 , 103 , 104 , 105 , 106 , 107 , 108 , 109

  4. D:\0002.Exercise\C>
复制代码

        
        楼主似乎格外偏爱 L,定义个结构名叫 L,定义个函数,名字还叫 L,这是为什么呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-26 00:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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