Gestion d'une liste doublement chainee en C
Classé dans Sciences humaines et sociales
Écrit le en
français avec une taille de 5,65 KB
Gestion d'une liste doublement chaînée en C
Ce code présente l'implémentation et la manipulation d'une liste doublement chaînée en langage C (chargement, insertion ordonnée, recherche, modification, affichage, suppression et tri).
Structure des données
struct datos {
int num;
struct datos * prox;
struct datos * ant;
} *pri;Menu interactif
int Menu() {
int opt;
system("cls");
printf("********ELEGIR OPCION*******\n");
printf("1.- Cargar\n");
printf("2.- Insertar\n");
printf("3.- Modificar\n");
printf("4.- Borrar\n");
printf("5.- Buscar\n");
printf("6.- Listar\n");
printf("0.- Salir\n\n");
printf("Opcion: ");
scanf("%d", &opt);
return (opt);
}Fonction Cargar
void Cargar() {
struct datos * aux, * aux2;
char opt;
while(opt != 's') {
if(pri == NULL) {
pri = (struct datos *) malloc(sizeof(struct datos));
pri->prox = NULL;
pri->ant = NULL;
aux = pri;
} else { // SE AGREGA DESPUES DEL PRI
aux = (struct datos *) malloc(sizeof(struct datos));
aux2->prox = aux;
aux->ant = aux2;
}
printf("Ingrese Numero: ");
fflush(stdin);
scanf("%d", &aux->num);
aux2 = aux;
aux->prox = NULL;
printf("Desea Salir (S/N): ");
fflush(stdin);
scanf("%c", &opt);
}
}Fonction Insertar
struct datos * Insertar() {
struct datos * aux, * aux2 = pri;
aux = (struct datos *) malloc(sizeof(struct datos));
printf("Ingrese Numero: ");
fflush(stdin);
scanf("%d", &aux->num);
if(pri != NULL) {
if(pri->num > aux->num) {
aux->prox = pri;
aux->ant = NULL;
pri->ant = aux;
pri = aux;
} else {
aux2 = pri;
while(aux2->prox != NULL && (aux2->num < aux->num))
aux2 = aux2->prox;
if(aux->num < aux2->num) {
aux->ant = aux2->ant;
aux2->ant->prox = aux;
aux->prox = aux2;
aux2->ant = aux;
} else {
aux2->prox = aux;
aux->ant = aux2;
aux->prox = NULL;
}
}
return (pri);
}
}Fonction Buscar
struct datos * Buscar(int num_busq, int show) {
struct datos * aux2;
aux2 = pri;
while(aux2 != NULL) {
if(aux2->num == num_busq) {
if(show != 0) {
printf("num:%d\nprox:%x\nant:%x\n", aux2->num, aux2->prox, aux2->ant);
system("pause");
}
return (aux2);
}
aux2 = aux2->prox;
}
printf("\nNO EXISTE ESE DATO\n\n");
system("pause");
return (NULL);
}Fonction Modificar
void Modificar(int num_busq) {
struct datos *aux;
aux = Buscar(num_busq, 0);
if(aux) {
printf("Nuevo valor: ");
fflush(stdin);
scanf("%d", &aux->num);
}
}Fonction Listar
void Listar() {
struct datos * aux2;
aux2 = pri;
while(aux2 != NULL) {
printf("%d\n", aux2->num);
aux2 = aux2->prox;
}
fflush(stdin);
getchar();
}Fonction Eliminar
void Eliminar(int num_busq) {
struct datos *aux;
aux = pri;
while(aux != NULL && num_busq != aux->num)
aux = aux->prox;
if(aux == NULL) {
printf("\nNO EXISTE ESE DATO\n");
system("pause");
} else { // SI EXISTE...
if(aux == pri) {
pri = pri->prox;
pri->ant = NULL;
} else {
aux->ant->prox = aux->prox;
if(aux->prox != NULL)
aux->prox->ant = aux->ant;
}
free(aux);
}
}Fonction Ordenar
void Ordenar() {
int cant;
struct datos *aux, *aux2;
aux = pri;
while(aux->prox != NULL) {
cant++;
aux = aux->prox;
}
aux = pri;
for(int i = 0; i < cant; i++) {
aux = pri;
aux2 = aux->prox;
while(aux2 != NULL) {
if(aux->num > aux2->num && aux2->prox != NULL) {
if(aux == pri) {
aux2->ant = NULL;
aux->prox = aux2->prox;
aux2->prox = aux;
aux->ant = aux2;
aux->prox->ant = aux;
pri = aux2;
aux2 = aux->prox;
} else {
aux->prox = aux2->prox;
aux2->prox = aux;
aux->prox->ant = aux;
aux2->ant = aux->ant;
aux->ant = aux2;
aux2->ant->prox = aux2;
}
aux2 = aux->prox;
} else {
if(aux2->prox == NULL && aux->num > aux2->num) {
aux->prox = NULL;
aux2->ant = aux->ant;
aux2->ant->prox = aux2;
aux->ant = aux2;
aux2->prox = aux;
break;
} else {
aux = aux->prox;
aux2 = aux->prox;
}
}
}
}
}