List loop error

-2

The function of bottom of all, extra , does not work correctly, before reaching the first if after while the program closes me. What can be wrong?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct nodo{
    struct nodo *ant;
    struct nodo *sig;
    char nombreUsuario[30];
    char tw[140];
};

struct listaDoble{
    struct nodo *primero;
    struct nodo *ultimo;
};
void addOrd(struct listaDoble *lista);
void visLista(struct listaDoble *lista);
void eliminar(struct listaDoble *lista);
void verInversa(struct listaDoble *lista);
void extra(struct listaDoble lista);
int main() {
    struct listaDoble lista;
    int opc;
    lista.primero=NULL;
    lista.ultimo=NULL;

    do{
        opc=menu();
        switch(opc){
            case 1:
                addOrd(&lista);
                break;
            case 2:
                eliminar(&lista);
                break;
            case 3:
                visLista(&lista);
                break;
            case 4:
                verInversa(&lista);
                break;
            case 5:
                extra(lista);
                break;
        }
    }while(opc!=0);

}

int menu(){
    int opc;
    printf("Introduce una opcion :\n");
    printf("1) Insertar tweet \n");
    printf("2) Eliminar tweet. \n");
    printf("3) Recorrer lista. \n");
    printf("4) Recorrer lista al reves. \n");
    printf("5) Estadisticas. \n");
    printf("0) Salir\n");
    scanf("%d",&opc);
    return opc;
}


void eliminar(struct listaDoble *lista){
    struct nodo *recorre, *anterior, *siguiente;
    char nombre[30];
    recorre = lista->primero;
    int aux=1, i=1;
    if (recorre == NULL){
        printf("La lista esta vacia");
        return;
    }

    anterior=recorre->ant;
    siguiente=recorre->sig;

    printf("Introduce el nombre del usuario :\n"); fflush(stdin); gets(nombre);

    while(aux==1){
        while(recorre!=NULL && strcmp(nombre, recorre->nombreUsuario)!=0){
            anterior=recorre;
            recorre=recorre->sig;
            if(recorre!=NULL){
                siguiente=recorre->sig;
            }

        }
        if(recorre==NULL){

            printf("No se ha encontrado ese usuario.\n");       
            return;
        }
        else if(anterior==NULL && siguiente==NULL){
            lista->primero=NULL;
            lista->ultimo=NULL;
            free(recorre);
            return;
        }
        else if(siguiente==NULL){
                anterior->sig=NULL;
                lista->ultimo=anterior;
                return;
        }
        else if(anterior==NULL){
            siguiente=recorre->sig;
            siguiente->ant=NULL;
            lista->primero=siguiente;

            }
        else{
            anterior->sig=siguiente;
            siguiente->ant=anterior;
        }

        if(strcmp(siguiente->nombreUsuario, nombre)==0 && recorre!=NULL){
            free(recorre);
            recorre = lista->primero;
            anterior=recorre->ant;
            siguiente=recorre->sig;
            i=0;
        }
        else{
            return;
        }


    }   
}


void addOrd(struct listaDoble *lista){
    struct nodo *temp,*recorre,*anterior;

    temp=(struct nodo *) malloc (sizeof(struct nodo));

    printf("Introduce el nombre de usuario: \n"); fflush(stdin); gets(temp->nombreUsuario);
    printf("Introduce el tweet: \n"); fflush(stdin); gets(temp->tw);
    temp->ant=NULL;
    temp->sig=NULL;
    recorre=lista->primero;

    if(recorre==NULL){
        lista->primero=temp;
        lista->ultimo=temp;
        return;
    }
    else if(strcmp(temp->nombreUsuario, recorre->nombreUsuario)<=0){
        temp->sig=recorre;
        recorre->ant=temp;
        lista->primero=temp;
        return;
    }
    else{
        while(recorre!=NULL && strcmp(temp->nombreUsuario, recorre->nombreUsuario)>=0){
            anterior=recorre;
            recorre=recorre->sig;
        }
        if(recorre==NULL){
            anterior->sig=temp;
            temp->ant=anterior;
            lista->ultimo=temp;
        }
        else{
            anterior->sig=temp;
            temp->ant=anterior;
            temp->sig=recorre;
            recorre->ant=temp;
        }
    }
}

void visLista(struct listaDoble *lista){
    struct nodo *recorre;

    recorre=lista->primero;
    if(recorre==NULL){
        printf("La lista esta vacia.\n");
        return;
    }
    while(recorre!=NULL){
        printf("%s:  ", recorre->nombreUsuario);
        printf("%s\n", recorre->tw);
        recorre=recorre->sig;

    }
}
void verInversa(struct listaDoble *lista){
    struct nodo *recorre;

    recorre=lista->ultimo;
    if(recorre==NULL){
        printf("La lista esta vacia. \n");
        return;
    }
    while(recorre != NULL){
        printf("%s:  ", recorre->nombreUsuario);
        printf("%s\n", recorre->tw);
        recorre=recorre->ant;
    }
}

void extra(struct listaDoble lista){
    struct nodo *recorre, *siguiente, *anterior;
    float i, contUser, contMedia;
    recorre=lista.primero;

    if(recorre==NULL){
        printf("No hay tweets");
        return;
    }
    siguiente=recorre->sig;
    anterior=recorre->ant;

    contUser=1;
    contMedia=1;
    while(recorre!=NULL){

        if(strcmp(anterior->nombreUsuario, recorre->nombreUsuario) == 0 || strcmp(siguiente->nombreUsuario,recorre->nombreUsuario) == 0){

            contMedia++;
        }
        else{
            contUser++;
            contMedia++;
        }

        anterior=recorre;
        recorre=recorre->sig;

        if(recorre!=NULL){

        siguiente=recorre->sig;
        }
    }
    printf("El total de usuarios es de : %f y la media es de %f tweets por usuario", contUser, contMedia/contUser);
}
    
asked by Guillermo Fuentes 26.04.2018 в 20:29
source

1 answer

1
void extra(struct listaDoble lista){
    struct nodo *recorre, *siguiente, *anterior;
    float i, contUser, contMedia;
    recorre=lista.primero; // 1

    if(recorre==NULL){
        printf("No hay tweets");
        return;
    }
    siguiente=recorre->sig;
    anterior=recorre->ant; // 2

    contUser=1;
    contMedia=1;
    while(recorre!=NULL){

        if(strcmp(anterior->nombreUsuario, recorre->nombreUsuario) == 0 ||  ... ){ // 3

Following the comments:

  • recorre points to the first item in the list
  • anterior points to NULL
  • try to access NULL->nombreUsuario - > access to memory not addressed by your application.
  • When you get to point 3 you have all the ballots so that the Operating System, if it is minimally modern, kill your application to avoid corruption of memory.

    It would not hurt to check the status of anterior and siguiente before using them (yes, both because when you are in the last element of the list, siguiente will point to NULL ):

    if( anterior != NULL && strcmp(anterior->nombreUsuario, recorre->nombreUsuario) == 0 )
    {
      // ...
    }
    
    if( siguiente != NULL && strcmp(siguiente->nombreUsuario,recorre->nombreUsuario) == 0 )
    {
      // ...
    }
    
        
    answered by 27.04.2018 в 10:48