/************************************************************************* > File Name: only_one.c > Author: > Mail: > Created Time: Thu 01 Nov 2018 09:21:50 AM CST ************************************************************************/ #include<stdio.h> #include<stdlib.h> typedef int ElemType; typedef struct node { ElemType data; struct node *next; }slink; void list(slink *head) { slink *p = head->next; while(p!=head) { printf("%d ",p->data); p=p->next; } printf("\n"); } void onlyone(int n) { slink *head,*p,*q; head = p = (slink *)malloc(sizeof(slink)); int i,m,k; for(i=1;i<=n;i++) { q = (slink *)malloc(sizeof(slink)); q->data = i; p->next = q; p = q; } p->next = head; m = 0; while(m<n-1) { k = 0; while(k<3) { k++; q = p; p = p->next; if(p==head) { q = p; p = p->next; } } q->next = p->next; free(p); p = q; m++; } list(head); } int main(int argc,char **argv) { int n; printf("please input n:\n"); scanf("%d",&n); onlyone(n); return 0; }