编辑链接列表中的节点

发布于 2024-10-07 07:03:13 字数 516 浏览 3 评论 0原文

我正在创建一个学生列表(链接列表),可以添加、查看和编辑学生信息。我有两个字段,即学生姓名和学生成绩,我在列表中添加新学生,其方式是根据学生的成绩降序排列。

我已经完成了添加和查看部分。问题出在编辑部分,因为我需要编辑信息,然后我需要再次对其进行排序,以便它位于列表的正确位置。

例如,我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

又爬满兰若 2024-10-14 07:03:13

您可以通过以下方式实现您的目标:

  • 删除目标节点
  • 编辑目标节点数据
  • 使用现有的插入节点逻辑重新插入节点。

You can accomplish your goal by

  • Removing target node
  • Editing target node data
  • Reinsert the node using your existing logic for inserting nodes.
仙女山的月亮 2024-10-14 07:03:13

单链表?

找到要编辑的节点,然后保留指向前一个节点的指针,或者编写一个例程来检索前一个节点。

从链接列表中删除您的节点(通过设置 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

北方的巷 2024-10-14 07:03:13

基本上你的想法是正确的,除了我不会创建一个新节点。我要做的是:

  1. 确定值是否增加或减少(并更新节点)。
  2. 从列表中取消节点链接。
  3. 向前或向后搜索(取决于等级的增加或减少)以找到正确的位置。
  4. 将节点链接到新位置。

请注意,将列表索引到数组等中可能会比线性遍历提供更快的搜索速度。如果您已经有了这样的机制,那么在查找重新插入节点的位置时使用它可能会更快。

Basically your idea is correct except that I wouldn't create a new node. What I would do would be:

  1. Identify if value has increased or decreased (and update node).
  2. Unlink node from the list.
  3. Search forwards or backwards (depending on increase or decrease of grade) for correct location.
  4. Link node into new location.

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.

缱绻入梦 2024-10-14 07:03:13

您可以执行编辑指定节点的功能。扫描列表直到找到该节点,然后直接编辑它。当然你会使用指针。对于排序部分,假设你有 n 个节点,将每个节点 i 与其后面的节点进行比较,如果你要比较的节点更大,则交换它们:

for every node n1 in list
    for every remaining node n2 in list
        if n2->grade > n1->grade
            swap 'em

你可以交换它们复制它们的内存,所以你不需要改变任何指针。

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:

for every node n1 in list
    for every remaining node n2 in list
        if n2->grade > n1->grade
            swap 'em

you can swap them copying their memory, so you won't need to change any pointer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文