#include <stdio.h>
#include <malloc.h>
typedef struct person {
char name[ 50 ] ;
struct person * next;
struct person * prev;
} Person;
void create( Person** head) {
Person * p,* q;
int count;
printf ( "%s" , "Number of the kids:" ) ;
* head
= ( Person
* ) malloc ( sizeof ( Person
) ) ; scanf ( "%s" , ( * head
) -> name
) ; ( * head) -> next = * head;
( * head) -> prev = * head;
p = * head;
for ( size_t i = 0 ; i < count - 1 ; i++ ) {
q
= ( Person
* ) malloc ( sizeof ( Person
) ) ; //here I am doing the links
q-> next = * head;
q-> prev = p;
( * head) -> prev = q;
p-> next = q;
p = q;
}
}
void display( Person* head) {
Person * temp = head;
do {
head = head-> next;
} while ( temp != head ) ;
}
int main( int argc, char const * argv[ ] ) {
Person * head= NULL, * q;
int number_of_syllables,
count = 0 ,
sense = 1 ; //may be either 1 , either -1
create( & head) ;
/*
Remove the first element from the Doubly Linked List
q = head;
head->next->prev = head->prev;
head->prev->next = head->next;
head = head->next;
free(q);
*/
display( head) ;
printf ( "%s:" , "Number of syllables" ) ; scanf ( "%d" , & number_of_syllables
) ;
while ( head!= head-> next) {
for ( int i = 0 ; i < number_of_syllables- 1 ; ++ i)
if ( sense + 1 ) {
head = head-> next;
} else {
head = head-> prev;
}
printf ( "%d#Out of game: %s\n " ,++ count
, head
-> name
) ;
//remake the links
q = head;
head-> prev-> next = head-> next;
head-> next-> prev = head-> prev;
if ( sense+ 1 ) {
head = head-> next;
} else {
head = head-> prev;
}
sense *= - 1 ;
}
printf ( "The winner is ... %s\n " , head
-> name
) ;
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYWxsb2MuaD4KCnR5cGVkZWYgc3RydWN0IHBlcnNvbiB7CiAgICAgICAgY2hhciBuYW1lWzUwXTsKICAgICAgICBzdHJ1Y3QgcGVyc29uICpuZXh0OwogICAgICAgIHN0cnVjdCBwZXJzb24gKnByZXY7Cn0gUGVyc29uOwoKdm9pZCBjcmVhdGUoUGVyc29uKipoZWFkKSB7CgogICAgIFBlcnNvbiAqcCwqcTsKICAgICBpbnQgY291bnQ7CgogICAgIHByaW50ZigiJXMiLCAiTnVtYmVyIG9mIHRoZSBraWRzOiIpOwogICAgIHNjYW5mKCIlZCIsICZjb3VudCk7CgogICAgICAgKmhlYWQgPSAoUGVyc29uKiltYWxsb2MoIHNpemVvZiggUGVyc29uICkgKTsKICAgICAgIHByaW50ZigiTmFtZT0iKTsKICAgICAgIHNjYW5mKCIlcyIsICgqaGVhZCktPm5hbWUpOwogICAgICAgKCpoZWFkKS0+bmV4dCA9ICpoZWFkOwogICAgICAgKCpoZWFkKS0+cHJldiA9ICpoZWFkOwogICAgICAgcCA9ICpoZWFkOwoKCiAgICAgIGZvciAoc2l6ZV90IGkgPSAwOyAgaSA8IGNvdW50IC0gMTsgaSsrKSB7CgogICAgICAgICAgIHEgPSAoUGVyc29uKiltYWxsb2MoIHNpemVvZiggUGVyc29uICkgKTsKICAgICAgICAgICBwcmludGYoIk5hbWU9Iik7CiAgICAgICAgICAgc2NhbmYoIiVzIiwgcS0+bmFtZSk7CiAgICAgICAgICAgLy9oZXJlIEkgYW0gZG9pbmcgdGhlIGxpbmtzCiAgICAgICAgICAgcS0+bmV4dCA9ICpoZWFkOwogICAgICAgICAgIHEtPnByZXYgPSBwOwogICAgICAgICAgICgqaGVhZCktPnByZXYgPSBxOwogICAgICAgICAgIHAtPm5leHQgPSBxOwogICAgICAgICAgIHAgPSBxOwogICAgICB9Cn0KCnZvaWQgZGlzcGxheShQZXJzb24qaGVhZCkgewoKICAgICBQZXJzb24gKnRlbXAgPSBoZWFkOwoKICAgICBkbyB7CgogICAgICAgICBwcmludGYoIiVzICIsIGhlYWQtPm5hbWUpOwoKICAgICAgICAgaGVhZCA9IGhlYWQtPm5leHQ7CgogICAgIH0gd2hpbGUoIHRlbXAgIT0gaGVhZCApOwp9CgppbnQgbWFpbihpbnQgYXJnYywgY2hhciBjb25zdCAqYXJndltdKSB7CgogIFBlcnNvbiAqaGVhZD1OVUx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