⒈建立一个具有n个链结点,无头结点的循环链表; ⒉确定第1个报数人的位置; ⒊不断地从链表中删除链结点,直到链表为空。
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int item;
struct node *next;
}node_t;
node_t *mk_node(int n)
{
node_t *p = (node_t*)malloc(sizeof(node_t));
if(p!=NULL){
p->item = n;
p->next = p;
}
return p;
}
node_t *insert_tail(node_t *head,int n)
{
node_t *newp = mk_node(n);
node_t *tail;
tail = head;
if(!head||!newp){
if(!head)
head = newp;
return head;
}
if(tail->next == head)
{
tail->next = newp;
newp->next = head;
tail=tail->next;
return head;
}
else{
for(tail = head; tail->next!= head;tail = tail->next)
;
tail->next=newp;
tail = newp;
tail->next = head;
return head;
}
}
node_t * link_delete(node_t **headp,int n)
{
node_t *cur,*tail,*p;
tail = *(headp);
cur = *(headp);
int i;
for(i = 0;i < n; i ++,p =cur, cur = cur->next)
;
p->next = cur->next;
}
void link_print(node_t *head)
{
node_t *cur,*tail;
cur = head;
tail = head;
printf("%d",cur->item);
for(cur = head->next;cur!=tail;cur = cur->next)
printf("%d",cur->item);
printf("\n");
}
int main(void)
{
node_t *head = NULL;
int n;
while(1){
scanf("%d",&n);
if(n==0)
break;
head = insert_tail(head,n);
}
link_print(head);
printf("shu ru n:");
while(1){
scanf("%d",&n);
if(head->next == head)
break;
head = link_delete(&head,n);
link_print(head);
}
printf("最后剩:%d",head->item);
return 0;
}