Fiz esse algorítimo mas não consigo finaliza-lo, tenho algumas duvidas sobre teoria de lista encadeada.
Nesse código ta faltando alterar a função insere para inserir em ordem respectivamente (1,2,5,6) e mudar a estrutura para duplamente encadeada, se alguém puder ajudar agradeço
#include <stdio.h>
#include <stdlib.h>
typedef struct lista Lista;
typedef struct listano ListaNo;
struct lista {
ListaNo *prim;
};
struct listano {
int info;
float salario;
ListaNo *prox;
};
Lista *lst_cria(void) {
Lista *l = (Lista *)malloc(sizeof(Lista));
l->prim = NULL;
return l;
}
void lst_insere(Lista *l, int v) {
ListaNo *novo = (ListaNo *)malloc(sizeof(ListaNo));
novo->info = v;
novo->prox = l->prim;
l->prim = novo;
}
void lst_insereultimo(Lista *l, int v) {
ListaNo *novo = (ListaNo *)malloc(sizeof(ListaNo));
novo->info = v;
novo->prox = NULL;
if (l->prim == NULL) {
l->prim = novo;
} else {
ListaNo *p;
ListaNo *ult;
for (p = l->prim; p != NULL; p = p->prox) {
ult = p;
}
ult->prox = novo;
}
}
void lst_imprime(Lista *l) {
ListaNo *p;
for (p = l->prim; p != NULL; p = p->prox) {
printf("info = %d\n", p->info);
}
}
int pertence(Lista *l, int v) {
ListaNo *p;
for (p = l->prim; p != NULL; p = p->prox) {
if (p->info == v) {
return 1;
}
}
return 0;
}
void lst_libera(Lista *l) {
ListaNo *p = l->prim;
ListaNo *t;
while (p != NULL) {
t = p->prox;
free(p);
p = t;
}
free(l);
}
void lst_retira(Lista *l, int v) {
ListaNo *ant = NULL;
ListaNo *p = l->prim;
while (p != NULL && p->info != v) {
ant = p;
p = p->prox;
}
if (p != NULL) {
if (ant == NULL) {
l->prim = p->prox;
} else {
ant->prox = p->prox;
}
free(p);
}
}
int main() {
Lista *minha;
minha = lst_cria();
lst_insere(minha, 2);
lst_insere(minha, 6);
lst_insere(minha, 5);
lst_insere(minha, 1);
lst_imprime(minha);
printf("\n");
lst_libera(minha);
}