双链表实现约瑟夫 大家帮忙看看有什么不足的 小白求教
#include <stdio.h>#include <stdlib.h>
typedef struct LNode
{
int num;
int passwd;
struct LNode *next;
struct LNode *prior;
}LNode,*LinkList;
LinkList Creat_L(int n)
{
LNode *head,*p,*q;
int i;
head = NULL;
for(i = 1;i <= n;i++)
{
p = (LNode *)malloc(sizeof(LNode));
scanf("%d",&p->passwd);
p->num = i;
if(head == NULL)
{
head = q = p;
}
else
{
q->next = p;
p->prior = q;
q = p;
}
p->next = head;
head->prior = p;
}
return head;
}
void Print_L(LinkList L)
{
LNode *p;
p = L;
printf("%d ",p->passwd);
p = p->next;
while(p != L)
{
printf("%d ",p->passwd);
p = p->next;
}
}
void Delete_L(LinkList L,int m,int n)
{
LNode *p,*q,*head;
int j,k = 0; //k为删除次数
p = q = head = L;
while(k < n )
{
j = 1;
while(j < m) //指向被删除的节点
{
p = p->next;
j++;
}
q = p->next;
p->prior->next = p->next;
p->next->prior = p->prior;
m = p->passwd;
printf("%d ",p->num);
free(p);
p = NULL;
p = q;
k++;
}
}
void main()
{
int n,m;
LinkList L;
scanf("%d%d",&m,&n);
L = Creat_L(n);
Delete_L(L,m,n);
}