1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #include <stdio.h> #include <stdlib.h>
typedef struct Node { int data; struct Node *next; } Node;
Node *createLinkedList(int length) { Node *last, *this, *head; head = malloc(sizeof(Node)); head->data = 0; head->next = NULL; last = head; for (int i = 1; i < length; i++) { this = malloc(sizeof(Node)); this->next = NULL; this->data = 0; last->next = this; last = this; } last->next = head; return head; }
void solve(Node *head, int n,int skip) { Node *p = head; int count = 1; for (int i = 0; i < n; i++) { for (int a = 0; a < skip; a++) { p = p->next; if (p->data != 0) { a--; } } p->data = count; count++; } }
void show(Node *head){ Node *p = head; while (1) { printf("%d ",p->data); p = p->next; if (p == head) { return; }
} }
int main(int argc, char const *argv[]) { Node *linkedlist = NULL; int n = 0; scanf("%d", &n); linkedlist = createLinkedList(n); solve(linkedlist, n,3); show(linkedlist); return 0; }
|