fork download
  1. #include <stdio.h>
  2. #include <malloc.h>
  3.  
  4. typedef struct person {
  5. char name[50];
  6. struct person *next;
  7. struct person *prev;
  8. } Person;
  9.  
  10. void create(Person**head) {
  11.  
  12. Person *p,*q;
  13. int count;
  14.  
  15. printf("%s", "Number of the kids:");
  16. scanf("%d", &count);
  17.  
  18. *head = (Person*)malloc( sizeof( Person ) );
  19. printf("Name=");
  20. scanf("%s", (*head)->name);
  21. (*head)->next = *head;
  22. (*head)->prev = *head;
  23. p = *head;
  24.  
  25.  
  26. for (size_t i = 0; i < count - 1; i++) {
  27.  
  28. q = (Person*)malloc( sizeof( Person ) );
  29. printf("Name=");
  30. scanf("%s", q->name);
  31. //here I am doing the links
  32. q->next = *head;
  33. q->prev = p;
  34. (*head)->prev = q;
  35. p->next = q;
  36. p = q;
  37. }
  38. }
  39.  
  40. void display(Person*head) {
  41.  
  42. Person *temp = head;
  43.  
  44. do {
  45.  
  46. printf("%s ", head->name);
  47.  
  48. head = head->next;
  49.  
  50. } while( temp != head );
  51. }
  52.  
  53. int main(int argc, char const *argv[]) {
  54.  
  55. Person *head=NULL, *q;
  56. int number_of_syllables,
  57. count = 0,
  58. sense = 1;//may be either 1 , either -1
  59. create(&head);
  60. /*
  61.   Remove the first element from the Doubly Linked List
  62.   q = head;
  63.   head->next->prev = head->prev;
  64.   head->prev->next = head->next;
  65.   head = head->next;
  66.   free(q);
  67.   */
  68. display(head);
  69.  
  70. printf("%s:", "Number of syllables");
  71. scanf("%d", &number_of_syllables);
  72.  
  73. while(head!=head->next) {
  74.  
  75. for(int i = 0; i < number_of_syllables-1; ++i)
  76.  
  77. if(sense + 1) {
  78. head = head->next;
  79. } else {
  80. head = head->prev;
  81. }
  82.  
  83. printf("%d#Out of game: %s\n",++count, head->name);
  84.  
  85. //remake the links
  86. q = head;
  87. head->prev->next = head->next;
  88. head->next->prev = head->prev;
  89.  
  90. if(sense+1) {
  91. head = head->next;
  92. } else {
  93. head = head->prev;
  94. }
  95.  
  96. free(q);
  97.  
  98. sense *= -1;
  99. }
  100.  
  101. printf("The winner is ... %s\n", head->name);
  102.  
  103. free(head);
  104.  
  105. return 0;
  106. }
  107.  
Success #stdin #stdout 0.01s 5408KB
stdin
5
one
two
three
four
five
2
stdout
Number of the kids:Name=Name=Name=Name=Name=one two three four five Number of syllables:1#Out of game: two
2#Out of game: one
3#Out of game: three
4#Out of game: five
The winner is ... four