Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在结构(链表)中存储数据_C_List_Structure - Fatal编程技术网

C 在结构(链表)中存储数据

C 在结构(链表)中存储数据,c,list,structure,C,List,Structure,大家好,我正在学习C语言,有些问题我无法解决 首先,这是我的数据结构: 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

大家好,我正在学习C语言,有些问题我无法解决

首先,这是我的数据结构:

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;
其思想是,用户可能有一条或多条消息

我可以创建和删除用户,但在存储消息时遇到问题,例如:

此函数的目的是将temp作为消息放入给定用户的数据结构中,我们可以在消息本身中找到该用户。temp已经是msg_节点,其中包含我从另一个函数获取的数据

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;
}
我知道代码是错的,但我从昨天起就一直在胡闹,我想不出来,有人能帮我吗


提前感谢

您可以通过将新节点的下一个指针设置为列表的标题,然后将lis的标题设置为新节点,在链接列表的前面插入节点。即使对于空列表,当列表头为空时,这也有效

这几乎是对的,但是列表的标题是与当前用户关联的,而不是与用户列表的标题(即数据库中的第一个用户)关联的

以下代码应该执行您想要的操作:

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;
}
注:

我已经将指针从相当不起眼的temp和ptr重命名为更具描述性的msg和user。 我已使函数返回成功代码:0表示成功,0表示成功−1.失败。 strncmp将只比较一定数量的字符。我把它改成了strcmp,因此用户Paul和Pauline被认为是不同的。 无需将遍历指针设为指向节点的指针。该技术非常有用,但仅当您希望插入或删除节点时才有用。在前面插入是一种特殊情况,您不需要它。然后插入一条消息,而不是用户,因此,如果您想将消息插入前端以外的其他位置,可以使用指向消息节点的指针在子列表中进行迭代。
从您的结构中,我了解到用户有一个消息列表。对吗?这里不需要指向用户节点指针的指针;只要一个节点用户\ u node*指针就足够了。插入消息时,请使用迭代器ptr,而不是全局用户_数据库:tmp->msgnext=ptr->msgnext;ptr->msgnext=tmp;现在还不清楚你为什么要用符号;你可能想用*ptr->msgnext做点什么。@gsamaras是的,没错。@MOehm你能给它编码吗?我写了一个未经测试的答案,对不起!代码。这给了我编译问题:while*user{if strcmptr->name,msg->receiver==0{msg->msgnext=usr->msgnext;usr->msgnext=msg;可能是我的编译器?很好,你找到了错误。很抱歉,这是因为发布了未经测试的代码。但我无法鼓起热情围绕它编写测试框架。