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

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: LINUX

2011-08-17 02:33:56

     一群人围在一起从1到3循环数数,数到3的人退出,问最后留下的是几号?
 
  1. #include <stdio.h>
  2. #include <malloc.h>

  3. #define COUNT_MAX_NUMBER 3

  4. typedef struct _STUDENT_INFO_
  5. {
  6.     int num;
  7.     struct _STUDENT_INFO_ *next;
  8. }STUDENT_INFO;


  9. STUDENT_INFO *Creat_List(void)
  10. {
  11.     STUDENT_INFO *head=NULL;
  12.     STUDENT_INFO *pcurrent=NULL,*ptemp=NULL;
  13.     int stu_num=0,n=0;
  14.     
  15.     do
  16.     {
  17.         printf("please input the number of student:");
  18.         scanf("%d",&stu_num);
  19.         if( stu_num == 0 )
  20.             printf("sorry,the number is error!\n");
  21.         else
  22.             printf("\n");
  23.             
  24.     }while( stu_num == 0 );
  25.        
  26.     while( n<stu_num )
  27.     {
  28.         n=n+1;
  29.                          
  30.         pcurrent=(STUDENT_INFO *)malloc(sizeof(STUDENT_INFO));
  31.         if( pcurrent == NULL )
  32.         {
  33.             printf("malloc error!\n");
  34.             break;
  35.         }
  36.         else
  37.            pcurrent->num=n;
  38.         
  39.         if( n == 1 )
  40.             head=pcurrent;
  41.         else
  42.             ptemp->next=pcurrent;

  43.         ptemp=pcurrent;
  44.     }
  45.     
  46.     ptemp->next=NULL;
  47.     return(head);
  48. }

  49. STUDENT_INFO *Del_StuInfo(STUDENT_INFO *head,int stu_num)
  50. {
  51.     STUDENT_INFO *pcurrent=NULL,*ptemp=NULL;
  52.     
  53.     if(head == NULL)
  54.     {
  55.         printf("no infomation to been printed,please check it!\n");
  56.         return(NULL);
  57.     }
  58.     
  59.     pcurrent=head;
  60.     
  61.     while( (stu_num!=pcurrent->num)&&(pcurrent->next!=NULL))
  62.     {
  63.         ptemp=pcurrent;
  64.         pcurrent=pcurrent->next;
  65.     }
  66.     
  67.     if( stu_num==pcurrent->num)
  68.     {
  69.         if(pcurrent == head)
  70.             head=pcurrent->next;
  71.         else
  72.             ptemp->next=pcurrent->next;
  73.         
  74.         free(pcurrent);
  75.         printf("we have deleted the num:%d\n",stu_num);
  76.     }
  77.     else
  78.         printf("sorry,can not find the num:%d\n",stu_num);
  79.     
  80.     return(head);
  81. }

  82. int main(int argc,char *argv[])
  83. {
  84.     STUDENT_INFO *head=NULL,*pcurrent=NULL,*ptemp=NULL;
  85.     int i;
  86.     
  87.     head=Creat_List();
  88.     pcurrent=head;
  89.     
  90.     for(i=1;i<=COUNT_MAX_NUMBER;i++)
  91.     {            
  92.         ptemp=pcurrent->next;
  93.         
  94.         if( (pcurrent==head)&&(pcurrent->next==NULL) )
  95.         {
  96.             printf("the last number is: %d\n",pcurrent->num);
  97.             break;
  98.         }
  99.     
  100.         if(i == COUNT_MAX_NUMBER )
  101.         {
  102.             head=Del_StuInfo(head,pcurrent->num);
  103.             i=0; //i should set to '0',because i++
  104.         }
  105.         
  106.         pcurrent=ptemp;
  107.         if( pcurrent==NULL)
  108.             pcurrent=head;
  109.     }
  110.     
  111.     return 0;
  112. }
阅读(1043) | 评论(0) | 转发(0) |
0

上一篇:单链表的相关操作

下一篇:倒叙输出

给主人留下些什么吧!~~