p = (Node *)malloc(sizeof(Node)); //为链表申请内存 Head = p; //第一个为头 Last = Head; Head->value = rand() % 50; Head->number = cnt; while (n--) { cnt++; Last = (Node *)malloc(sizeof(Node)); //每次申请的都指向尾节点 Last->value = rand() % 50; Last->number = cnt; p->next = Last; p = Last; } Last->next = NULL; return Head; }
/** * description find postion in all list * @param[in] pos list * @retval node address pointer **/ Node *FindPos(uint8_t pos, Node *list) { Node *pnode; pnode = list->next; /* old is while ((pnode->next != NULL) && (pos != 0)) I find the x-- to 0 have a x+1 times */ while ((pnode->next != NULL) && (pos != 1)) { pos--; pnode = pnode->next; // printf("pos=%2d\tpos->number=%d\n", pos, pnode->number); }
if (pos == 1) { return pnode; } else { returnNULL; } } /** * description find the value first time int List * @param[in] value List * @retval node * **/ Node *FindValue(uint8_t value, Node *list) {