一群人围在一起从1到3循环数数,数到3的人退出,问最后留下的是几号?
- #include <stdio.h>
- #include <malloc.h>
- #define COUNT_MAX_NUMBER 3
- typedef struct _STUDENT_INFO_
- {
- int num;
- struct _STUDENT_INFO_ *next;
- }STUDENT_INFO;
- STUDENT_INFO *Creat_List(void)
- {
- STUDENT_INFO *head=NULL;
- STUDENT_INFO *pcurrent=NULL,*ptemp=NULL;
- int stu_num=0,n=0;
-
- do
- {
- printf("please input the number of student:");
- scanf("%d",&stu_num);
- if( stu_num == 0 )
- printf("sorry,the number is error!\n");
- else
- printf("\n");
-
- }while( stu_num == 0 );
-
- while( n<stu_num )
- {
- n=n+1;
-
- pcurrent=(STUDENT_INFO *)malloc(sizeof(STUDENT_INFO));
- if( pcurrent == NULL )
- {
- printf("malloc error!\n");
- break;
- }
- else
- pcurrent->num=n;
-
- if( n == 1 )
- head=pcurrent;
- else
- ptemp->next=pcurrent;
- ptemp=pcurrent;
- }
-
- ptemp->next=NULL;
- return(head);
- }
- STUDENT_INFO *Del_StuInfo(STUDENT_INFO *head,int stu_num)
- {
- STUDENT_INFO *pcurrent=NULL,*ptemp=NULL;
-
- if(head == NULL)
- {
- printf("no infomation to been printed,please check it!\n");
- return(NULL);
- }
-
- pcurrent=head;
-
- while( (stu_num!=pcurrent->num)&&(pcurrent->next!=NULL))
- {
- ptemp=pcurrent;
- pcurrent=pcurrent->next;
- }
-
- if( stu_num==pcurrent->num)
- {
- if(pcurrent == head)
- head=pcurrent->next;
- else
- ptemp->next=pcurrent->next;
-
- free(pcurrent);
- printf("we have deleted the num:%d\n",stu_num);
- }
- else
- printf("sorry,can not find the num:%d\n",stu_num);
-
- return(head);
- }
- int main(int argc,char *argv[])
- {
- STUDENT_INFO *head=NULL,*pcurrent=NULL,*ptemp=NULL;
- int i;
-
- head=Creat_List();
- pcurrent=head;
-
- for(i=1;i<=COUNT_MAX_NUMBER;i++)
- {
- ptemp=pcurrent->next;
-
- if( (pcurrent==head)&&(pcurrent->next==NULL) )
- {
- printf("the last number is: %d\n",pcurrent->num);
- break;
- }
-
- if(i == COUNT_MAX_NUMBER )
- {
- head=Del_StuInfo(head,pcurrent->num);
- i=0; //i should set to '0',because i++
- }
-
- pcurrent=ptemp;
- if( pcurrent==NULL)
- pcurrent=head;
- }
-
- return 0;
- }
阅读(1085) | 评论(0) | 转发(0) |