正文

报数问题(循环链表的应用)2005-04-28 12:11:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/jay0518/799.html

分享到:

问题; 一个经常出现的问题: n个人围成一圈,从第一个人开始1,2,3报数,当一个人报到数3时,退出.接下的从一在开始往下抱,遇3退出,依次循环下去,直到只剩一个人,输出这个人的原始数,如:输入7 结果输出;4 #include<stdio.h> #include<alloc.h> #include<conio.h> typedef struct node { int data; struct node *next; }node,*cllist; cllist head; void createlist(void) { cllist p,new,new1; int i; printf("please input the number of people:"); scanf("%d",&i); printf("\n"); while(i<=1) { printf("error due to the wrong input ! please input it again!\n"); scanf("%d",&i); } head=NULL; p=new=(node *)malloc(sizeof(node)); if(!new) {   printf("overflow!"); exit(0); } new->next=head; head=new; head->data=i; while(i>1) {   new1=(node *)malloc(sizeof(node)); if(!new1)    {     printf("overflow.\n"); exit(0); } i--; new1->next=head; head=new1; head->data=i; } p->next=head; p=p->next; while(head->next!=head)    {     p=head=head->next;     p=p->next; head->next=head->next->next; free(p); if(head->next!=head) p=head=head->next; } printf("the left one's number is:%d",head->data); } main() { clrscr(); createlist(); }

阅读(5125) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册