Add item to the top list

0

I need to add the content, to the first place in the list. But I can not do it, any help? This is the code I have.

if (primero == NULL) { //Por si el primer elemento es NULL
    printf("\nNuevo elemento:\n");
    printf("Nombre: ");
    fflush(stdin);
    scanf("%s",nuevo->nombre);
    printf("Teléfono: ");
    fflush(stdin);
    scanf("%s", nuevo->telefono);
    nuevo->siguiente = NULL;
    if (primero==NULL) {
        printf( "Primer elemento\n");
        primero = nuevo;
        ultimo = nuevo;
    }
    else {
        ultimo->siguiente = nuevo;
        ultimo = nuevo;
    }
        }
else{ //En caso de que el primer lugar este ocupado
    primero -> siguiente = nuevo;
    printf("\nNuevo elemento:\n");
    printf("Nombre: ");
    fflush(stdin);
    scanf("%s",nuevo->nombre);
    printf("Teléfono: ");
    fflush(stdin);
    scanf("%s", nuevo->telefono);

    if (primero!=NULL) {
        primero=nuevo;
        ultimo=nuevo;
    }
    else {
        printf( "Primer elemento\n");
        /* el que hasta ahora era el ultimo tiene que apuntar al nuevo */
        ultimo->siguiente = nuevo;
        /* hacemos que el nuevo sea ahora el ultimo */
        ultimo = nuevo;
    }
    
asked by Fernando Withmore 06.11.2017 в 23:22
source

2 answers

0

At the end I solved it on my own, and I came to share the code. In case someone has the same problem, or need example code. Greetings.

funcion.c

#include "cabecera.h"

void mostrar_menu() {
printf("\n\nMenu:\n=====\n\n");
printf("1.- Anadir contacto al inicio\n");
printf("2.- Anadir contacto al final\n");
printf("3.- Eliminar contactos\n");
printf("4.- Ordenar contactos por nombre\n");
printf("5.- Mostrar lista de contactos\n");
printf("6.- Salir\n\n");
printf("Elegir una opcion: ");
fflush(stdout);
}

void anadir_elemento(int num_nodo) {
struct _agenda *nuevo = NULL;
nuevo = (struct _agenda *) malloc (sizeof(struct _agenda));
if (nuevo==NULL) {
    printf( "No hay memoria disponible!\n");
    return;
 }

printf("\nNuevo elemento:\n");
printf("Nombre: ");
fflush(stdin);
scanf("%s",nuevo->nombre);
printf("Teléfono: ");
fflush(stdin);
scanf("%s", nuevo->telefono);
nuevo->num = num_nodo;

nuevo->siguiente = NULL;

if (primero==NULL) {
    printf( "Primer elemento\n");
    primero = nuevo;
    ultimo = nuevo;
    Ainicio = 0;
    Afinal = 0;
 }else {
    if (Ainicio == 1){
        nuevo->siguiente = primero;
        primero = nuevo;
        printf("Se agrego elemento al inicio de la lista");
        Ainicio = 0;
    }else{
        ultimo->siguiente = nuevo;
        ultimo = nuevo;
    }

    if (Afinal == 1){
        printf("Se agrego elemento al final de la lista");
        Afinal = 0;
    }
}
}

main.c

#include "cabecera.h"

int main() {
int opcion;
primero = (struct _agenda *) NULL;
ultimo = (struct _agenda *) NULL;
Ainicio = 0;
Afinal = 0;
int cant_nodos = 0;
do {
    mostrar_menu();
    scanf("%d", &opcion);
    fflush(stdin);
    switch ( opcion ) {
        case 1: cant_nodos++; Ainicio = 1; anadir_elemento(cant_nodos); break;
        case 2: cant_nodos++; Afinal = 1; anadir_elemento(cant_nodos); break;
        case 3: borrar_elemento(); break;
        case 4: ordenar_lista(); break;
        case 5: mostrar_lista(); break;
        case 6: liberarYSalir(); exit(1);
        default: printf( "Opción no válida\n" ); break;
    }
} while (opcion!='6');
return 0;
}

cabezera.h

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


struct _agenda {
char nombre[20];
char telefono[12];
int num;
struct _agenda *siguiente;
};

struct _agenda *primero, *ultimo;
int Afinal;
int Ainicio;

void mostrar_menu();
void anadir_elemento(int num_nodo);
void mostrar_lista();
void borrar_elemento();
void liberarYSalir();
    
answered by 27.11.2017 / 17:52
source
0

Just as the cards are first shuffled and then dealt, first arrange the data in the array according to your need, without printing them yet . Then, being ordered, print them without any other type of condition. Greetings!

    
answered by 07.11.2017 в 03:30