Edw4rd
Edw4rd

Reputation: 147

Storing data inside structs (Linked List)

Hi guys I'm learning C and there are some issues that I can't solve.

first of all, this are my data structure:

struct user_node {
 void *name;
 struct user_node *next;
 struct msg_node *msgnext;
};

struct msg_node {
 void *sender;
 void *receiver;
 void *title;
 void *content;
 struct msg_node *msgnext;
};
struct user_node *user_database = NULL;

The idea is that a user may have one or more messages.

Well I can create and delete users but I'm having problem storing messages, for example here:

The purpose of this function is to put temp as a message inside my data structure for a given user that we find in the message itself. (temp is already msg_node with data which I take from another function)

void sendMsg(struct msg_node* temp) {
//if list is empty
if (user_database == NULL) {
    printf("There aren't users on the system.\n\n");
    return;
}

struct user_node** ptr = &user_database;
while (*ptr) {
    if (strncmp((*ptr)->name, (temp)->receiver, strlen(temp-
>receiver)) == 0) {


        temp->msgnext = &user_database->msgnext;
        user_database->msgnext = temp;

        return;
    }
    ptr = &(*ptr)->next;
}
printf("User not found on the system\n\n");

return;
}

I know that the code is wrong but I been messing arround with this since yesterday and I can't figure it , may someone help me?

Thanks in advance

Upvotes: 0

Views: 42

Answers (1)

M Oehm
M Oehm

Reputation: 29136

You can insert a node at the front of a linked list by sett ing the new node's next pointer to the list's head and then setting the lis's head to the new node. This works even for an empty list, when the list's head is NULL.

You've got this almost right, but the List's head is the one associated with the current user, not with the user list's head, i.e. the first user in the database.

The following code should do what you want:

int sendMsg(struct msg_node *msg)
{
    struct user_node *user = user_database;

    if (user == NULL) {
        printf("There aren't users on the system.\n");
        return -1;
    }

    while (user) {
        if (strcmp(ptr->name, msg->receiver) == 0) {
            msg->msgnext = user->msgnext;
            user->msgnext = msg;

            return 0;
        }

        user = user->next;
    }

    printf("User '%s' not found on the system.\n", msg->receiver);

    return -1;
}

Notes:

  • I've renamed the pointers from the rather nondescript temp and ptr to the more descriptive msg and user.
  • I've made the function return a success code: 0 for success and −1 for failure.
  • strncmp will compare only a certain number of characters. I've changed this to strcmp, so that the users Paul and Pauline are considered different.
  • There's no need to make the traversing pointer a pointer to pointer to node. That technique is useful, but only when you want to insert or delete nodes. Inserting at the front is a special case where you won't need it. (And you insert a message, not a user, so if you wanted to insert the message somewhere else than at the front, you could iterate through the sub-list with a pointer to pointer to message node.)

Upvotes: 1

Related Questions