Home > Back-end >  node pointer of a list wont update
node pointer of a list wont update

Time:01-11

I had a problem and I cannot seem to find a solution.I tried to do a simple program that creates a list and prints it in C, but when i tried to run it it looped printing the first value of the list.This is the program if anyone could help plz:

I tried adding parenthesis etc but didn't work.

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

typedef struct nodo{
    int dato;
    struct nodo *next;
}nodo_t;

typedef nodo_t *Ptr_nodo;

int main(){
    Ptr_nodo testa,temp;
    int q;
    temp=NULL;
    testa=NULL;
    temp=malloc(sizeof(nodo_t));
    if(temp){
        q=0;
        while(q!=-1){
            printf("Inserire valore: ");
            scanf("%d",&q);
            if(q!=-1){
                temp->dato=q;
                temp->next=testa;
                testa=temp;
            }
        }
        while(testa!=NULL){
            printf("%d",testa->dato);
            if(testa->next!=NULL)
                printf(" -> ");
            else
                printf(" -|");
            testa=testa->next;
        }
    }
    else
        printf("Errore allocazione memoria"),
    free(temp);
    return 0;
}

CodePudding user response:

I tried out your code and got the repeated printing of the one node which was pointing to itself. What appeared to be missing was when more data was being entered, new nodes needed to be created with memory allocated for each node. With that in mind, following is a refactored version of your code.

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

typedef struct nodo{
    int dato;
    struct nodo *next;
}nodo_t;

typedef nodo_t *Ptr_nodo;

int main(){
    Ptr_nodo testa,temp;
    int q;
    temp=NULL;
    testa=NULL;
    temp=malloc(sizeof(nodo_t));
    testa = temp;
    if(temp){
        q=0;
        while(q!=-1){
            printf("Insert Value: ");
            scanf("%d",&q);
            if(q!=-1){
                temp->dato=q;
                temp->next=malloc(sizeof(nodo_t));  /* Created new node and allocate memory */
                temp=temp->next;
                temp->next = NULL;                  /* Make sure this latest node has the NULL terminator */
            }
        }
        while(testa!=NULL){
            printf("%d",testa->dato);
            if(testa->next!=NULL)
                printf(" -> ");
            else
                printf(" -|");
            testa=testa->next;
        }
        printf("\n");                               /* Just added this for appearances on the terminal output */
    }
    else
        printf("Memory allocation error"),
    free(temp);
    return 0;
}

Some bits to point out.

  • When another value is received from the user, a new node needs to be created with memory allocated via the "malloc" function.
  • Pointer references for the previous node and new node need to be updated once the new node is created.
  • In the new node, its "next" pointer needs to be set to NULL so as to ensure that the subsequent list printing does not get caught up in an endless loop.

With those bits added, following was a sample output at the terminal.

@Vera:~/C_Programs/Console/MultiNode/bin/Release$ ./MultiNode 
Insert Value: 4
Insert Value: 1998
Insert Value: 22
Insert Value: 45
Insert Value: 88
Insert Value: -1
4 -> 1998 -> 22 -> 45 -> 88 -> 0 -|

One small side effect I saw was that the final node has a value of zero. I don't know if that is a desired outcome, but I will leave that as something to possibly be refined.

Give that a try and see if it meets the spirit of your project.

CodePudding user response:

draw it out:

temp = malloc(...);  // creates a node

          ------------ 
temp --> |  data: ??  |
         |  next      | --> ?? points nowhere right now
          ------------ 

testa --> NULL

You scanf your data, just call it 5

scanf("%d",&q); // user inputs 5
if(q!=-1){
   // set data
   temp->dato=q;
   temp->next=testa;
          ------------ 
temp --> |  data: 5   |
         |  next      | --> testa, which is NULL right now
          ------------ 

   testa=temp;  // results in testa and temp pointing to the same node

           ------------ 
temp  --> |  data: 5   |
testa --> |  next      | --> NULL
           ------------ 
}

Now you loop back around

scanf("%d",&q); // user inputs 8
if(q!=-1){
   // whoops, didn't create a new node, so it's still the same temp from before
   temp->dato=q;
   temp->next=testa;
   testa=temp;

           ------------ 
temp  --> |  data: 8   |
testa --> |  next      | --> testa, which is the same as temp ----- 
           ------------                                            |
                 ^                                                 |
                 |                                                 |
                  ------------------------------------------------- 

You have one node that points to itself. You need to create a new node on each user entry, I trust the other answer(s) show you how to do that.

  • Related