
Insersión ordenada en lista enlazada simple
Publicado por Diego (150 intervenciones) el 02/10/2016 20:20:00
Buenas tardes, según lo veo no puedo asignarle NULL a un puntero, lo cual me parece extraño. Y quisiera que me dijeran porque tengo violación de acceso al intentar crear la lista.
listas.h
listas.c
prueba.c
listas.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef LISTAS_H
#define LISTAS_H
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
struct nodo {
uint64_t dni;
char *nombre;
struct nodo *psig;
};
typedef struct nodo * Lista;
void CrearLista(Lista *);
void Insertar(Lista *, uint64_t, char *);
void MostrarLista(Lista *);
#endif
listas.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "listas.h"
void CrearLista(Lista *l)
{
*l = NULL;
}
void Insertar(Lista *l, uint64_t dni, char *nombre)
{
uint64_t resul;
Lista ant, act;
Lista nue = malloc(sizeof(struct nodo) * 1);
nue->dni = dni;
nue->nombre = nombre;
nue->psig = NULL;
act = *l;
ant = *l;
resul = strcmp(act->nombre, nue->nombre);
while ((act != NULL)&&(resul <= 0)){
ant = act;
act = act->psig;
}
if (ant == act){
nue->psig = *l;
*l = nue;
}else{
ant->psig = nue;
nue->psig = act;
}
}
void MostrarLista(Lista *l)
{
Lista l_aux = *l;
while (l_aux != NULL){
printf("%" PRIu64 "\t", l_aux->dni);
printf("%s\n", l_aux->nombre);
l_aux = l_aux->psig;
}
}
prueba.c
1
2
3
4
5
6
7
8
9
10
11
12
#include "listas.h"
int main (int argc, char *argv[]){
Lista mi_lista;
CrearLista(&mi_lista);
Insertar(&mi_lista, 36117118, "DIEGO");
Insertar(&mi_lista, 92678184, "LEONOR");
Insertar(&mi_lista, 98765432, "ABELINO");
Insertar(&mi_lista, 12345678, "FELIPE");
Insertar(&mi_lista, 56478901, "SOLEDAD");
MostrarLista(&mi_lista);
return EXIT_SUCCESS;
}
Valora esta pregunta


0