josephon问题

josephon问题
看到首页上有人写,自己也写了一个

名字起错了,其实写个stack更合适

// !Node information
// !
struct  Node
{
    
int serialNumber;
    
int flag;
    
struct Node* next;
}
;

struct  List
{
    Node
* head;

    List()
    
{
        head
= NULL;
    }

    
~List()
    
{
        
if (head==NULL)
        
{
            
return;
        }


        Node
* p=head;
        Node
* q=head->next;

        
while(q!=NULL)
        
{
            delete p;
            p
=q;
            q
=q->next;
        }


        delete p;
        p
=NULL;
        

    }

    
    
void init(int size)
    
{
        
int i=1;
        
while(i<=size)
        
{
            
            push(size
-i+1);
            i
++;
        }

    }

    
//! the last one is the head
    void push(int i)
    
{
        Node
* pNew=new Node();
        pNew
->serialNumber= i;
        pNew
->flag= 1;
        pNew
->next=head;
        head
=pNew;
    }

    
    
void showAll()
    
{
        
if (head==NULL)
        
{
            
return;
        }


        Node
* temp=head;

        
while(temp)
        
{
            
if (temp->flag==1)
            
{
                printf(
"%d  ", temp->serialNumber);
            }

            
            temp
=temp->next;
            
        }


        printf(
"\n");
    }


    
int pop()
    
{
        
int result=0;

        
if (head==NULL)
        
{
            
return result;
        }


        Node
* temp=head;
        result
= head ->serialNumber;
        head
= head->next;
        delete temp;
        
return result;
    }


    
void kickOut(int circleNum, int liveNum)
    
{
        Node
* temp=head;
        
while( lenLive() > liveNum )
        
{
            
            
for (int i=0; i<circleNum;i++ )
            
{
                
if (temp->flag == 0)
                
{
                    i
--;
                }

                
if (i==( circleNum-1)&&temp->flag==1 )
                
{
                    temp
->flag = 0;
                }

                
                temp
=temp->next;
                
if (temp==NULL)
                
{
                    temp
=head;
                }


            }


            showAll();
            printf(
"\n");
        }

    }


    
int len()
    
{
        
if (head==NULL)
        
{
            
return 0;
        }

        Node
* temp=head;
        
int count=0;
        
while(temp)
        
{

            count
++;
            temp
=temp->next;
        }

        
return count;
    }


    
int lenLive()
    
{
        
if (head==NULL)
        
{
            
return 0;
        }

        Node
* temp=head;
        
int count=0;
        
while(temp)
        
{
            
if (temp->flag == 1)
            
{
                count
++;
            }

            
            temp
=temp->next;
        }

        
return count;
    }


}
;


// main.cpp
#include  < stdio.h >
#include 
< stdlib.h >
#include 
" list.h "

int  main()
{
    
    
int size=0;
    
while(true)
    
{
        List liveList;
        scanf(
"%d"&size);

        liveList.init(size);

        printf(
"liveList len: %d \n", liveList.len());

        liveList.kickOut(
32);

        liveList.showAll();
    }


    
return 0;
}

你可能感兴趣的:(josephon问题)