码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-12-02 20:44:24      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   sp   for   on   div   log   

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

 

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

标签:style   blog   io   color   sp   for   on   div   log   

原文地址:http://www.cnblogs.com/ht-beyond/p/4138360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!