Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define T 20
- struct listNode {
- char nombre[T];
- int edad;
- int matricula;
- struct listNode *nextP;
- };
- typedef struct listNode LISTNODE;
- typedef LISTNODE *LISTNODEP;
- void inserta(LISTNODEP *, char[], int, int);
- int elimina(LISTNODEP *, char[], int, int);
- int esVacia(LISTNODEP);
- void imprimeLista(LISTNODEP);
- void menu(void);
- int esVacia(LISTNODEP sP) {
- return (sP==NULL);
- }
- void imprimeLista(LISTNODEP actualP) {
- if(actualP == NULL) {
- printf("\n Lista vacia");
- }
- else {
- printf("\n La lista es: \n");
- while(actualP != NULL) {
- printf("\nNombre: %s", actualP->nombre);
- printf("\nEdad: %d", actualP->edad);
- printf("\nMatricula: %d", actualP->matricula);
- actualP = actualP->nextP;
- }
- }
- }
- void menu(void) {
- printf("\nMENU: \n");
- printf("\n1. Inserta un elemento en la lista \n");
- printf("\n2. Borra un elemento de la lista \n");
- printf("\n3. Terminar \n");
- }
- void inserta(LISTNODEP *sP, char value[], int value2, int value3) {
- LISTNODEP nuevoP, anteriorP, actualP;
- nuevoP = (LISTNODEP) malloc (sizeof(LISTNODE));
- if(nuevoP != NULL) {
- //nuevoP->nombre = value;
- strcpy(nuevoP->nombre, value);
- nuevoP->edad = value2;
- nuevoP->matricula = value3;
- nuevoP->nextP = NULL;
- anteriorP = NULL;
- actualP = *sP;
- while(actualP != NULL && value>actualP->nombre && value2>actualP->edad && value3>actualP->matricula) {
- anteriorP = actualP;
- actualP = actualP->nextP;
- }
- if(anteriorP == NULL) {
- nuevoP->nextP = *sP;
- *sP = nuevoP;
- }
- else {
- anteriorP->nextP=nuevoP;
- nuevoP->nextP = actualP;
- }
- }
- else {
- printf("\n %s %d %d no se inserto. No hay memoria", value, value2, value3);
- }
- }
- int elimina(LISTNODEP *sP, char value[], int value2, int value3) {
- LISTNODEP anteriorP, actualP, tempP;
- if(value == (*sP)->nombre && value2 == (*sP)->edad && value3 == (*sP)->matricula) {
- tempP = *sP;
- *sP = (*sP)->nextP;
- free(tempP);
- return value, value2,value3;
- }
- else {
- anteriorP = *sP;
- actualP = (*sP)->nextP;
- while(actualP != NULL && actualP->nombre != value && actualP->edad != value2 && actualP->matricula != value3) {
- anteriorP = actualP;
- actualP = actualP->nextP;
- }
- if(actualP != NULL) {
- tempP = actualP;
- anteriorP->nextP = actualP->nextP;
- free(tempP);
- return value, value2,value3;
- }
- }
- }
- int main() {
- LISTNODEP startP = NULL;
- int op;
- char item;
- int item2, item3;
- menu();
- printf("\nEscoge una opcion: \n");
- scanf(" %d", &op);
- while(op!=3) {
- switch (op) {
- case 1:
- printf("\nIngresa nombre: \n");
- scanf(" %s", &item);
- printf("\nIngresa edad: \n");
- scanf(" %d", &item2);
- printf("\nIngresa matricula: \n");
- scanf(" %d", &item3);
- inserta(&startP, &item, item2, item3);
- imprimeLista(startP); /*Extra*/
- break;
- case 2:
- if(!esVacia(startP)) {
- printf("\nTeclea el nombre a borrar\n");
- scanf(" %s", &item);
- printf("\nTeclea el edad a borrar\n");
- scanf(" %d", &item2);
- printf("\nTeclea el matricula a borrar\n");
- scanf(" %d", &item3);
- if(elimina(&startP, &item, item2,item3)) {
- printf(" %s %d %d eliminado \n", item, item2, item3);
- imprimeLista(startP);
- }
- else {
- printf(" %s %d %d no se encontro \n", item, item2, item3);
- }
- }
- else {
- printf("\n Lista vacia...");
- }
- break;
- default:
- printf("\n Eleccion no valida...");
- menu();
- break;
- }
- printf("\n Escoge una opcion: ");
- scanf(" %d", &op);
- }
- printf("Fin de programa...");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement