用链表实现约瑟夫环
来源:互联网 发布:js获取所有兄弟元素 编辑:程序博客网 时间:2024/06/14 12:39
#include<stdio.h>#include<stdlib.h>struct stu{//ID用来保存序号int ID;struct stu* next;};struct stu*creat(struct stu *pHead,int n)//创建一个具有n个节点的循环链表{struct stu *pNew,*pEnd;int i=1;pHead=(struct stu *)malloc(sizeof(struct stu));pHead->ID=1;pEnd=pHead;for(i=2;i<=n;i++){pNew=(struct stu *)malloc(sizeof(struct stu));pNew->ID=i;pEnd->next=pNew;pEnd=pNew; }pEnd->next=pHead;return pHead;}void dis(struct stu*pHead)//显示链表{struct stu* p=pHead;int i=0;while(p->next!=pHead){printf("%d ",p->ID);p=p->next;i++;if(i%5==0) printf("\n"); } printf("%d\n",p->ID);}struct stu* del(struct stu* p)//删除传入节点的下一个节点{struct stu* temp=p->next;printf("%d ",p->next->ID);p->next=p->next->next;free(temp->next);return p->next;}int main(){int num,place,i,j;printf("请输入有多少人,出列位置(>=2)");scanf("%d %d",&num,&place);struct stu*pHead,*p;pHead=(struct stu*)malloc(sizeof(struct stu));pHead=creat(pHead,num);dis(pHead);p=pHead;printf("删除\n"); for(i=1;i<=num;i++){for(j=2;j<place;j++){p=p->next;}p=del(p);if(i%5==0) printf("\n");} }
0 0
- 用链表实现约瑟夫环
- 用链表实现约瑟夫环
- 成功用链表实现约瑟夫环
- 【约瑟夫】编程实现约瑟夫环
- 约瑟夫问题用链表实现
- 约瑟夫环(VC++实现)
- java实现约瑟夫环
- 数组实现约瑟夫环
- 约瑟夫环 php实现
- 约瑟夫环数组实现
- java实现约瑟夫环
- 约瑟夫环 PHP实现
- 约瑟夫环的实现
- java 实现约瑟夫环
- 约瑟夫环--java实现
- 约瑟夫环的实现
- 约瑟夫环 java实现
- 约瑟夫环 python 实现
- Android之数据存储
- 一个小故事
- Node.js系列——(4)优势及场景
- c++实验5—数组分离
- html基础编程
- 用链表实现约瑟夫环
- 这里一定有你需要的资源和方法!
- MySql 之建表语句
- bzoj 4872: [Shoi2017]分手是祝愿 期望dp
- Maven中国仓库
- c++实验五-数组分离
- kali2.0 或者滚动版,安装32位运行库
- 微信授权登录获取用户信息的步骤
- log 4 qt 使用(三)