#include"stdio.h"
#include"windows.h"
#include"malloc.h"
#include"time.h"
#define N 10
typedef struct node
{
int num ;
int sec ;
struct node *pre;
struct node *next;
}body;
void Initiate(body *x);
void Delect(int x,body *head);
void main()
{
int m;
body *first;
first=(body *)malloc(sizeof(body));
first->next=first;
first->pre=first;
Initiate(first);
m=rand()%(N-1);
m=m+1;
printf("\n任意数m=%d\n",m);
Delect(m,first);
}
void Initiate(body *head)
{
int xx[N],yy[N];
int i;
body *p,*q;
for(i=0;i<N;i++) yy[i]=i+1;
srand( time(NULL) );
for(i=0;i<N;i++) xx[i]=yy[rand()%N];
head->num=yy[0];
head->sec=xx[0];
for(i=1;i<N;i++)
{
p=head;
q=(body *)malloc(sizeof(body));
q->num=yy[i];
q->sec=xx[i];
while(p->next!=head) p=p->next;
p->next=q;
q->pre=p;
q->next=head;
head->pre=q;
}
printf("序号:");
for(i=0;i<N;i++) printf("%2d ",i+1);
printf("\n密码:");
for(i=0;i<N;i++) printf("%2d ",xx[i]);
}
void Delect(int x,body *head)
{
body *p;
p=head;
int i=1,sec;
while(i<x && p->next!=p) {p=p->next;i++;}
if(p->next!=p)
{
sec=p->sec;
printf("序号: %2d 密码: %2d \n",p->num,p->sec);
p=p->pre;
p->next=p->next->next;
p->next->pre=p;
head=p->next;
Delect(sec,head);
}
else
{
printf("序号: %2d 密码: %2d \n",p->num,p->sec);
printf("\n问题结束!\n");
}
}