编辑链接列表中的节点
我正在创建一个学生列表(链接列表),可以添加、查看和编辑学生信息。我有两个字段,即学生姓名和学生成绩,我在列表中添加新学生,其方式是根据学生的成绩降序排列。
我已经完成了添加和查看部分。问题出在编辑部分,因为我需要编辑信息,然后我需要再次对其进行排序,以便它位于列表的正确位置。
例如,我有3个学生的信息按照他们的成绩排列:
student1 90 -> student2 85 -> student3 80 -> NULL
那么我需要将学生2的成绩编辑为75,所以编辑后的链表现在应该排列如下:
student1 90 -> student3 80 -> student2 75 -> NULL
我应该怎么做?您不需要给我任何代码。我只是想要一些关于如何实现程序的编辑部分的建议。我正在考虑创建一个新节点(带有编辑后的信息),删除旧节点并将编辑后的节点插入列表中。我的逻辑正确吗?或者有更好的方法来解决我的问题。
I am creating a student list (linked list) that can add, view and edit student information. I have two fields namely Student Name and Student Grade and I add new students in the list in a way that it is sorted according to the student's grades in descending order.
I have finished doing the add and view portion. The problem is on the edit part because I need to edit the information, then I need to sort it again so that it would be on the proper location of the list.
For example, I have 3 students information arranged according to their grades:
student1 90 -> student2 85 -> student3 80 -> NULL
Then I need to edit student2's grade to 75 so the edited linked list should now be arranged as follows:
student1 90 -> student3 80 -> student2 75 -> NULL
How should I do that? You don't need to give me any code. I just want some advices on how I can implement the edit part of my program. I am thinking of creating a new node (with the edited info), delete the old node and insert the edited node into the list. Is my logic correct? or is there a better way on solving my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过以下方式实现您的目标:
You can accomplish your goal by
单链表?
找到要编辑的节点,然后保留指向前一个节点的指针,或者编写一个例程来检索前一个节点。
从链接列表中删除您的节点(通过设置 previous_node->next to thisOne->next)
进行编辑。
将新节点插入列表中的正确位置(通过遍历列表直到下一个节点小于您编辑的值。
拼接到列表中(editedNode->next = nextNode; current->next =editedNode)
将编辑的节点 双向链表您可以使用“其他”/后退/向上链接来查找前一个节点
Singly linked list?
Find the node you want to edit, and either keep a pointer to the previous node or write a routine to retrieve the previous node.
Remove your node from the linked list (by setting previous_node->next to thisOne->next)
Make your edits.
Insert the new node in the right place in the list (by traversing the list unti the next node is less than your edited value.
Splice edited into the list ( editedNode->next = nextNode; current->next = editedNode)
With doubly linked lists you can just use the "other"/back/up link to find the previous node
基本上你的想法是正确的,除了我不会创建一个新节点。我要做的是:
请注意,将列表索引到数组等中可能会比线性遍历提供更快的搜索速度。如果您已经有了这样的机制,那么在查找重新插入节点的位置时使用它可能会更快。
Basically your idea is correct except that I wouldn't create a new node. What I would do would be:
Note that indexing the list into an array, etc. may give a quicker search than a linear traversal. If you already have such a mechanism it may be quicker to use that when finding the location to re-insert the node.
您可以执行编辑指定节点的功能。扫描列表直到找到该节点,然后直接编辑它。当然你会使用指针。对于排序部分,假设你有 n 个节点,将每个节点 i 与其后面的节点进行比较,如果你要比较的节点更大,则交换它们:
你可以交换它们复制它们的内存,所以你不需要改变任何指针。
You could do a function that edits a specified node. Scan the list until you find that node and then directly edit it. Of course you will use a pointer. For the sorting part, say you have n nodes, compare every node i with the nodes after it and swap them if the one you are comparing with is larger:
you can swap them copying their memory, so you won't need to change any pointer.