鱼C论坛

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

[技术交流] 56课-链表 参考码源

[复制链接]
发表于 2015-12-9 20:27:21 | 显示全部楼层 |阅读模式

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

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

x

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #define LEN sizeof(struct str)
  5. struct str * CJLB();//创建链表 返回链表头 地址
  6. void DYLB(struct str *head);//打印链表

  7. struct str //自定义数据类型
  8. {
  9.     long xuehao;
  10.     float chengji;
  11.     struct str *next;

  12. };
  13. void main()

  14. {
  15.    
  16.    
  17.    
  18.     DYLB(CJLB());
  19.     /*
  20.     DYLB 参数1:链表的首地址
  21.     CJLB 没有参数 返回链表首地址

  22.     */


  23.    



  24.    
  25.    


  26.    
  27.    

  28. }
  29. struct str *  CJLB()//定义创建链表 函数
  30. {
  31.     struct str *head,*p1,*p2;
  32.    
  33.    
  34.     p1=(struct str*)malloc(LEN);//申请内存
  35.    
  36.    
  37.    
  38.    
  39.    
  40.     head=p1;//首地址
  41.    
  42.    
  43.      
  44.    
  45.     printf("请输入学号:");
  46.     scanf("%d",&p1->xuehao);//头地址
  47.     printf("请输入成绩:");
  48.     scanf("%f",&p1->chengji);//头地址

  49.     p2=(struct str*)malloc(LEN);//申请内存
  50.     p1->next=p2;//指向申请的内存的地址
  51.     while (1)
  52.     {   struct str *v;
  53.         printf("请输入学号:");
  54.         scanf("%d",&p2->xuehao);
  55.         
  56.         if(p2->xuehao ==0)//判断是否符合条件结束循环
  57.         {  
  58.             v->next=NULL;

  59.         break;
  60.         
  61.         }
  62.         printf("请输入成绩:");
  63.         scanf("%f",&p2->chengji);
  64.         p1=(struct str*)malloc(LEN);
  65.         p2->next=p1;
  66.         v=p2;//记录P2的地址 如果下一次输入学号 0  则让p2指向地址为 NULL


  67.         p2=p1;
  68.         
  69.         
  70.         
  71.         
  72.     }
  73.     return head;


  74. }
  75. void DYLB(struct str *head)//定义打印链表 函数
  76. {
  77.     printf("***************************************\n");
  78.    
  79.     do
  80.     {  /*
  81.         if (head->xuehao==0)
  82.         {   head=head->next;
  83.         continue;
  84.         
  85.         }
  86.         */
  87.       
  88.         
  89.         
  90.         
  91.         
  92.         printf("学号:%d\n",head->xuehao);
  93.         printf("成绩:%f\n",head->chengji);
  94.         printf("-----------------------------\n");
  95.         head=head->next;
  96.         
  97.         
  98.     } while (head);
  99.    
  100.     printf("***************************************\n");
  101.    
  102.    
  103.    


  104. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-12-9 20:31:39 | 显示全部楼层
眼看代码千篇,不如模仿一篇,模仿代码一篇,不如调试一篇
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-21 13:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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