elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado:


  Mostrar Temas
Páginas: [1]
1  Programación / Programación C/C++ / Ayuda eliminacion nodo lista enlazada en: 25 Febrero 2017, 21:07 pm
Buenas tardes, tengo poco que empeze con el tema de listas enlazadas y estoy realizando un ejercicio de un libro el cual es un registro de un hotel, en el cual el usuario ingresa nombre y habitacion, todo me funciona bien solo que al eliminar un nodo dependiendo cual deseo eliminar me provoca un crash y la verdad no encuentro el error por mas que intento.
Por ejemplo:

nodo1 -> nodo2 -> nodo3

si elimino nodo2 me queda:

nodo1->nodo3

al eliminar nodo3 deja de funcionar el programa.

o tambien cuando elimino nodo1 y nodo3 tambien se eliminan perfectamente pero el nodo2 restante al tratar de eliminarloigual deja de funcionar.

Se los agradeceria si me pudieran proporcionar una ayuda en como puedo solucionar esto o si estoy cometiendo algun error en mi codigo.

Código:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define SIZE 20

struct Registro {
char nombre[SIZE];
int habit;
struct Registro *siguiente;
};

typedef struct Registro NODOLISTA;
typedef NODOLISTA *NODOLISTAPTR;
NODOLISTAPTR inicio = NULL;

/* Function prototype */
void insertar ( NODOLISTAPTR *, char[SIZE] ,int);
void imprimir ( NODOLISTAPTR );
int count ( NODOLISTAPTR );
void eliminar(NODOLISTAPTR);

int main() {
    char cliente[SIZE];
    int opc=0;
    int i=0;
    int hab=0;;
    do{
        printf("\n\t\t* * MENU * *");
        printf("\n1) Registrar\n2) Eliminar\n3) Imprimir registro\n4) Salir\n\nOpcion: ");
        scanf("%d",&opc);
        system("cls");
        switch(opc){
        case 1:
            printf("\nIngrese los siguientes datos:");
            printf("\n\nNombre: ");
            fflush(stdin);
            scanf ("%[^\n]%*c", cliente);
            printf("Habitacion: ");
            fflush(stdin);
            scanf("%d",&hab);
            insertar ( &inicio, cliente, hab);
            printf("\nRegistro exitoso.");
            getch();
            system("cls");
            break;
        case 2:
            eliminar(inicio);
            break;
        case 3:
            imprimir(inicio);
            getch();
            break;
        case 4:
            printf("Saliendo...");
            getch();
            break;
        }
    }while(opc!=4);
    return 0;
}

void insertar ( NODOLISTAPTR *sPtr, char valor[SIZE],int _hab )
{
NODOLISTAPTR nuevo, previo, actual;
int cmp;
nuevo = malloc(sizeof(NODOLISTA));
if ( nuevo != NULL ) {
strcpy(nuevo->nombre, valor);
nuevo->habit=_hab;
nuevo->siguiente = NULL;
previo = NULL;
actual = *sPtr;
while ( actual != NULL ) {
cmp = strcmp(valor, actual->nombre);
if (cmp < 0) {
/* you're at the point where you need to add the node */
break;
}
previo = actual;
actual = actual->siguiente;
}
if ( previo == NULL ) {
nuevo->siguiente = *sPtr;
*sPtr = nuevo;
}
else{
previo->siguiente = nuevo;
nuevo->siguiente = actual;
}
}
else
printf("%s no se inserto.\n", valor);
}

void imprimir ( NODOLISTAPTR actual )
{
if ( actual == NULL )
printf("Lista esta vacia.\n\n");
else{
printf("The list is:\n");
while ( actual != NULL ){
printf("%s , %d --> ", actual->nombre,actual->habit);
actual = actual->siguiente;
}
printf("NULL\n\n");
}
}

void eliminar(NODOLISTAPTR actual){
    NODOLISTAPTR prev;
    char cli[SIZE];
    printf("Ingrese nombre a borrar: ");
    fflush(stdin);
    scanf ("%[^\n]%*c", cli);
    while ( actual != NULL ) {
        if (strcmp(cli, actual->nombre) == 0) {
            if(actual==inicio){
                inicio=actual->siguiente;
                free(actual);
            }else{
                prev->siguiente=actual->siguiente;
                free(actual);
            }
        }
        else{
            prev = actual;
            actual = actual->siguiente;
        }
    }
}

Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines