首页 > 代码库 > 线性表及其应用(约瑟夫环)

线性表及其应用(约瑟夫环)

我的工程包括: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;}

 

线性表及其应用(约瑟夫环)