Josephus Ring(链表)

#include
#include

typedef struct node{
    int number;
    int psw;
    struct node *next;
}LNode,*LinkList;


int main(){

    void createJoseph(LNode *,int);
    void exJoseph(LNode *,int);

    LNode *jsp;
    int n,m;
    jsp = (LinkList)malloc(sizeof(LNode));

    std::cout<<"Please enter the number of the peaple in the circle:"<
    std::cin>>n;
    createJoseph(jsp,n);

    std::cout<<"Please enter the first maximum number:"<
    std::cin>>m;
    exJoseph(jsp,m);

    return 0;
}


void createJoseph(LNode *jsp,int n){
    LNode *p = jsp,*q;
    int i,e;

    std::cout<<"Plaease enter the password for people in the Joseph circle:"<
    std::cin>>e;
    p->number = 1;
    p->psw = e;
    jsp = p;

    for(i = 1;i
        std::cin>>e;
        q = (LinkList)malloc(sizeof(LNode));
        q->number = i+1;
        q->psw = e;

        p->next = q;
        p = q;
    }

    p->next = jsp;
}


void exJoseph(LNode *jsp,int m){
    LNode *p,*q;
    int i;

    p = q = jsp;

    while(p->next!=p){
        for(i = 0;i
            p = p->next;
        }
        q = p->next;

        p->next = q->next;
        std::cout<number<<"";
        m = q->psw;
        free(q);
        p = p->next;
    }
    std::cout<<"The last person int the circle is "<number<
}

你可能感兴趣的:(code)