Where there is a dream ,there is hope

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  64 Posts :: 0 Stories :: 8 Comments :: 0 Trackbacks

常用链接

留言簿(1)

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

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

名字起错了,其实写个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;
}
posted on 2011-03-16 10:58 IT菜鸟 阅读(311) 评论(1)  编辑 收藏 引用

Feedback

# re: josephon问题 2011-03-16 11:31 Husiwa
不过这个模拟过程非常不好,对于100000以上的人数来说简直就是悲剧。。。
  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理