The realization of Joseph's link list in C language

The realization of Joseph's link list in C language

Joseph Ring is generally used for games in life

Code block

linklist.cpp:

// linklist.cpp  : defines the entry point for the console application.
//

#include "stdafx.h"
#include "linklist.h"


using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    link_plist l = NULL;
    int n = 0,ret = 0;
    //Create a one-way circular list
    cout<<"please input game number:";
    cin>>n;  

    linklist_create(&l,n);
    linklist_show(l);
    //Count N out and output (Joseph huhuan)
    cout<<"please input josep hu number:"<<endl;
    cin>>n;
    ret = josephu(l,n);
    cout<<"josep hu is:"<<ret<<endl;
    system("pause");
    return 0;
}

linklist.h:

#ifndef __LINKLIST_H_
#define __LINKLIST_H_

#include <stdio.h>
#include <stdlib.h>
#include "iostream"


typedef int datatype;

typedef struct linklist{
    datatype data;
    struct linklist *next;
}link_list,*link_plist;

extern void linklist_create(link_plist *l ,int n);
extern void linklist_show(link_plist l);
extern int josephu(link_plist l, int n);

#endif 

josephu.cpp:

#include "stdafx.h"
#include "linklist.h"

using namespace std;
/**

* Function name: Func

* By Lin Sen

* Date: December 12, 2017 15:16:42

**/ 
void linklist_create(link_plist *l ,int n)
{
    int i=0,num=0;
    link_plist New = NULL,p;
    //p = l;
    for(i = 1;i<=n;i++)
    {
        if(1 == i)
        {
            (*l) = (link_plist)malloc(sizeof(link_list));
            if((*l) == NULL)
            {
                cout<<"linklist create failed!";
                exit(1);
            }
            (*l)->data = i;
            (*l)->next = (*l);
            p = *l;
        }
        else 
        {       
            New = (link_plist)malloc(sizeof(link_list));
            if(New == NULL)
            {
                cout<<"linklist create failed!";
                exit(1);
            }
            New->data = i;
            //Insert footer
            New->next = (*l)->next;
            (*l)->next = New;

            (*l) = (*l)->next;

        }       
    }
}
/**

* Function name: linklist_show

* By Lin Sen

* Date: December 12, 2017 15:16:42

**/ 
void linklist_show(link_plist l)
{
    link_plist p = l->next;
    while(p!= l)
    {
        cout<<" "<<p->data<<"";
        p=p->next;
    }
    cout<<" "<<p->data<<"";
    cout<<""<<endl;
}
/**

* Function name: josephu

* By Lin Sen

* Date: December 12, 2017 15:16:42

**/ 
int josephu(link_plist l, int n)
{
    link_plist p = l,q;
    int i = 0;

    while(p->next != p)
    {
        //Find the previous location to delete
         for(i=0; i<n-2; i++)
        {
            p =p ->next;
        }
        //Delete element
         q = p->next;
         p->next =q->next;
         free(q);

        //Point to next position
         p = p->next;

    }
    //cout<<""<<endl;
    return p->data;;
}

footnote

Generate a footnote 1.

catalog

To generate a directory with [TOC]:

flow chart:

Created with Rapha ë l 2.1.0 starts with only one out? End yesno

ha-ha! Write a blog with markdown for the first time, and record

  1. Here is the content of the footnote

Keywords: C

Added by eatc7402 on Sun, 31 May 2020 13:08:12 +0300