编辑链接列表中的节点 [英] Editing a node in a Linked list

查看:49
本文介绍了编辑链接列表中的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

例如,我有3个按年级排列的学生信息:

For example, I have 3 students information arranged according to their grades:

student1 90 -> student2 85 -> student3 80 -> NULL

然后,我需要将Student2的分数编辑为75,这样,已编辑的链表现在应按以下顺序排列:

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.

推荐答案

基本上,您的想法是正确的,只是我不会创建新节点.我会做的是:

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

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

请注意,将列表索引到数组等中可以比线性遍历更快地进行搜索.如果您已经拥有这种机制,则在查找位置以重新插入节点时可以更快地使用它.

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.

这篇关于编辑链接列表中的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