鱼C论坛

 找回密码
 立即注册
楼主: 小甲鱼

第十章 结构体与共用体(课件+源代码)

  [复制链接]
发表于 2014-5-18 16:54:29 | 显示全部楼层
贵。。。。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-5-19 21:26:35 | 显示全部楼层
堆了几天的鱼币瞬间就没有了  好贵:cry
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-5-31 20:40:25 | 显示全部楼层
22222222222222222222222222222222
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-6-5 17:18:22 | 显示全部楼层

  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>

  4. #define LEN sizeof(struct student)

  5. struct student *creat();                                //  创建链表
  6. struct student *del(struct student *head, int num);     //  del函数用于删除节点
  7. struct student *insert(struct student *head, struct student *stu_2);         /*插入一个节点, 第一个形参,待插入的链表, 第二个形参,待插入的结构的地址*/           
  8. void print(struct student *head);                       //  打印链表, 可以不用参数

  9. struct student
  10. {
  11.         int num;
  12.         float score;
  13.         struct student *next;
  14. };

  15. int n;


  16. void main()
  17. {
  18.         struct student *stu, *p, *stu_2;
  19.         int del_num;
  20.         printf("input records!\n");
  21.         stu=creat();
  22.         p = stu;
  23.         print(p);

  24.         printf("\n");

  25.         printf("please input the num to delete:  "); //删除
  26.         scanf("%d", &del_num);

  27.     while(del_num!=0)      // 循环删除,0 退出           
  28.         {
  29.                 p=del(p,del_num);
  30.                 print(p);
  31.                 printf("please input the num to delete:  ");
  32.             scanf("%d", &del_num);

  33.         }
  34.        

  35.     printf("\nplease input the record to insert:   \n");  //插入
  36.         stu_2=(struct student * )malloc(LEN);
  37.         printf("please input the num : ");
  38.         scanf("%d",&stu_2->num);
  39.         printf("please input the score : ");
  40.         scanf("%f",&stu_2->score);

  41.     while(stu_2->num!=0)
  42.         {
  43.                 p=insert(stu,stu_2);
  44.             print(p);
  45.         printf("\nplease input the record to insert:   \n");
  46.                 stu_2=(struct student * )malloc(LEN);
  47.                 printf("please input the num : ");
  48.                 scanf("%d",&stu_2->num);
  49.                 printf("please input the score : ");
  50.                 scanf("%f",&stu_2->score);
  51.         }       
  52.    
  53.         printf("\n");
  54.         system("pause");
  55. }

  56. struct student *creat()           //创建链表
  57. {
  58.         struct student *head;
  59.         struct student *p1, *p2;
  60.         p1=p2=(struct student * )malloc(LEN);   // LEN 是结构体 student 的长度大小

  61.         printf("please input the num : ");
  62.         scanf("%d",&p1->num);
  63.         printf("please input the score : ");
  64.         scanf("%f",&p1->score);
  65.     printf("\n");
  66.        
  67.         head=NULL;
  68.         n=0;
  69.         while(p1->num!=0)
  70.         {
  71.                 n=n+1;
  72.                 if(n==1)
  73.                 {
  74.                         head=p2;            
  75.                 }
  76.                 else
  77.                 {
  78.                         p2->next=p1;                              
  79.                 }
  80.                 p2=p1;                                //  p2 指向表尾, p1为新建单元
  81.                 p1=(struct student * )malloc(LEN);
  82.                 printf("please input the num : ");
  83.                 scanf("%d",&p1->num);
  84.                 printf("please input the score : ");
  85.                 scanf("%f",&p1->score);
  86.                 printf("\n");
  87.         }
  88.         p2->next=NULL;

  89.         return head;
  90. }


  91. void print(struct student *head)                    //打印链表
  92. {
  93.         struct student *p;
  94.         printf("\nThere are %d records!\n\n",n);

  95.         p=head;
  96.         if(head!=NULL)
  97.         {
  98.                 do
  99.                 {
  100.                         printf("学号为 %d 的成绩为 %f \n", p->num, p->score);
  101.                         p=p->next;
  102.                 }while(p!=NULL);
  103.         }

  104. }



  105. struct student *del(struct student *head, int num)      // 删除链表节点
  106. {
  107.         struct student *p1, *p2;
  108.         if(head==NULL)
  109.         {
  110.                 printf("\nList null\n");
  111.                 goto end;
  112.         }
  113.     p1=head;
  114.         while(num!=p1->num && p1->next!=NULL)
  115.         {
  116.                 p2=p1;
  117.                 p1=p1->next;
  118.         }
  119.         if(num==p1->num)
  120.         {
  121.                 if(p1==head)                   //要删的是头结点的时候
  122.                 {
  123.                         head=p1->next;
  124.                 }
  125.                 else
  126.                 {
  127.                         p2->next=p1->next;
  128.                 }
  129.                 printf("delete: No. %d succeed!\n", num);
  130.                 n=n-1;                        //n是一个全局变量,记录存放了多少链表单元
  131.         }
  132.         else
  133.         {
  134.                 printf("%d not been found! \n", num);
  135.         }
  136. end:
  137.         return head;
  138. }



  139. struct student *insert(struct student *head, struct student *stu_2)    //插入函数
  140. {
  141.         struct student *p0,*p1, *p2;
  142.         p1=head;
  143.         p0=stu_2;
  144.         if(head==NULL)
  145.         {
  146.                 head=p0;
  147.                 p0->next=NULL;
  148.         }
  149.         else
  150.         {  
  151.                 while( (p0->num > p1->num) && (p1->next!=NULL) )     //   两种情况退出 while
  152.                 {
  153.                         p2=p1;
  154.                         p1=p1->next;
  155.                 }
  156.                 if(p0->num <= p1->num)
  157.                 {
  158.                         if(head==p1)
  159.                         {
  160.                                 head=p0;
  161.                         }
  162.                         else
  163.                         {
  164.                                 p2->next=p0;
  165.                         }
  166.                         p0->next=p1;
  167.                 }
  168.                 else              //p0的num最大,插在尾部
  169.                 {
  170.                         p1->next=p0;
  171.                         p0->next=NULL;
  172.                 }
  173.                 n=n+1;
  174.                 return head;
  175.         }
  176. }
