用数据结构 求解 约瑟夫环问题 栈 队列

来源:百度知道 编辑:UC知道 时间:2024/05/17 22:00:24

#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;
}