嘉庆阳光物业刘娟照片:用数据结构 求解 约瑟夫环问题 栈 队列

来源:百度文库 编辑:高校问答 时间:2024/05/01 03:35:07

#include<stdio.h>
#include<stdlib.h>
#include<alloc.h>
struct node
{
int number;
int cipher;
struct node* next;
};
struct node * CreatList(int num)
{
int i;
struct node *ptr1,*head;
if((ptr1=(struct node*)malloc(sizeof(struct node)))==NULL)
{
perror("malloc");
return ptr1;
}
head=ptr1;
ptr1->next=head;
for(i=1;i<num;i++)
{
if((ptr1->next=(struct node*)malloc(sizeof(struct node)))==NULL)
{
perror("malloc");
ptr1->next=head;
return head;
}
ptr1=ptr1->next;
ptr1->next=head;
}
return head;
}
void main()
{
int i,n=30,m;
struct node*head,*ptr;
randomize();
head=CreatList(n);
for(i=1;i<=30;i++)
{
head->number=i;
head->cipher=rand();
head=head->next;
}
m=rand();
i=0;
while(head->next!=head)
{
if(i==m)
{
ptr=head->next;
printf("number:%d\n",ptr->number);
printf("cipher:%d\n",ptr->cipher);
m=ptr->cipher;
head->next=ptr->next;
head=head->next;
free(ptr);
i=0;
}
else
{
head=head->next;
i++;
}
}
printf("number:%d\n",head->number);
printf("cipher:%d\n",head->cipher);
free(head);
}