复制代码
c语言,第十章第五节, 结构体与共用体,共057.
作业, 实现多次插入和多次删除。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-6-5 20:08:17 | 显示全部楼层
淡定,淡定,淡定……
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-6-9 16:07:28 | 显示全部楼层
淡定,淡定,淡定……
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-6-19 20:56:55 | 显示全部楼层
我无语了……好贵啊。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-6-20 21:15:23 | 显示全部楼层
淡定,淡定,淡定……
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-6-23 23:47:40 | 显示全部楼层
下了,看看。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-7-4 18:50:35 | 显示全部楼层
没钱了:cry:cry:cry:cry:cry:cry:cry
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-7-16 09:50:51 | 显示全部楼层
感恩无私的分享与奉献 :)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-7-30 17:10:03 | 显示全部楼层
没有课题的源代码么。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-7-30 17:13:59 | 显示全部楼层
没有看到源代码啊,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-8-18 20:54:01 | 显示全部楼层
呜呜呜,没钱买呀      
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-8-27 10:16:48 | 显示全部楼层
我没鱼币啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-9-23 22:48:57 | 显示全部楼层
我是VIP,我骄傲!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-1-23 21:23:04 | 显示全部楼层
太贵了啊!太生气了,无法HOLD啦 >_<......
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-3-10 09:39:31 | 显示全部楼层
xuexi
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-5-4 11:51:13 | 显示全部楼层
谢谢老师上传!强烈支持楼主ing……
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-5-4 11:51:48 | 显示全部楼层
好多鱼币啊!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-19 09:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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