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

implementa insere ordenado

parent f1ef5480
Branches
No related tags found
No related merge requests found
......@@ -9,7 +9,6 @@ int inicializa_lista(t_lista *l)
/* Insert beginning sentinel */
sentinel = (t_nodo*) malloc (sizeof (t_nodo));
if (sentinel == NULL)
return 0;
......@@ -18,7 +17,6 @@ int inicializa_lista(t_lista *l)
/* Insert end sentinel */
sentinel = (t_nodo*) malloc (sizeof (t_nodo));
if (sentinel == NULL)
return 0;
......@@ -60,10 +58,18 @@ void destroi_lista(t_lista *l)
l->tamanho = 0;
}
int tamanho_lista(int *tam, t_lista *l)
{
if (l->ini == NULL && l->fim == NULL)
return 0;
*tam = l->tamanho;
return 1;
}
int insere_inicio_lista(int item, t_lista *l)
{
t_nodo *new;
new = (t_nodo*) malloc (sizeof (t_nodo));
if (new == NULL)
return 0;
......@@ -80,19 +86,9 @@ int insere_inicio_lista(int item, t_lista *l)
return 1;
}
int tamanho_lista(int *tam, t_lista *l)
{
if (l->ini == NULL && l->fim == NULL)
return 0;
*tam = l->tamanho;
return 1;
}
int insere_fim_lista(int item, t_lista *l)
{
t_nodo *new;
new = (t_nodo*) malloc (sizeof (t_nodo));
if (new == NULL)
return 0;
......@@ -109,18 +105,56 @@ int insere_fim_lista(int item, t_lista *l)
return 1;
}
/*
Insere o elemento item na lista de maneira que ela fique em ordem
crescente, do início para o final dela.
Retorna 1 se a operação foi bem sucedida e zero caso contrário.
*/
int insere_ordenado_lista(int item, t_lista *l){return 1;}
int insere_ordenado_lista(int item, t_lista *l)
{
if (lista_vazia(l))
return insere_inicio_lista (item, l);
t_nodo *new;
new = (t_nodo*) malloc (sizeof (t_nodo));
if (new == NULL)
return 0;
new->chave = item;
t_nodo *p = l->ini->prox;
while (p->prox != NULL && p->chave < item)
p = p->prox;
new->prox = p;
new->prev = p->prev;
p->prev->prox = new;
p->prev = new;
l->tamanho++;
return 1;
}
/*
Remove o primeiro elemento da lista e o retorna em *item.
Retorna 1 se a operação foi bem sucedida e zero caso contrário.
*/
int remove_inicio_lista(int *item, t_lista *l){return 1;}
int remove_inicio_lista(int *item, t_lista *l)
{
if (lista_vazia(l))
{
item = NULL
return 0;
}
t_nodo *p = l->ini->prox;
l->ini = p->prox;
p->prox->prev = l->ini
free (p);
l->tamanho--;
return 1;
}
/*
Remove o último elemento da lista e o retorna em *item.
......
......@@ -2,19 +2,32 @@
#include "lib_lista.h"
void imprime (t_lista *l)
{
t_nodo *p = l->ini->prox;
while (p->prox->prox != NULL)
{
printf ("%d ",p->chave);
p = p->prox;
}
printf ("%d\n",p->chave);
}
int main ()
{
t_lista l;
int tam;
inicializa_lista(&l);
insere_inicio_lista (10, &l);
insere_inicio_lista (5, &l);
tamanho_lista(&tam, &l);
printf ("tam: %d\n",tam);
insere_fim_lista (8, &l);
tamanho_lista(&tam, &l);
printf ("tam: %d\n",tam);
insere_ordenado_lista(7, &l);
insere_ordenado_lista(10, &l);
insere_ordenado_lista(1, &l);
insere_ordenado_lista(3, &l);
insere_ordenado_lista(2, &l);
imprime (&l);
remove_inicio_lista(&l);
imprime (&l);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment