Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4027410
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7195
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: LINUX

2011-08-17 01:19:07

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4. //#include "student.h"

  5. struct student
  6. {
  7.     int num;
  8.     char name[20];
  9.     int score;
  10.     struct student *next;
  11. };

  12. typedef enum _stu_flag_
  13. {
  14.     STU_NULL,
  15.     STU_HEAD,
  16.     STU_NUM,
  17.     STU_SCORE,
  18.     STU_END,
  19.     STU_MAX
  20. }stu_flag;

  21. typedef enum _sort_flag_
  22. {
  23.     SORT_NULL,
  24.     SORT_UP,
  25.     SORT_DOWN,
  26.     SORT_MAX
  27. }sort_flag;

  28. struct student *Input_StuData(void)
  29. {
  30.     struct student *head=NULL;
  31.     struct student *pcurrent=NULL,*ptemp=NULL;
  32.     int stu_num=0,n=0;
  33.     
  34.     do
  35.     {
  36.         printf("please input the number of student:");
  37.         scanf("%d",&stu_num);
  38.         if( stu_num == 0 )
  39.             printf("sorry,the number is error!\n");
  40.         else
  41.             printf("\n");
  42.             
  43.     }while( stu_num == 0 );
  44.        
  45.     while( n<stu_num )
  46.     {
  47.         n=n+1;
  48.                          
  49.         pcurrent=(struct student *)malloc(sizeof(struct student));
  50.         if( pcurrent == NULL )
  51.         {
  52.             printf("malloc error!\n");
  53.             break;
  54.         }
  55.         else
  56.         {
  57.             printf("input the number:\n");
  58.             scanf("%d",&pcurrent->num);
  59.             printf("input the name:\n");
  60.             scanf("%s",pcurrent->name);
  61.             printf("input the score:\n");
  62.             scanf("%d",&pcurrent->score);
  63.         }
  64.         
  65.         if( n == 1 )
  66.             head=pcurrent;
  67.         else
  68.             ptemp->next=pcurrent;

  69.         ptemp=pcurrent;
  70.     }
  71.     
  72.     printf("input finished!\n");
  73.     ptemp->next=NULL;
  74.     return(head);
  75. }

  76. void Print_StuInfo(struct student *head)
  77. {
  78.     struct student *ptemp=NULL;
  79.     
  80.     if(head == NULL)
  81.     {
  82.         printf("no infomation to been printed,please check it!\n");
  83.         return;
  84.     }
  85.     
  86.     ptemp=head;
  87.     while( ptemp!=NULL )
  88.     {
  89.         printf("num :%4d,name :%10s,score :%4d\n",ptemp->num,ptemp->name,ptemp->score);
  90.         ptemp=ptemp->next;
  91.     }
  92.     
  93.     return;
  94. }

  95. struct student *Delete_WithNum(struct student *head,int stu_num)
  96. {
  97.     struct student *pcurrent=NULL,*ptemp=NULL;
  98.     
  99.     if(head == NULL)
  100.     {
  101.         printf("no infomation to been printed,please check it!\n");
  102.         return(NULL);
  103.     }
  104.     
  105.     pcurrent=head;
  106.     
  107.     while( (stu_num!=pcurrent->num)&&(pcurrent->next!=NULL))
  108.     {
  109.         ptemp=pcurrent;
  110.         pcurrent=pcurrent->next;
  111.     }
  112.     
  113.     if( stu_num==pcurrent->num)
  114.     {
  115.         if(pcurrent == head)
  116.             head=pcurrent->next;
  117.         else
  118.             ptemp->next=pcurrent->next;
  119.         
  120.         free(pcurrent);
  121.         printf("we have deleted the num:%d\n",stu_num);
  122.     }
  123.     else
  124.         printf("sorry,can not find the num:%d\n",stu_num);
  125.     
  126.     return(head);
  127. }

  128. /*if have the num,just update the student information*/
  129. int Check_StuNum(struct student *head,struct student *stu_info)
  130. {
  131.     struct student *ptemp;

  132.     for(ptemp=head;ptemp->next!=NULL;ptemp=ptemp->next)
  133.     {
  134.         if((ptemp->num)==(stu_info->num))
  135.         {
  136.             strcpy(ptemp->name,stu_info->name);
  137.             ptemp->score=stu_info->score;
  138.             return 1;
  139.         }
  140.     }
  141.     
  142.     return 0;
  143. }

  144. struct student *Add_2Head(struct student *head,struct student *stu_info)
  145. {
  146.     struct student *ptemp;
  147.     
  148.     if( Check_StuNum(head,stu_info) )
  149.         return(head);
  150.         
  151.     ptemp=head;
  152.     head=stu_info;
  153.     stu_info->next=ptemp;
  154.     
  155.     return(head);
  156. }

  157. struct student *Add_2End(struct student *head,struct student *stu_info)
  158. {
  159.     struct student *ptemp;
  160.     
  161.     if( Check_StuNum(head,stu_info) )
  162.         return(head);
  163.         
  164.     for(ptemp=head;ptemp->next!=NULL;ptemp=ptemp->next);
  165.     
  166.     ptemp->next=stu_info;
  167.     stu_info->next=NULL;
  168.     
  169.     return(head);
  170. }

  171. struct student *Add_WithNum(struct student *head,struct student *stu_info)
  172. {
  173.     struct student *pcurrent=NULL,*ptemp=NULL,*pinsert=NULL;
  174.     
  175.     pcurrent=head;
  176.     pinsert=stu_info;
  177.     
  178.     if( Check_StuNum(pcurrent,pinsert) )
  179.         return(head);
  180.     
  181.     if( head==NULL)
  182.     {
  183.         head=pinsert;
  184.         pinsert->next=NULL;
  185.     }
  186.     else
  187.     {
  188.         while((pinsert->num<pcurrent->num)&&(pcurrent->next!=NULL))
  189.         {
  190.             ptemp=pcurrent;
  191.             pcurrent=pcurrent->next;
  192.         }
  193.         
  194.         if(pinsert->num>=pcurrent->num)
  195.         {
  196.             if( head==pcurrent )
  197.                 head=pinsert;
  198.             else
  199.                 ptemp->next=pinsert;
  200.                 
  201.             pinsert->next=pcurrent;
  202.         }
  203.         else
  204.         {
  205.             pcurrent->next=pinsert;
  206.             pinsert->next=NULL;
  207.         }
  208.     }
  209.     
  210.     return(head);
  211. }

  212. struct student *Add_WithScore(struct student *head,struct student *stu_info)
  213. {
  214.     struct student *pcurrent=NULL,*ptemp=NULL,*pinsert=NULL;
  215.     
  216.     pcurrent=head;
  217.     pinsert=stu_info;
  218.     
  219.     if( Check_StuNum(pcurrent,pinsert) )
  220.         return(head);
  221.     
  222.     if( head==NULL)
  223.     {
  224.         head=pinsert;
  225.         pinsert->next=NULL;
  226.     }
  227.     else
  228.     {
  229.         while((pinsert->score>pcurrent->score)&&(pcurrent->next!=NULL))
  230.         {
  231.             ptemp=pcurrent;
  232.             pcurrent=pcurrent->next;
  233.         }
  234.         
  235.         if(pinsert->score<=pcurrent->score)
  236.         {
  237.             if(head==pcurrent)
  238.                 head=pinsert;
  239.             else
  240.                 ptemp->next=pinsert;
  241.                 
  242.             pinsert->next=pcurrent;
  243.         }
  244.         else
  245.         {
  246.             pcurrent->next=pinsert;
  247.             pinsert->next=NULL;
  248.         }
  249.     }
  250.     
  251.     return(head);
  252. }

  253. /*flag: indicat which way to insert the student information*/
  254. struct student *Add_StuInfo(struct student *head,struct student *stu_info,stu_flag flag)
  255. {
  256.     struct student *ptemp=NULL;
  257.     
  258.     if( stu_info == NULL )
  259.     {
  260.         printf("sorry,your information error,please check it!\n");
  261.         return(NULL);
  262.     }    
  263.     
  264.     switch(flag)
  265.     {
  266.         case STU_HEAD:
  267.             ptemp=Add_2Head(head,stu_info);
  268.         break;
  269.         
  270.         case STU_NUM:
  271.             ptemp=Add_WithNum(head,stu_info);
  272.         break;

  273.         case STU_SCORE:
  274.             ptemp=Add_WithScore(head,stu_info);
  275.         break;
  276.         
  277.         case STU_END:
  278.             ptemp=Add_2End(head,stu_info);
  279.         break;
  280.         
  281.         default:
  282.             printf("error:flag %d\n",flag);
  283.             return(NULL);
  284.         break;
  285.     }
  286.     
  287.     head=ptemp;
  288.     return(head);
  289. }

  290. struct student *Sort_Up4Num(struct student *head)
  291. {
  292.     struct student *pcurrent=NULL,*pcompare=NULL,*pbig=NULL;    
  293.     int temp;
  294.     char name[20];
  295.     
  296.     for(pcurrent=head;pcurrent->next!=NULL;pcurrent=pcurrent->next)
  297.     {
  298.         pbig=pcurrent;
  299.         
  300.         for(pcompare=pcurrent->next;pcompare;pcompare=pcompare->next)
  301.         {
  302.             if(pbig->num<pcompare->num)
  303.                 pbig=pcompare;
  304.         }
  305.         
  306.         if( pbig!=pcurrent )
  307.         {                            
  308.             temp=pcurrent->num;
  309.             pcurrent->num=pbig->num;
  310.             pbig->num=temp;
  311.             
  312.             temp=pcurrent->score;
  313.             pcurrent->score=pbig->score;
  314.             pbig->score=temp;
  315.             
  316.             strcpy(name,pcurrent->name);
  317.             strcpy(pcurrent->name,pbig->name);
  318.             strcpy(pbig->name,name);
  319.         }
  320.     }
  321.     
  322.     return(head);    
  323. }

  324. struct student *Sort_Down4Num(struct student *head)
  325. {
  326.     return(NULL);
  327. }

  328. /*flag: indicat how to sort*/
  329. struct student *Sort_4Num(struct student *head,sort_flag flag)
  330. {
  331.     struct student *ptemp=NULL;    
  332.     
  333.     switch(flag)
  334.     {
  335.         case SORT_UP:
  336.             ptemp=Sort_Up4Num(head);
  337.         break;
  338.         
  339.         case SORT_DOWN:
  340.             ptemp=Sort_Down4Num(head);
  341.         break;
  342.         
  343.         default:
  344.             printf("error:flag %d\n",flag);
  345.             return(NULL);
  346.         break;
  347.     }
  348.     
  349.     head=ptemp;
  350.     return(head);
  351. }

  352. struct student *Sort_Up4Score(struct student *head)
  353. {
  354.     return(NULL);
  355. }

  356. struct student *Sort_Down4Score(struct student *head)
  357. {
  358.     return(NULL);
  359. }

  360. struct student *Sort_4Score(struct student *head,sort_flag flag)
  361. {
  362.     struct student *ptemp=NULL;
  363.     
  364.     switch(flag)
  365.     {
  366.         case SORT_UP:
  367.             ptemp=Sort_Up4Score(head);
  368.         break;
  369.         
  370.         case SORT_DOWN:
  371.             ptemp=Sort_Down4Score(head);
  372.         break;
  373.         
  374.         default:
  375.             printf("error:flag %d\n",flag);
  376.             return(NULL);
  377.         break;
  378.     }
  379.     
  380.     head=ptemp;
  381.     return(head);
  382. }

  383. int main(int argc,char **argv)
  384. {
  385.     struct student *head=NULL,*stu_info=NULL;
  386.     int stu_num;
  387.     
  388.     head=(struct student *)Input_StuData();
  389.     head=Sort_4Num(head,SORT_UP);
  390.     Print_StuInfo(head);
  391.     
  392.     printf("please input the delete number of student:\n");
  393.     scanf("%d",&stu_num);
  394.     head=Delete_WithNum(head,stu_num);
  395.     Print_StuInfo(head);
  396.     
  397.     printf("please input the inset information:\n");
  398.     stu_info=(struct student *)malloc(sizeof(struct student));    
  399.     printf("input the number:\n");
  400.     scanf("%d",&stu_info->num);
  401.     printf("input the name:\n");
  402.     scanf("%s",stu_info->name);
  403.     printf("input the score:\n");
  404.     scanf("%d",&stu_info->score);
  405.     head=Add_StuInfo(head,stu_info,STU_NUM);
  406.     Print_StuInfo(head);
  407.     
  408.     return 0;
  409. }

包含以下一些内容:

1、建立学生链表信息;2、增加或删除一个节点;3、排序

具体的一些api可编程验证!

 

 student_info.rar   

阅读(1086) | 评论(4) | 转发(0) |
给主人留下些什么吧!~~

txgc_wm2013-04-14 22:06:14

都不敢相信这是我写的代码。

2011-08-17 16:12:55

211阳光生活网 大学生信用卡
http://www.211sun.com/
http://www.83193999.com/

txgc_wm2011-08-17 01:42:04

c语言是有bool类型的,而且c语言的声明也同样可以位于语句之后。

只不过,c90不支持上述特性,c99才支持。

c99允许声明想c++那样,在语句之后进行。

在c99中,加入stdbool.h头文件就有bool类型了。

在c90中可以通过枚举类型进行模拟,枚举是最好的方法:

enum bool { false, true };