#include "stdafx.h" typedef struct ficha { int edad; float peso; char educacion; char nombre[40]; } tficha; /*definición de un tipo estructural para manipulaciones con listas doblemente enlazadas*/ struct elemento_de_lista { tficha info; struct elemento_de_lista * ANT; struct elemento_de_lista * SIG; }; /*'regisro' será un alias para el tipo anterior*/ typedef struct elemento_de_lista registro; char genletra2() { return 'a' + rand() % 26; } void gen_tficha(tficha * ptficha) { ptficha->edad = rand() % 120; ptficha->peso = 1 + rand() % 300; int w = rand() % 6; switch (w) { case 0: ptficha->educacion = 'N'; break; case 1: ptficha->educacion = 'S'; break; case 2: ptficha->educacion = 'P'; break; case 3: ptficha->educacion = 'L'; break; case 4: ptficha->educacion = 'M'; break; default: ptficha->educacion = 'D'; break; } int lon = rand() % 30, i; for (i = 0; inombre[i] = genletra2(); ptficha->nombre[i] = '\0'; } void impr_tficha(tficha * ptficha) { printf("\n edad %d \n", ptficha->edad); printf("peso %g \n", ptficha->peso); printf("educacion %c \n", ptficha->educacion); puts(ptficha->nombre); } /*generar un 'registro' inicializando campos ANT y SIG a NULL */ registro* ini_lista() { registro*nuevo = // 'nuevo' apuntará a la memoria para un 'registro'… (registro *)malloc(sizeof(registro)); //… prestada por sistema operativo gen_tficha(&(nuevo->info)); nuevo->ANT = NULL; nuevo->SIG = NULL; impr_tficha(&nuevo->info); return nuevo; } void copy_tficha(tficha *origen, tficha *destino) { int sz = sizeof(tficha), i; //sizeof(*origen); char *f = (char*)origen, *f1 = (char*)destino; for (i = 0; iinfo)); copy_tficha(origen, &(nuevo->info)); nuevo->ANT = NULL; nuevo->SIG = NULL; //impr_tficha(&nuevo->info); return nuevo; } /*a partir de un elemento de lista, encontrar su primer elemento*/ registro* dame_cabeza(registro* un_elem) { while (un_elem->ANT != NULL) un_elem = un_elem->ANT; return un_elem; } registro* intro_nuevo_de_otra_lista_antes_cabeza (registro* un_elem, registro* fuente) { registro* cab = dame_cabeza(un_elem); registro *nuevo = /*ini_lista();*/ini_lista_de_lista(&fuente->info); nuevo->SIG = cab; cab->ANT = nuevo; return nuevo; } /*a partir de un elemento de lista, encontrar su último elemento*/ registro* dame_cola(registro* un_elem) { while (un_elem->SIG != NULL) un_elem = un_elem->SIG; return un_elem; } /*generar nuevo elemento de lista y insertarlo antes del 1r elemento*/ registro* intro_nuevo_antes_cabeza (registro* un_elem) { registro* cab = dame_cabeza(un_elem); registro *nuevo = ini_lista(); nuevo->SIG = cab; cab->ANT = nuevo; return nuevo; } void impr_lista_en_orden_natural(registro* un_elem) { un_elem = dame_cabeza(un_elem); do { impr_tficha(&un_elem->info); un_elem = un_elem->SIG; } while (un_elem != NULL); }; char comparar_cad(char array_1[], char array_2[]) { int i = 0; while (array_1[i] != '\0' && array_2[i] != '\0') if (array_1[i] == array_2[i]) i++; else return array_1[i] > array_2[i] ? '>' : '<'; return array_1[i] > array_2[i] ? '>' : (array_1[i] == array_2[i] ? '=' : '<'); } registro* intercam_reg(registro* pr) { if (pr == NULL) return pr; //pr significa "primer registro" if (pr->SIG == NULL) return pr; registro* seg = pr->SIG, *pr_ant = pr->ANT, *ter = seg->SIG; if (pr_ant != NULL) pr_ant->SIG = seg; pr->ANT = seg; pr->SIG = seg->SIG; seg->ANT = pr_ant; seg->SIG = pr; if (ter != NULL) ter->ANT = pr; return seg; } registro* orden_lista_nombre(registro* un_elem) { registro* pr, *sig; bool desorden; do { desorden = false; int i; pr = dame_cabeza(un_elem); while (pr != NULL) { sig = pr->SIG; if (sig != NULL) { if (comparar_cad(pr->info.nombre, sig->info.nombre) == '>') { desorden = true; pr = intercam_reg(pr); } } else break; pr = pr->SIG; } } while (desorden); return un_elem; } registro * desenlazar(registro * reg) { if (reg->ANT != NULL) { reg->ANT->SIG = reg->SIG; //reasignar seguidor del // reg->ANT al seguidor de reg if (reg->SIG != NULL) reg->SIG->ANT = reg->ANT; return reg->ANT->SIG != NULL ? reg->ANT->SIG : reg->ANT; } else {//reg es 1r elemento de lista if (reg->SIG == NULL) return NULL; //reg es ultimo else { reg->SIG->ANT = NULL; /*asignar seguidor del reg como nuevo 1r el de lista*/ return reg->SIG; /*regresar seguidor del reg como ancla a lista sin reg*/ } } } registro *filtrar(registro *lista) { registro *aux = lista, *aux2; aux = dame_cabeza(aux); /*si Educación NO sea 'L', o 'M', o 'D'. o Edad NO sea mayor que 25 pero menor que 55 años desenlazar registro corriente y avanzar al siguiente */ while (aux != NULL) { aux2 = aux; if (/*condicion de eliminar aux*/ aux->info.educacion != 'L'&& aux->info.educacion != 'M'&& aux->info.educacion != 'D' || aux->info.edad <= 25 || aux->info.edad >= 55 ) { aux = desenlazar(aux); } else aux = aux2->SIG; } return aux2; } int contar_num_reg_lista(registro*un_elem) { un_elem = dame_cabeza(un_elem); int num = 1; while (un_elem->SIG != NULL) { num++; un_elem = un_elem->SIG; } return num; } void fgrabar_tficha(tficha *origen, FILE*pf) { int sz = sizeof(tficha), i; char *f = (char*)origen; for (i = 0; iinfo); lista = dame_cabeza(lista); while (lista->SIG != NULL) { lista = lista->SIG; lista2 = intro_nuevo_de_otra_lista_antes_cabeza (lista2, lista); } /*la lista2 anterior ahora incluye todos regisros de lista; Ahora vamos a filtrarla*/ lista2 = filtrar(lista2); puts("\n Lista filtrada\n"); impr_lista_en_orden_natural(lista2); int numel = contar_num_reg_lista(lista2), numel1; FILE *pf; //grabar fopen_s(&pf, "lista", "w"); fprintf(pf, "%d\n", numel); lista2 = dame_cabeza(lista2); fgrabar_tficha(&lista2->info, pf); fclose(pf); //leer fopen_s(&pf, "lista", "r"); if (pf == NULL) { puts("no se pudo abrir archivo para lectura"); return 1; } int n; fscanf(pf, "%d\n", &numel1); tficha tf; fleer_tficha(&tf, pf); fclose(pf); // cerrar el archivo printf("\nnum registros recuperado desde archivo %d\n", numel1); puts("\ntficha recuperada\n"); impr_tficha(&tf); return 0; }