特点: 头结点不使用,只用于判断链表是否为空,这样遍历代码写起来就比较好看了 bool SetVariable(VariableSpace space, const char *name, const char *value) { struct _variable *current, *previous; if (!space) return false; if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name)) return false; if (!value) return DeleteVariable(space, name); for (previous = space, current = space->next; current; previous = current, current = current->next) { if (strcmp(current->name, name) == 0) { /* found entry, so update */ if (current->value) free(current->value); current->value = pg_strdup(value); if (current->assign_hook) (*current->assign_hook) (current->value); return true; } } /* not present, make new entry */ current = pg_malloc(sizeof *current); current->name = pg_strdup(name); current->value = pg_strdup(value); current->assign_hook = NULL; current->next = NULL; previous->next = current; return true; }