Home > Back-end >  C language linked list problem.. i dont know why next value is not working of create
C language linked list problem.. i dont know why next value is not working of create

Time:03-15

i'm noob of C language, so i have a study of Linked list

first, i'll show my code.

typedef struct _USER {
    char name[100];
    struct _USER* next;
} USER;

int question(USER** head){
    USER* tempHead = *head;
    USER* a = tempHead;
    while(a != NULL){
        a = a->next;
    }
    USER* b = (USER*)malloc(sizeof(USER));
    strcpy(b->name,"qwer");
    b->next = NULL;

    USER* c = a;
    c = b;

    while(tempHead != NULL){
        tempHead = tempHead->next;
        printf("%s\n",tempHead->name);
    }
    //list(tempHead) doesn't exist "qwer".. why?
    printf("end\n");
}

int main(void) {
    USER* head = NULL;
    //void makeList(USER** head);
    makeList(&head);
    //makeList is working success
    question(&head);
}

i think point variable is not reference to "head"..

why?

CodePudding user response:

This bit

    USER* c = a;
    c = b;

makes no sense to me. c is never used.

Compiler warnings

Turn on compiler warnings, it will tell you this. I think that knowing that c is never used, and immediately overwritten, will lead to finding the problem

Separation of concerns

I would also aim to separate out functionality. One method to query, another to mutate.

Hungarian

Not the cause in this case. But I also found that a little Hungarian notation help with pointers. Don't use if for everything, but with pointers it helps.

E.g.

USER user;
USER* user_pt;

CodePudding user response:

You simply do not add the new node to your list.

int question(USER** head){
    USER* tempHead = *head;
    USER* a = tempHead;
    while(a != NULL){
        a = a->next;
    }

After this loop, a holds NULL. You cannot add any node to the list with that.

    USER* b = (USER*)malloc(sizeof(USER));
    strcpy(b->name,"qwer");
    b->next = NULL;

You do not store b anywhere.

    USER* c = a;
    c = b;
    while(tempHead != NULL){
        tempHead = tempHead->next;
        printf("%s\n",tempHead->name);

You don't print node tempHead but tempHead->next. That could be NULL. Also you skip the first node.

    }
    //list(tempHead) doesn't exist "qwer".. why?
    printf("end\n");
}

What you need is this:

int question(USER **head) {
// Note: Some error handling in case (head == NULL) would be useful

// First create the new node
    USER *b = malloc(sizeof(USER)); // In C you shouldn't cast the result of malloc
    // You should check for NULL...
    strcpy(b->name,"qwer");
    b->next = NULL;

//Now we have a new node, let's check where it should go.

// First check if the list is empty
    if (*head == NULL) 
    { // Just put it as new head.
      *head = b;
    }
    else
    { // Search for the end of the list.
      USER *temp = *head;
      // No need to check for NULL as we would not be here in that case.

      while(temp->next != NULL){
         temp = temp->next;
      }
      // Now temp points to the last node in the list.
      temp->next = b;
    }

    // Print the list.
    USER *tempHead = *head;
    while (tempHead != NULL) {
        printf("%s\n",tempHead->name); // First print, then advance the pointer
        tempHead = tempHead->next;
    }

    printf("end\n");
}
  • Related