简体   繁体   中英

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:

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.

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)

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:

  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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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