#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef struct str {
char * child;
struct str* next;
} NO;
typedef struct {
NO* head;
} LIST;
int SizeList( LIST * l) {
NO* p = l-> head-> next;
int tam = 0 ;
while ( p != l-> head)
{
tam++;
p = p-> next;
}
return ( tam) ;
}
void showList( LIST * l) {
NO* p = l-> head;
while ( p-> next != l-> head)
{
p = p-> next;
}
if ( p
-> next
== l
-> head
) printf ( "head \n " ) ; }
void IniList( LIST* l) {
l
-> head
= ( NO
* ) malloc ( sizeof ( NO
) ) ; l-> head-> next = l-> head;
//assim mesmo que imprima o valor do head não estara a aceder a uma zona de memoria invalida
l
-> head
-> child
= malloc ( sizeof ( char ) ) ; * ( l-> head-> child) = '\0 ' ;
}
NO* nChild( LIST * l, int n, NO** bef) {
NO* p = l-> head-> next;
* bef = l-> head; //iniciliazação que estava em falta de bef
int i = 0 ;
while ( ( p != l-> head) && ( i < n) ) //aqui de n-1 para n
{
* bef = p;
p = p-> next;
i++;
}
return i >= SizeList( l) ? NULL : p; //troquei if para ternario assim como condição
}
NO* LastChild( LIST * l) {
NO* p = l-> head-> next;
if ( p == l-> head) return l-> head;
while ( p-> next != l-> head)
{
p = p-> next;
}
return p;
}
void insChild( LIST* l, char name[ ] ) { //simplifiquei bastante o código
NO* p = LastChild( l) ;
NO
* newnode
= ( NO
* ) malloc ( sizeof ( NO
) ) ; //conjuguei declaração com atribuição newnode-> next = l-> head;
p-> next = newnode;
}
void DelChild ( LIST* l)
{
int slist = SizeList( l) ;
if ( slist == 1 )
{
NO* p = l-> head-> next;
printf ( "\n Criança Vencedora: %s\n " , p
-> child
) ; return ;
}
else
{
int del
= rand ( ) % slist
; //de slist-1 para slist
NO* bef;
NO* p = nChild( l, del, & bef) ;
if ( p) //retirei o if(!p) return; que assim fica mais simples
{
bef-> next = p-> next;
}
}
}
int main( ) {
LIST l;
IniList( & l) ;
int op = 0 ;
while ( op != 3 )
{
printf ( "\n Digite o numero a operacao desejada:\n " ) ; printf ( "\n \n 1.Inserir Crianca \t \t 2.Remover Crianca \t \t 3.Exit\n \n " ) ;
char name[ 100 ] ;
switch ( op)
{
case 1 :
printf ( "\n Digite o nome a inserir:\n " ) ; insChild( & l, name) ;
showList( & l) ;
break ;
case 2 :
DelChild( & l) ;
showList( & l) ;
break ;
case 3 :
break ;
default :
printf ( "Digite uma opção válida!\n " ) ; }
}
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0ZGJvb2wuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKdHlwZWRlZiBzdHJ1Y3Qgc3RyIHsKICAgIGNoYXIqIGNoaWxkOwogICAgc3RydWN0IHN0ciogbmV4dDsKfSBOTzsKCnR5cGVkZWYgc3RydWN0IHsKICAgIE5PKiBoZWFkOwp9IExJU1Q7CgoKCmludCBTaXplTGlzdChMSVNUICpsKSB7CiAgICBOTyogcCA9IGwtPmhlYWQtPm5leHQ7CiAgICBpbnQgdGFtID0gMDsKICAgIHdoaWxlIChwICE9IGwtPmhlYWQpCiAgICB7CiAgICAgICAgdGFtKys7CiAgICAgICAgcCA9IHAtPm5leHQ7CiAgICB9CiAgICByZXR1cm4odGFtKTsKfQoKdm9pZCBzaG93TGlzdChMSVNUICpsKSB7CiAgICBOTyogcCA9IGwtPmhlYWQ7CiAgICBwcmludGYoImhlYWQtPiIpOwogICAgd2hpbGUocC0+bmV4dCAhPSBsLT5oZWFkKQogICAgewogICAgICAgIHAgPSBwLT5uZXh0OwogICAgICAgIHByaW50ZigiJXMtPiIsIHAtPmNoaWxkKTsKICAgIH0KICAgIGlmKHAtPm5leHQgPT0gbC0+aGVhZCkgcHJpbnRmKCJoZWFkIFxuIik7CiAgICBwcmludGYoIlxuXG4iKTsKfQoKdm9pZCBJbmlMaXN0KExJU1QqIGwpIHsKICAgIGwtPmhlYWQgPSAoTk8qKW1hbGxvYyhzaXplb2YoTk8pKTsKICAgI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sZChsKTsKICAgIE5PKiBuZXdub2RlID0gKE5PKiltYWxsb2Moc2l6ZW9mKE5PKSk7IC8vY29uanVndWVpIGRlY2xhcmHDp8OjbyBjb20gYXRyaWJ1acOnw6NvCiAgICBuZXdub2RlLT5jaGlsZCA9IG1hbGxvYygoc3RybGVuKG5hbWUpKzEpKnNpemVvZihjaGFyKSk7IAogICAgc3RyY3B5KG5ld25vZGUtPmNoaWxkLCBuYW1lKTsKICAgIG5ld25vZGUtPm5leHQgPSBsLT5oZWFkOwogICAgcC0+bmV4dCA9IG5ld25vZGU7Cn0KCnZvaWQgRGVsQ2hpbGQgKExJU1QqIGwpCnsKICAgIGludCBzbGlzdCA9IFNpemVMaXN0KGwpOwogICAgaWYgKHNsaXN0ID09IDEgKQogICAgewogICAgICAgIE5PKiBwID0gbC0+aGVhZC0+bmV4dDsKICAgICAgICBwcmludGYoIlxuQ3JpYW7Dp2EgVmVuY2Vkb3JhOiAlc1xuIiwgcC0+Y2hpbGQpOwogICAgICAgIHJldHVybjsKICAgIH0KICAgIGVsc2UKICAgIHsKICAgICAgICBpbnQgZGVsID0gcmFuZCgpICUgc2xpc3Q7IC8vZGUgc2xpc3QtMSBwYXJhIHNsaXN0CiAgICAgICAgcHJpbnRmKCJSQU5ET006ICVkXG4iLCBkZWwpOwoKICAgICAgICBOTyogYmVmOwogICAgICAgIE5PKiBwID0gbkNoaWxkKGwsIGRlbCwgJmJlZik7CiAgICAgICAgaWYgKHApLy9yZXRpcmVpIG8gaWYoIXApIHJldHVybjsgcXVlIGFzc2ltIGZpY2EgbWFpcyBzaW1wbGVzCiAgICAgICAgewogICAgICAgICAgICBiZWYtPm5leHQgPSBwLT5uZXh0OwogICAgICAgICAgICBmcmVlKHApOwogICAgICAgIH0gCiAgICB9Cn0KCmludCBtYWluKCkgewogICAgTElTVCBsOwogICAgSW5pTGlzdCgmbCk7CiAgICBpbnQgb3AgPSAwOwoKICAgIHdoaWxlIChvcCAhPSAzKQogICAgewogICAgICAgIHByaW50ZigiXG5EaWdpdGUgbyBudW1lcm8gYSBvcGVyYWNhbyBkZXNlamFkYTpcbiIpOwogICAgICAgIHByaW50ZigiXG5cbjEuSW5zZXJpciBDcmlhbmNhIFx0XHQyLlJlbW92ZXIgQ3JpYW5jYSBcdFx0My5FeGl0XG5cbiIpOwogICAgICAgIHNjYW5mKCIlZCIsICZvcCk7CgogICAgICAgIGNoYXIgbmFtZVsxMDBdOwogICAgICAgIHN3aXRjaChvcCkKICAgICAgICB7CgogICAgICAgIGNhc2UgMToKICAgICAgICAgICAgcHJpbnRmKCJcbkRpZ2l0ZSBvIG5vbWUgYSBpbnNlcmlyOlxuIik7CiAgICAgICAgICAgIHNjYW5mKCIlcyIsIG5hbWUpOwogICAgICAgICAgICBpbnNDaGlsZCgmbCwgbmFtZSk7CiAgICAgICAgICAgIHNob3dMaXN0KCZsKTsKICAgICAgICAgICAgYnJlYWs7CgogICAgICAgIGNhc2UgMjoKICAgICAgICAgICAgRGVsQ2hpbGQoJmwpOwogICAgICAgICAgICBzaG93TGlzdCgmbCk7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIGNhc2UgMzoKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgcHJpbnRmKCJEaWdpdGUgdW1hIG9ww6fDo28gdsOhbGlkYSFcbiIpOwogICAgICAgIH0KICAgIH0KCiAgICByZXR1cm4gMDsKfQo=