Create list simply linked in C with linux?

0

Some time ago I used linked lists in C, but using the Windows operating system and the IDE Visual Studio, I am currently using Linux (Ubuntu), and I realize that the GCC compiler does not allow me to do the things like I did in windows.

I have the linked list code that I did a few months ago, but I can not make it work on Linux, can someone give me an idea?

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

struct lista {
    int valor;
    lista *prox;
};

lista *crear(int x) {
    lista *aux = new(lista);
    aux -> valor = x;
    aux -> prox = NULL;
    return(aux);
};

void insertarPorCabConRep(lista **p, int x) {
    lista *aux = new(lista);
    aux -> valor = x;
    aux -> prox = *p;
    *p = aux;
};

void insertarPorColaConRep(lista **p, int x) {
    lista *aux = *p;
    if(!aux)
        *p = crear(x);
    else {
        while((aux) && (aux -> prox))
            aux = aux -> prox;
        aux -> prox = crear(x);
    }
};

int buscarX(lista *p, int x) {
        while(p != NULL) {
            if(p -> valor == x)
                return(1);
            p = p -> prox;
        };
    return(0);
};

void eliminarXPrimeraVez(lista **p, int x) {
    lista *aux = *p, *aux2 = NULL;
    while(aux != NULL) {
        if((aux -> valor == x) && (aux -> prox != NULL)) {
            *p = (*p) -> prox;
            delete(aux);
            break;
        }
        else if((aux -> valor == x) && (aux -> prox == NULL)) { //Revisar esta parte porque da error cuando solo tiene un elemento
            aux2 = aux;
            delete(aux2);
            aux = NULL;

            break;
        }
        else if((aux -> prox -> valor == x) && (aux -> prox != NULL)) {
            aux2 = aux -> prox;
            aux -> prox = aux -> prox -> prox;
            delete(aux2);
            break;
        };
        aux = aux -> prox;
    };
};

void mostrarLista(lista *p) {
    if(p != NULL) 
        while(p != NULL) {
            printf(" [%d] -> ",p->valor);
            p = p->prox;
        }
    else
        printf("La lista se encuentra vacia.\n");
};

void main ( )
{
lista *A = NULL;
int n;
int op;
op=-1;
while (op!=0){
printf ("1.    Insertar elemento en lista por cabeza \"Con repeticiones\"\n");
printf ("2.    Insertar elemento en lista por cola \"Con repeticiones\"\n");
printf ("3.    Buscar elemento X en la lista\"\n");
printf ("4.    Eliminar elemento la primera vez que aparece\n");
printf ("99.   Mostrar la lista\n");

printf ("0.    Salir\n");
scanf_s("%d", &op);
switch (op){
case 1: 
    system("cls");
    printf(" \nIntroduzca el elemento a insertar en la cabeza de la lista: ");
    scanf_s("%i",&n);
    insertarPorCabConRep(&A, n);
break;
case 2: 
    system("cls");
    printf(" \nIntroduzca el elemento a insertar en la cola de la lista: ");
    scanf_s("%i",&n);
    insertarPorColaConRep(&A, n);
break;
case 3: 
    system("cls");
    printf("Ingrese el elemento que desea buscar: ");
    scanf_s("%d",&n);
    if(buscarX(A,n) == 1)
        printf("\n\nEl elemento %d si se encuentra.\n\n",n);
    else 
        printf("\n\nEl elemento %d no se encuentra.\n\n",n);
break;
case 4:
    system("cls");
    printf("Ingrese el elemento que desea eliminar de la lista: ");
    scanf_s("%d",&n);
    eliminarXPrimeraVez(&A,n);
    break;
case 99: 
    mostrarLista(A);
    printf("[NULL]\n\n");
break;

};
system("pause");
system("cls");
};
}
    
asked by Pedro Fumero 04.11.2017 в 20:55
source

1 answer

0

The problem was because it is not possible to use the new function in the GCC compiler, so it is necessary to use malloc to request and reserve the necessary memory. Next I share the corrected code.

typedef struct lista {
    int valor;
    struct lista *prox;
}nodo;

nodo *crear(int x) {
    nodo *aux = (nodo*)malloc(sizeof(nodo));
    aux -> valor = x;
    aux -> prox = NULL;
    return(aux);
};

void insertarPorColaConRep(nodo **p, int x) {
    nodo *aux = *p;
    if(!aux)
        *p = crear(x);
    else {
        while((aux) && (aux -> prox))
            aux = aux -> prox;
        aux -> prox = crear(x);
    }
};

void mostrarLista(nodo *p) {
    if(p != NULL) 
        while(p != NULL) {
            printf("[ %d ]\n",p->valor);
            p = p->prox;
        }
    else
        printf("La lista se encuentra vacia.\n");
};

void contarElementos(nodo *p) {
    int i = 0;
    if(p != NULL) {
        while(p != NULL) {
            i++;
            p = p->prox;
        }
    }
    printf("Elementos de la lista: %d", i);
}
    
answered by 05.11.2017 в 15:52