约瑟夫问题(单链表)
2014-04-07 23:11
190 查看
#include <stdio.h> #include <stdlib.h> struct student{ int id; struct student * next; }; struct student * tailCreateList(){ struct student * L,* p, * last; L = (struct student *)malloc(sizeof(struct student)); last = L; int x; scanf("%d",&x); while(x!=-1){ p = (struct student *)malloc(sizeof(struct student)); p->id=x; last->next=p; last=p; p->next=NULL; scanf("%d",&x); } return L; } int studentNum(struct student * L){ int num=0; struct student * p; p = L->next; while(p!=NULL){ num++; p=p->next; } return num; } int updateList(struct student * L,int pos){ struct student * p; p = L; int step=0; while(p!=NULL&&step!=pos){ p=p->next; step++; } printf("%d ",p->id); p->id=0; return 0; } void display(struct student * L){ struct student * p; p=L->next; while(p!=NULL){ printf("%d ",p->id); p=p->next; } printf("\n"); } int menu(){ int pos=0; int count=0; struct student * L; printf("请依次输入学生编号:"); L=tailCreateList(); printf("学生编号依次为:"); display(L); int num=studentNum(L); printf("一共有学生%d人\n",num); printf("规定学生从1到m报数,报到m的退出!\n"); printf("请输入m值:"); int m; scanf("%d",&m); struct student * p; p = (struct student *)malloc(sizeof(struct student)); p = L->next; printf("依次出局的学生为:"); while(num>1){ if(p->id!=0){ count++; } if(count==m){ updateList(L,pos+1); count=0; num--; } pos++; p=p->next; if(pos==studentNum(L)){ pos=0; p=L->next; } } printf("\n"); for(p=L->next;p!=NULL;p=p->next){ if(p->id!=0){ printf("获胜者id: %d\n",p->id); } } free(L); return 0; } int main(){ menu(); return 0; }
相关文章推荐
- C语言数据结构之利用循环链表解决约瑟夫问题
- 约瑟夫问题(单循环链表解决)
- 链表---约瑟夫问题
- 约瑟夫问题(本人使用链表解决的)
- 链表解决约瑟夫环问题
- 环形链表实现约瑟夫问题 java语言
- 约瑟夫问题3——出列序列(链表)
- C++ HOJ 约瑟夫问题 的 数组、链表及STL实现
- 17.环形链表,以及用环形链表解决约瑟夫问题
- SDUT OJ 约瑟夫问题——链表
- 约瑟夫问题(链表经典)
- 约瑟夫问题——链表实现
- 数据结构学习笔记(二) 链表之用循环链表解决约瑟夫问题
- 【练习】双向链表应用于约瑟夫问题
- 循环链表解决约瑟夫问题
- 韩顺平_PHP程序员玩转算法公开课(第一季)05_使用单链表解决约瑟夫问题_学习笔记_源代码图解_PPT文档整理
- 用链表实现约瑟夫问题(c/C++)
- 用单循环链表实现约瑟夫问题。
- 小孩报数-约瑟夫问题数组链表
- 约瑟夫(Josephus)问题的求解——利用循环链表