#include<stdio.h>
struct node
{
int data;
struct node *next;
};
void display_link(struct node *head)
{
if(head == NULL)
return;
struct node *p = head->next;
do
{
printf("%3d", p->data);
p = p->next;
} while(p != head->next);
printf("%3d", p->data);
printf("\n");
}
int main()
{
int i;
struct node *p = NULL;
struct node *head = NULL;
head = (struct node *) malloc(sizeof(struct node));
head->data = -1;
head ->next = NULL;
printf("input p:");
for(i = 0; i < 30; i++)
{
p = (struct node *) malloc (sizeof (struct node));
scanf("%d",&(p->data));
p->next = head->next;
head->next = p;
}
struct node *q = head->next;
while(p != NULL)
{
q = p;
p = p->next;
}
q->next = head->next;
//display_link(head);
int num = 0;
struct node *a = head->next;
p = head->next;
while ( num < 15)
{
for(i=0 ; i<2 ; i++ )
{
a = p;
p = p->next;
}
printf("%d\n",p->data);
a->next = p->next;
p = a->next;
a = p;
num++;
}
}
约瑟夫问题(本人使用链表解决的)
最新推荐文章于 2023-04-19 19:48:10 发布