Problem of segmentation of memory project in c

1

I have a problem with the following program, I introduce a first data through the function add and the data is saved without problem, to the second round and try to enter a second data along with another node the program jumps with a error and it closes "In windows it tells me that the program has stopped".

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

typedef struct nodo{

    int dato;
    struct nodo *siguiente;

}Nodo;

typedef Nodo *list;
typedef Nodo *pnodo;


void agregar_lista1(list *inicio,list *ultimo);
void agregar_lista2(list *inicio2,list *ultimo2);
void mostrar_lista();

int main (){
    int op;

    list inicio = NULL ,inicio2 = NULL,ultimo = NULL,ultimo2 = NULL;

    do{

        printf ("MENU");
        printf ("\nOp1 Ingresar nodo en la lisata 1");
        printf ("\nOp2 Ingresar nodo en la lisata 2");
        printf ("\nOp3 Mostar los datos almacenados en la lista");
        printf ("\nOp4 Mostrar suma de las listas anteriores\n\n");
        scanf ("%d",&op);

        switch(op){

            case 1:
                agregar_lista1(&inicio,&ultimo);
                break;
            case 2:
                agregar_lista1(&inicio2,&ultimo2);
                break;
            case 3:
                printf ("\n\nLista 1\n\n");
                mostrar_lista(inicio);
                printf ("\n\nLista 2\n\n");
                mostrar_lista(inicio2);
                 break; 
            case 4:

            break;          
        }
    }while (op!=4);

    return 0;
}

void agregar_lista1(list *inicio,list *ultimo){
    pnodo nue,act;
    nue=malloc(sizeof(Nodo));

    if (nue==NULL){
        printf ("\nNo se pudo crear el nodo\n");
    }

    printf ("Ingrese un numero entero positivo: ");
    scanf ("%d",&nue->dato);

    if (*inicio == NULL){

        *inicio = nue;
        nue->siguiente = NULL;
        act = nue;
    }
    else{

        act->siguiente = nue;
        nue->siguiente = NULL;
        act=nue; 
    }

    printf ("\n\n%i\n\n",nue->dato);

    }


    void mostrar_lista(inicio){
        pnodo ptr;

        ptr = inicio;

        if (inicio != NULL){

            while (ptr != NULL){

                printf ("\nDato %d \n",ptr->dato);
                ptr = ptr->siguiente;

            }

        }
        else {
            printf ("\n\nLa lista esta vacia\n\n");
        }       

    }
    
asked by Jose Ignacio Gonazález 27.04.2017 в 03:44
source

1 answer

1

specifically the violation of the segment by which questions this line generates:

act->siguiente = nue;

This is because act is a pointer variable that is declared in the scope of the function and you assign it a value within the if, therefore in the else, this variable does not have an assigned value, so when trying to access To a field of the data to which this variable must point generates a segment violation, the value of the act variable is null.

Review the way you insert and use the start and end pointers.

Greetings.

    
answered by 27.04.2017 в 05:41