Show list of integers

2

The program compiles but only shows the last item in the list to be entered and not the rest.

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

struct nodo{
    int valor;  
    struct nodo *sig;
};

int main(){
    int opcion;
    int valor;
    struct nodo *lista;
    lista = NULL;
    struct nodo *aux;
    aux = (struct nodo *)malloc(sizeof(struct nodo));

    do{
        printf("\n1- insertar elemento\n");
        printf("2- mostrar lista \n");
        printf("0- salir\n");
        scanf("%d", &opcion);
        switch(opcion){
        case 1:
            printf("ingrese un valor: ");
            scanf("%d", &valor);
            if(lista==NULL){
                lista = (struct nodo *)malloc(sizeof(struct nodo));
                lista->valor=valor;
                lista->sig=NULL;
            }
            else{
                lista->sig=aux;
                aux->valor=valor;
                aux->sig=NULL;
                aux=lista;
            }
            break;
        case 2: while(aux!=NULL){ //<- solo muestra el primer valor
            printf("el valor del elemento es %d \n", aux->valor); 
            aux = aux->sig;
        }
        break;
        case 0: printf("fin del programa \n");
        break;
        }
    }while (opcion !=0);
    getch();
    return 0;
}
    
asked by Alejandro Caro 06.09.2017 в 18:22
source

2 answers

1

I have made a series of modifications to your code so that you can save all the elements you want in the list and then view them. You had a bad way of booking and saving the data in the list and the way you visualized it. I hope I have helped you and anything you ask:)

Code:

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

struct nodo
{
    int valor;  
    struct nodo *sig;
};

int main()
{
    int opcion;
    int valor;
    struct nodo *principioLista = NULL;
    struct nodo *finalLista = NULL;
    struct nodo *aux;    

    do{
        printf("\n1- insertar elemento\n");
        printf("2- mostrar lista \n");
        printf("0- salir\n");
        scanf("%d", &opcion);

        switch(opcion)
        {
        case 1:
            printf("ingrese un valor: ");
            scanf("%d", &valor);
            aux = (struct nodo*)malloc(sizeof(struct nodo));
            aux->valor = valor;
            if(principioLista == NULL)
            {
                principioLista = aux;
                aux->sig = NULL;
                finalLista = aux;
            }
            else
            {
                finalLista->sig = aux;
                aux->sig = NULL;
                finalLista = aux;
            }
            break;
        case 2: 
            while(principioLista != NULL)
            { 
                printf("el valor del elemento es %d \n", principioLista->valor); 
                principioLista = principioLista->sig;
            }
            break;
            case 0: printf("fin del programa \n");
            break;
        }
    }while(opcion !=0);
    getch();
    return 0;
}
    
answered by 10.09.2017 / 22:47
source
1

You have forgotten one thing: Reserve memory for each new number. The sentence

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

You have to do it again every time you create a new node, or you will be reusing it continuously, so you always end up with a single element.

The body of the else would have to add that line and eliminate the last one so that the list points to the first element by many that you add

    
answered by 06.09.2017 в 18:27