下面的代码是我的问题的一个例子。我可以在链接列表中搜索、插入等,但我只有 1 个链接列表。因此,我想更改所有访问器函数,以允许传递包含将要处理的成员的结构,以便轻松分配 var_alias_t 类型的新链接列表。
此代码运行良好,但想要修改以允许此函数和其他函数修改 var_alias_t 类型的多个列表。
struct var_alias_t
{
char *alias;
char *command;
struct var_alias_t *next;
};
struct var_alias_t *ptr = NULL;
struct var_alias_t *head = NULL;
struct var_alias_t *curr = NULL;
struct var_alias_t* var_alias_ll_create_list(char *alias, char *cmd)
{
printf("\n creating list with headnode as [%s,%s]\n",alias,cmd);
struct var_alias_t *ptr = (struct var_alias_t*)malloc(sizeof(struct var_alias_t));
ptr->alias = malloc(strlen(alias)+1);
ptr->command = malloc(strlen(cmd)+1);
if(NULL == ptr)
{
printf("\n Node creation failed \n");
return NULL;
}
strcpy(ptr->alias,alias);
strcpy(ptr->command,cmd);
ptr->next = NULL;
head = curr = ptr;
return ptr;
}
我尝试更改代码。
struct var_alias_t
{
char *alias;
char *command;
struct var_alias_t *next;
};
struct var_alias_t *ptr = NULL;
struct var_alias_t *head = NULL;
struct var_alias_t *curr = NULL;
typedef struct
{
struct var_alias_t *ptr;
struct var_alias_t *head;
struct var_alias_t *current;
}ll_tracking_t;
ll_tracking_t ll_var = {NULL,NULL,NULL};
ll_tracking_t ll_alias = {NULL,NULL,NULL};
struct var_alias_t* Xvar_alias_ll_create_list(char *part1, char *part2,ll_tracking_t master)
{
printf("\n creating list with headnode as [%s,%s]\n",part1,part2);
struct var_alias_t *(master.ptr) = (struct var_alias_t*)malloc(sizeof(struct var_alias_t));
master.ptr->alias = malloc(strlen(part1)+1);
master.ptr->command = malloc(strlen(part2)+1);
if(NULL == master.ptr)
{
printf("\n Node creation failed \n");
return NULL;
}
strcpy(master.ptr->alias,part1);
strcpy(master.ptr->command,part2);
//ptr->alias = alias;
//ptr->command = cmd;
master.ptr->next = NULL;
//strcpy(*Items,Data) ; // Here we copy it
master.head = master.current = master.ptr;
return master.ptr;
}
我得到的错误在下面一行是“错误:预期的')'之前'。' 令牌”
struct var_alias_t *(master.ptr) = (struct var_alias_t*)malloc(sizeof(struct var_alias_t));
我的理解是我猜错了,但上面的陈述意味着。使用 var_alias_t 结构的指针设置名为 ptr 的主结构成员的值,它是指向分配内存位置的指针,该内存位置的大小与 var_alias_t 结构的大小相同。
感谢您的帮助,我对所有这些东西都是新手!