- #include<iostream>
- using namespace std;
- struct Node
- {
- int data;
- Node *next;
- };
- Node *initList(int n)
- {
- Node *list = new Node;
- Node *p = list;
- p->data = 1;
- p->next = NULL;
- for (int i = 2; i <= n; i++)
- {
- Node *temp = new Node;
- temp->data = i;
- temp->next = NULL;
- p->next = temp;
- p = temp;
- }
- p->next = list;
- return list;
- }
- void browse(Node *list)
- {
- if (list)
- {
- Node *p = list;
- cout<<p->data<<' ';
- while (p->next != list)
- {
- p = p->next;
- cout<<p->data<<' ';
- }
- }
- cout<<endl;
- }
- int main()
- {
- int n;
- int passwd[20];
- cout<<"请输入小孩的个数:";
- cin>>n;
- Node *mylist = initList(n);
- cout<<"初始编号是: ";
- browse(mylist);
- cout<<"请输入初始密码:";
- cin>>passwd[0];
- for (int j = 1; j <= n; j++)
- {
- cout<<"输入第"<<j<<"个小孩的密码:";
- cin>>passwd[j];
- }
- cout<<"出列顺序是: ";
- int m = 0;
- Node *p = mylist;
- Node *t = new Node;
- while( p->next != p)
- {
- for(int k = 1 ; k < passwd[m] ; k++ )
- {
- t = p;
- p = p->next;
- }
- cout<<p->data<<' ';
- m = p->data;
- t->next = p->next;
- delete p;
- p = p->next;
- }
- cout<<p->data<<' ';
- cout<<endl;
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/020520133111.html
来源: http://www.codesnippet.cn/detail/020520133111.html