| 
 | 
 
 
发表于 2021-3-15 18:04:09
|
显示全部楼层
   本楼为最佳答案    
 
 
 本帖最后由 jackz007 于 2021-3-15 18:07 编辑  
- #include <stdio.h>
 
 - #include <stdlib.h>
 
  
- struct student{
 
 -         int num                                                           ;
 
 -         struct student * next                                             ;
 
 - }                                                                         ;
 
  
- struct student * foo(struct student * head , int d)
 
 - {
 
 -         struct student * p1 , * p2                                        ;
 
 -         if((p1 = (struct student *) malloc(sizeof(struct student)))) {
 
 -                 p1 -> next = NULL                                         ;
 
 -                 p1 -> num = d                                             ;
 
 -                 for(p2 = head ; p2 && p2 -> next ; p2 = p2 -> next)       ;
 
 -                 if(p2) p2 -> next = p1                                    ;
 
 -                 else head = p1                                            ;
 
 -         } else {
 
 -                 fprintf(stderr , "\n")                                    ;
 
 -                 fprintf(stderr , "Error : malloc()\n")                    ;
 
 -                 fprintf(stderr , "\n")                                    ;
 
 -         }
 
 -         return head                                                       ;
 
 - }
 
  
- int main(void)
 
 - {
 
 -         struct student * L , * P                                          ;
 
 -         int i , m                                                         ;
 
 -         for(L = NULL , i = 0 ; i < 10 ; i ++) L = foo(L , 100 + i)        ;
 
 -         printf("%d" , L -> num)                                           ;
 
 -         for(P = L -> next ; P ; P = P -> next) printf(" , %d" , P -> num) ;
 
 -         printf("\n")                                                      ;
 
 -         for(P = L ; P ; L = P) {
 
 -                 P = L -> next                                             ;
 
 -                 free(L)                                                   ;
 
 -         }   
 
 - }
 
  复制代码 
        编译、运行实况: 
- D:\0002.Exercise\C>g++ -o x x.c
 
  
- D:\0002.Exercise\C>x
 
 - 100 , 101 , 102 , 103 , 104 , 105 , 106 , 107 , 108 , 109
 
  
- D:\0002.Exercise\C>
 
  复制代码 
         
        楼主似乎格外偏爱 L,定义个结构名叫 L,定义个函数,名字还叫 L,这是为什么呢? |   
 
 
 
 |