Mis*_*u4u 2 c linked-list nodes
到目前为止,在链表中,我只使用了一个temp节点来遍历给定链表的不同操作,这非常容易。
Now an assignment of a book demands to write a C code where the user input would be a number in the list and we have to compare the number just before the given number and the number just after the given number and tell which one is greater or equal, in case. For that, according to me, we need two pointers viz. prev and next on both side of current node to point to the two numbers at the different nodes and then collecting there data part we can compare them. But I can not code this part in C. So a code snippet to point to the required nodes would be helpful.
您不需要 prev 指针。您只需要一个临时变量来跟踪上一个和当前节点,如下所示:
prevNode = NULL;
curNode = *p;
while (curNode != NULL)
{
prevNode = curNode;
curNode = curNode->next;
}
Run Code Online (Sandbox Code Playgroud)
一旦找到要进行比较的位置,就可以使用 prevNode 的数据、curNode 的数据和 curNode->next 的数据。希望这可以帮助。