/*********************************************************************
题目:有n个人围成一圈,顺序排号,从第一个开始报数
(从1到3报数),凡报到3的人退出圈子,问最后最后留下
的是原来第几号的那位.
提示:用循环链表完成
*********************************************************************/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int num;
struct node *next;
};
struct node *head=NULL;
struct node *last=NULL;
cre_list() //初始化结点
{
head = (struct node *)malloc(sizeof(struct node));
head->next = head;
last=head;
}
add_node(int num) //插入结点
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
if(head->num==0)
{
head->num=num;
}
else
{
ptr->num = num;
ptr->next = head;
last->next = ptr;
last=ptr;
}
}
display_node() //遍历链表
{
struct node *p = head;
do
{
printf("%d\t",p->num);
p = p->next;
}while(p != head);
printf("\n");
}
delete_node(int num) //删除
{
struct node *p,*q;
p = last;
q = head;
int n=1;
while(q->next!=q )
{
if(n == num)
{
p->next=q->next;
free(q);
q=p->next;
n=1;
continue;
}
q=q->next;
p=p->next;
n++;
}
}
int main()
{
int i=1;
cre_list();
for(i;i<7;i++)
{
add_node(i);
}
display_node();
delete_node(3);
display_node();
return 0;
}