Skip to content
Snippets Groups Projects
Commit 2cb37827 authored by Leonardo Krambeck's avatar Leonardo Krambeck
Browse files

implementa ordena e arruma bug que estava em inicializa_lista

parent b4486c15
Branches
No related tags found
No related merge requests found
......@@ -11,18 +11,22 @@ int inicializa_lista(t_lista *l)
if (sentinel == NULL)
return 0;
sentinel->prox = NULL;
sentinel->prev = l->ini;
l->ini = sentinel;
sentinel = (t_nodo*) malloc (sizeof (t_nodo));
if (sentinel == NULL)
return 0;
l->ini->prox = sentinel;
sentinel->prox = NULL;
sentinel->prev = l->ini;
l->ini->prox = sentinel;
l->fim = sentinel;
l->atual = NULL;
l->atual = NULL;
l->tamanho = 0;
return 1;
......@@ -38,11 +42,8 @@ int lista_vazia(t_lista *l)
void destroi_lista(t_lista *l)
{
if (l->ini == NULL && l->fim == NULL && l->tamanho == 0)
{
printf ("This list is already destroyed.\n");
if (l->ini == NULL && l->atual == NULL && l->fim == NULL && l->tamanho == 0)
return;
}
t_nodo *p;
......
......@@ -54,19 +54,11 @@ int concatena_listas(t_lista *l, t_lista *c)
{
int item;
if (!inicializa_atual_inicio(c))
return 0;
while (consulta_item_atual(&item, c))
{
while (remove_inicio_lista(&item, c))
if (!insere_fim_lista (item, l))
return 0;
incrementa_atual(c);
}
/* BUG
destroi_lista(c);
*/
return 1;
}
......@@ -74,7 +66,24 @@ int concatena_listas(t_lista *l, t_lista *c)
Ordena a lista l em ordem crescente.
Retorna 1 se a operação foi bem sucedida e zero caso contrário.
*/
int ordena_lista(t_lista *l){return 1;}
int ordena_lista(t_lista *l)
{
t_lista aux;
if (!inicializa_lista(&aux))
return 0;
int item;
while (remove_fim_lista(&item, l))
if (!insere_ordenado_lista(item, &aux))
return 0;
if (!copia_lista(&aux, l))
return 0;
destroi_lista(&aux);
return 1;
}
/*
Funcao que cria uma nova lista i pela intercalacao dos elementos
......
......@@ -8,24 +8,40 @@ int main()
inicializa_lista(&l);
inicializa_lista(&u);
printf("insere inicio e fim:\n");
insere_inicio_lista(10,&l);
insere_inicio_lista(20,&l);
insere_inicio_lista(5,&l);
insere_fim_lista(8,&l);
insere_fim_lista(12,&l);
insere_fim_lista(32,&l);
printf ("lista l: ");
imprime_lista(&l);
printf("\n");
printf("\n\n");
printf("copia:\n");
copia_lista(&l, &u);
printf ("lista u: ");
imprime_lista(&u);
printf("\n");
printf("\n\n");
printf("concatena:\n");
concatena_listas(&l, &u);
printf ("lista l: ");
imprime_lista(&l);
printf("\n");
printf ("lista u: ");
imprime_lista(&u);
printf("\n\n");
printf("ordena:\n");
ordena_lista(&l);
printf ("lista l: ");
imprime_lista(&l);
printf("\n");
destroi_lista(&l);
destroi_lista(&u);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment