Editing a node in a Linked list

*爱你&永不变心* 提交于 2019-12-10 16:17:33

问题


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.


回答1:


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.




回答2:


You can accomplish your goal by

  • Removing target node
  • Editing target node data
  • Reinsert the node using your existing logic for inserting nodes.



回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/4399169/editing-a-node-in-a-linked-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!