循环链表的问题,急
#include<stdio.h>#include<malloc.h>
#include<stdlib.h>
struct M
{
int num;
struct M *next;
};
int main(void)
{
int i, j, n=11, t=5;
struct M *p, *q, *head;
p = (struct M*)malloc(sizeof(struct M));
printf("The number of monkeys: ");
// scanf("%d", &n);
printf("Deleted numbers: ");
// scanf("%d", &t);
for(i = 0;i <= n;i++)
{
q = (struct M*)malloc(sizeof(struct M));
q->num = i;
if(i == 0) head = q;
p->next = q;
p = q;
}
p->next = head;
p = head;
for(i = 0;i < n;i++)
{
for(j = 0;j <= t;j++) //循环两次出错?
{
q = p;
p = p->next;
}
q = p->next;
free(p);
p = q;
}
printf("The king: %d\n", p->num);
return 0;
}
帮忙改一下