#include #define LIVING 1 #define DEAD 0 typedef struct _node { int data; int mark; struct _node *next; } node; node *createList ( unsigned int n ) { node *head, *p; unsigned int i; if (n == 0) return NULL; p = head = (node *)malloc(sizeof(node)); p -> data = 1; p -> mark = LIVING; for (i=2; i<=n; ++i) { p -> next = (node *)malloc(sizeof(node)); p = p -> next; p -> data = i; p -> mark = LIVING; } p -> next = head; return head; } void printList ( node *head ) { node *p; printf("[ "); if (head != NULL) { if (head -> mark == LIVING) printf("%d ", head -> data); p = head -> next; while (p != head) { if (p -> mark == LIVING) printf("%d ", p -> data); p = p -> next; } } printf("]\n"); } int deleteCyclic ( node *head, unsigned int n, unsigned int m ) { node *p; unsigned int i, j; if (head == NULL) return 0; p = head; for (i=1; i mark == LIVING) ++j; p = p -> next; } while (p -> mark == DEAD) p = p -> next; p -> mark = DEAD; printf("Deleting %2d. ", p -> data); printList(head); } while (p -> mark == DEAD) p = p -> next; return p -> data; } int main () { unsigned int n, m; int survivor; node *head; printf("How many elements? "); scanf("%u",&n); printf("%u\n", n); head = createList(n); printf("Enter skip amount: "); scanf("%u", &m); printf("%u\n", m); printf("Initial list: "); printList(head); survivor = deleteCyclic(head,n,m); printf("Survivor = %d\n", survivor); }