简体   繁体   English

编辑链接列表中的节点

[英]Editing a node in a Linked list

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: 例如,我根据他们的成绩安排了3个学生信息:

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: 然后我需要将student2的成绩编辑为75,因此编辑后的链表现在应按如下方式排列:

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. 或者有更好的方法来解决我的问题。

You can accomplish your goal by 你可以通过实现目标

  • Removing target node 删除目标节点
  • Editing target node data 编辑目标节点数据
  • Reinsert the node using your existing logic for inserting nodes. 使用现有逻辑重新插入节点以插入节点。

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) 从链表中删除节点(通过设置previous_node-> 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) 拼接编辑到列表中(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: 我会做的是:

  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. 如果您已经有这样的机制,则在找到重新插入节点的位置时可能会更快地使用它。

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: 对于排序部分,假设您有n个节点,将每个节点i与其后的节点进行比较,如果您要比较的节点更大,则交换它们:

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. 你可以交换他们复制他们的内存,所以你不需要改变任何指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM