Skip to main content
Tweeted twitter.com/StackCodeReview/status/1449117926846513159
Became Hot Network Question
Spelling and grammar
Source Link
Toby Speight
  • 88.3k
  • 14
  • 104
  • 327

Given the following data struct:

typdef struct lista {
    int num1, num2;
    struct lista* sig;
} nodoNum ;

and a declared nodoNumnodoNum pointer variable, the following function will take said variable and create nodes, link them and stop when the user inputs "0" as the first number and return NULL ora pointer to the first node to the(or a null pointer that was passedif numbers were provided).

nodoNum * crearLista(nodoNum * registro) {
    nodoNum * auxNodo;
    int tempNum1;
    printf("Introducir numero 1 >\n");
    scanf("%d", &tempNum1);
    if (tempNum1 != 0) {
        auxNodo = (nodoNum *) malloc(sizeof(nodoNum));
        auxNodo->num1 = tempNum1;
        printf("Introducir numero 2 >\n");
        scanf("%d", &auxNodo->num2);
        auxNodo->sig = crearLista(auxNodo);
        return auxNodo;
    } 
    else {
        return NULL;
    }
    
}

I have been asking some questions over at stack overflowStack Overflow to understand more about pointers. I have arrived at this solution after a while. I'm interested in knowing if I'm breaking some best practice or where there could be room for improvement. As far as I know, it works...but but as a begginner...Ibeginner, I can never be sure.!

Given the following data struct:

typdef struct lista {
int num1, num2;
struct lista* sig;
} nodoNum ;

and a declared nodoNum pointer variable, the following function will take said variable and create nodes, link them and stop when the user inputs "0" as the first number and return NULL or the first node to the pointer that was passed.

nodoNum * crearLista(nodoNum * registro) {
    nodoNum * auxNodo;
    int tempNum1;
    printf("Introducir numero 1 >\n");
    scanf("%d", &tempNum1);
    if (tempNum1 != 0) {
        auxNodo = (nodoNum *) malloc(sizeof(nodoNum));
        auxNodo->num1 = tempNum1;
        printf("Introducir numero 2 >\n");
        scanf("%d", &auxNodo->num2);
        auxNodo->sig = crearLista(auxNodo);
        return auxNodo;
    } 
    else {
        return NULL;
    }
    
}

I have been asking some questions over at stack overflow to understand more about pointers. I have arrived at this solution after a while. I'm interested in knowing if I'm breaking some best practice or where there could be room for improvement. As far as I know, it works...but as a begginner...I can never be sure.

Given the following data struct:

typdef struct lista {
    int num1, num2;
    struct lista* sig;
} nodoNum ;

and a declared nodoNum pointer variable, the following function will take said variable and create nodes, link them and stop when the user inputs "0" as the first number and return a pointer to the first node (or a null pointer if numbers were provided).

nodoNum * crearLista(nodoNum * registro) {
    nodoNum * auxNodo;
    int tempNum1;
    printf("Introducir numero 1 >\n");
    scanf("%d", &tempNum1);
    if (tempNum1 != 0) {
        auxNodo = (nodoNum *) malloc(sizeof(nodoNum));
        auxNodo->num1 = tempNum1;
        printf("Introducir numero 2 >\n");
        scanf("%d", &auxNodo->num2);
        auxNodo->sig = crearLista(auxNodo);
        return auxNodo;
    } 
    else {
        return NULL;
    }
    
}

I have been asking some questions over at Stack Overflow to understand more about pointers. I have arrived at this solution after a while. I'm interested in knowing if I'm breaking some best practice or where there could be room for improvement. As far as I know, it works... but as a beginner, I can never be sure!

Source Link

Function to create single linked-list

Given the following data struct:

typdef struct lista {
int num1, num2;
struct lista* sig;
} nodoNum ;

and a declared nodoNum pointer variable, the following function will take said variable and create nodes, link them and stop when the user inputs "0" as the first number and return NULL or the first node to the pointer that was passed.

nodoNum * crearLista(nodoNum * registro) {
    nodoNum * auxNodo;
    int tempNum1;
    printf("Introducir numero 1 >\n");
    scanf("%d", &tempNum1);
    if (tempNum1 != 0) {
        auxNodo = (nodoNum *) malloc(sizeof(nodoNum));
        auxNodo->num1 = tempNum1;
        printf("Introducir numero 2 >\n");
        scanf("%d", &auxNodo->num2);
        auxNodo->sig = crearLista(auxNodo);
        return auxNodo;
    } 
    else {
        return NULL;
    }
    
}

I have been asking some questions over at stack overflow to understand more about pointers. I have arrived at this solution after a while. I'm interested in knowing if I'm breaking some best practice or where there could be room for improvement. As far as I know, it works...but as a begginner...I can never be sure.