#include <stdio.h>
#include <stdlib.h>
struct No
{
int dado;
struct No *prox;
};
typedef struct No No;
struct lista
{
struct No *inicio;
struct No *fim;
};
typedef struct lista lista;
//----------- FUNCOES ----------//
lista* cria_lista();
void insere_inicio_lista(lista *li);
void insere_fim_lista(lista *li);
void imprime_lista(lista *li);
void esvazia_lista(lista *li);
//---------- MAIN ----------//
int main()
{
No *aux;
No *anterior;
lista *li = cria_lista();
imprime_lista(li);
insere_inicio_lista(li);
insere_inicio_lista(li);
insere_inicio_lista(li);
imprime_lista(li);
return 0;
}
//---------- FUNCOES ----------//
lista* cria_lista()
{
lista
* li
= (lista
*) malloc(sizeof(lista
)); if(li != NULL)
{
li->fim = NULL;
li->inicio = NULL;
}
return li;
}
void insere_inicio_lista(lista *li)
{
No
*novo
= (No
*) malloc(sizeof(No
)); scanf("%d", &novo
->dado
); if(li->inicio == NULL)
{
li->inicio = novo;
novo->prox = NULL;
li->fim = novo;
}
else
{
novo->prox = li->inicio;
li->inicio = novo;
}
}
void insere_fim_lista(lista *li)
{
No
*novo
= (No
*)malloc(sizeof(No
)); scanf("%d", &novo
->dado
); if(li->inicio == NULL)
{
li->inicio = novo;
novo->prox = NULL;
li->fim = novo;
}
else
{
li->fim->prox = novo;
li->fim = novo;
li->fim->prox = NULL;
}
}
void imprime_lista(lista *li) {
if(li->inicio == NULL) {
}
else {
No *aux = li->inicio;
while(aux != NULL) { //while normal aqui garante que não usa o nó com NULL
aux = aux->prox;
}
}
}
void esvazia_lista(lista *li)
{
if(li->inicio == NULL)
{
}
else
{
No *aux = li->inicio;
while(aux != NULL)
{
li->inicio = li->inicio->prox;
aux = li->inicio;
}
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnN0cnVjdCBObwp7CiAgICBpbnQgZGFkbzsKICAgIHN0cnVjdCBObyAqcHJveDsKfTsKdHlwZWRlZiBzdHJ1Y3QgTm8gTm87CgpzdHJ1Y3QgbGlzdGEKewogICAg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iLCAmbm92by0+ZGFkbyk7CiAgICBpZihsaS0+aW5pY2lvID09IE5VTEwpCiAgICB7CiAgICAgICAgbGktPmluaWNpbyA9IG5vdm87CiAgICAgICAgbm92by0+cHJveCA9IE5VTEw7CiAgICAgICAgbGktPmZpbSA9IG5vdm87CiAgICB9CiAgICBlbHNlCiAgICB7CiAgICAgICAgbm92by0+cHJveCA9IGxpLT5pbmljaW87CiAgICAgICAgbGktPmluaWNpbyA9IG5vdm87CiAgICB9Cn0KCnZvaWQgaW5zZXJlX2ZpbV9saXN0YShsaXN0YSAqbGkpCnsKICAgIE5vICpub3ZvID0gKE5vKiltYWxsb2Moc2l6ZW9mKE5vKSk7CiAgICBzY2FuZigiJWQiLCAmbm92by0+ZGFkbyk7CiAgICBpZihsaS0+aW5pY2lvID09IE5VTEwpCiAgICB7CiAgICAgICAgbGktPmluaWNpbyA9IG5vdm87CiAgICAgICAgbm92by0+cHJveCA9IE5VTEw7CiAgICAgICAgbGktPmZpbSA9IG5vdm87CiAgICB9CiAgICBlbHNlCiAgICB7CiAgICAgICAgbGktPmZpbS0+cHJveCA9IG5vdm87CiAgICAgICAgbGktPmZpbSA9IG5vdm87CiAgICAgICAgbGktPmZpbS0+cHJveCA9IE5VTEw7CiAgICB9Cn0KCnZvaWQgaW1wcmltZV9saXN0YShsaXN0YSAqbGkpIHsKICAgIGlmKGxpLT5pbmljaW8gPT0gTlVMTCkgewogICAgICAgIHByaW50ZigiTGlzdGEgdmF6aWEhIVxuIik7CiAgICB9CiAgICBlbHNlIHsKICAgICAgICBObyAqYXV4ID0gbGktPmluaWNpbzsKCiAgICAgICAgd2hpbGUoYXV4ICE9IE5VTEwpIHsgLy93aGlsZSBub3JtYWwgYXF1aSBnYXJhbnRlIHF1ZSBuw6NvIHVzYSBvIG7DsyBjb20gTlVMTAogICAgICAgICAgICBwcmludGYoIiAlZCAiLCBhdXgtPmRhZG8pOwogICAgICAgICAgICBhdXggPSBhdXgtPnByb3g7CiAgICAgICAgfQogICAgfQp9Cgp2b2lkIGVzdmF6aWFfbGlzdGEobGlzdGEgKmxpKQp7CiAgICBpZihsaS0+aW5pY2lvID09IE5VTEwpCiAgICB7CiAgICAgICAgcHJpbnRmKCJMaXN0YSB2YXppYSEhXG4iKTsKICAgIH0KICAgIGVsc2UKICAgIHsKICAgICAgICBObyAqYXV4ID0gbGktPmluaWNpbzsKICAgICAgICB3aGlsZShhdXggIT0gTlVMTCkKICAgICAgICB7CiAgICAgICAgICAgIGxpLT5pbmljaW8gPSBsaS0+aW5pY2lvLT5wcm94OwogICAgICAgICAgICBmcmVlKGF1eCk7CiAgICAgICAgICAgIGF1eCA9IGxpLT5pbmljaW87CiAgICAgICAgfQogICAgICAgIHByaW50ZigiTGlzdGEgZXN2YXppYWRhIVxuIik7CiAgICB9Cn0K