标签:ar sp strong on bs amp size new as
v2版为非健全版本,很明显不能再最前端插入。
int sll_insert_v2(Node *current, int value) { Node *prev; Node *newNode; while (current != NULL && current->value >= value) { prev = current; current = current->next; } newNode = malloc(sizeof(Node)); if (newNode == NULL) { return FALSE; } newNode->value = value; if (prev == NULL) { newNode->next = current->next; current->next = newNode; } else { newNode->next = current; prev->next = newNode; } return TRUE; }
v3版本实现了插入功能,但是很明显就是,其实所有的插入都是:
新节点链向当前节点的下一 节点
当前节点链向新节点
所以有了第三版本我们需要的就是当前节点 和 指向当前节点link 的一个指针
int sll_insert_v3(Node **rootp, int value) { Node *newNode; Node *current; Node *prev; current = *rootp; while (current != NULL && current->value >= value) { prev = current; current= current->next; } newNode = malloc(sizeof(Node)); newNode->value = value; newNode->next = current; if (prev == NULL) { *rootp = newNode; } else { prev->next = newNode; } return TRUE; }
v4版本:current为当前版本, rootp为指向当前节点的link 的指针
int sll_insert_v4(Node **rootp, int value) { Node *newNode; Node *current; while ((current = *rootp) != NULL && current->value >= value) { rootp = ¤t->next; } newNode = malloc(sizeof(Node)); newNode->value = value; newNode->next = current->next; *rootp = newNode; return TRUE; }
标签:ar sp strong on bs amp size new as
原文地址:http://my.oschina.net/u/195896/blog/341052