只是想知道这是否是remove_from_front的正确实现。
struct lnode {
int item;
struct lnode *next;
};
// remove_from front consumes a linked list that is stored on the heap and removes the first list.
void remove_from_front(struct lnode **list) {
struct lnode *next = (**list).next;
free(*list);
*list = next;
}
答案 0 :(得分:1)
就功能而言,它看起来没问题。现在提供一些健壮性提示:
NULL
参数。这应该是assert
ed。重做:
// Removes the head from a non-empty list whose nodes have been malloc'ed.
void remove_from_front(struct lnode **list) {
assert(list && "The parameter must not be NULL");
assert(*list && "The list must not be empty");
struct lnode *next = (**list).next;
free(*list);
*list = next;
}