首页 > 代码库 > 线性表及其应用(约瑟夫环)
线性表及其应用(约瑟夫环)
我的工程包括:struct.h create.c next.c main.c
struct.h
#ifndef _STRUCT_H_#define _STRUCT_H_#include <stdio.h>#include <windows.h>#include <stdlib.h>struct person{ int r; int s; struct person *next; };extern struct person *create(int n); extern void next(struct person *p,int n,int m);#endif
create.c
#include "struct.h"struct person *create(int n) { struct person *head,*pnew,*ptail; int i; pnew=(struct person *)malloc(sizeof(struct person)); pnew->r=1; scanf("%d",&pnew->s); head=ptail=pnew; for(i=2;i<=n;i++) { pnew=(struct person *)malloc(sizeof(struct person)); pnew->r=i; scanf("%d",&pnew->s); ptail->next=pnew; ptail=pnew; } ptail->next=head; return ptail; }
next.c
#include "struct.h"void next(struct person *p,int N,int m){ int i=1,j=1,k=m; struct person *q; while(j <=N) { for(i=1;i<=k;i++) { q=p; p=p->next; } printf("%d ",p->r); k=p->s; q->next=p->next; free(p); p=q; j++; }}
main.c
#include "struct.h"int main(){ int m,N; struct person *head,*p; printf("Input the first m:"); scanf("%d",&m); printf("\nPlease input the number of people:"); scanf("%d",&N); printf("\nInput everyone secret:\n"); head=create(N); p=head; printf("The number is:\n"); next(p,N,m); return 0;}
线性表及其应用(约瑟夫环)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。