在C中的链表中更改索引 [英] Change index in a linked list in c
问题描述
有人可以帮助我找到一种根据输入来更改节点在链表中的位置的算法吗?
Can anyone help me with finding an algorithm to change a position of a node in a linked list according to the input?
示例:
我有一个链接列表:1-> 2-> 3-> 4-> END,用户选择了第二个节点和索引4.
I have the linked list: 1->2->3->4->END and the user chose the second node and the index 4.
该算法将为我们提供结果: 1-> 3-> 4-> 2-> END.
The algorithm will give us the result: 1->3->4->2->END.
感谢您的帮助!
推荐答案
在链表中移动节点确实很容易,因为节点已为此完成...
It is really easy to move nodes in a linked list because they are done for that...
要更改节点的位置,只需将其下一个指针更改为下一个节点地址,然后将上一个节点的下一个指针更改为要移动的节点即可.
To change a node's position, just change it's next pointer to the next node address, and the previous node's next pointer to the node you want to move and that's done.
这篇关于在C中的链表中更改索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!