Reputation: 35
I have three same type linked list nodes, schcurr, search and schtemp.However only the search has links. schcurr and schtemp are not linked. I use search to find the place I want to put schcurr and I use schtemp just to help me for keeping links. If the following conditions hold, schcurr dominates search. So, I want to replace search with schcurr. Search is already in the list and schcurr is not connected to anything yet.
I put the related part of the code as follows but I am stucked there:(
P.S: TC1 is int and WF1 is float and they are not NULL.
if(schcurr.TC1==search.TC1)
{
if(schcurr.WF1>search.WF1)
{
//schcurr dominates search.
schcurr.next=seach.next;
schcurr.prev=seach.prev;
}
}
Now I need to correct the search.prev's next and search.next's prev. I guess I can't set search.prev.next=schcurr; and seach.next.prev=schcurr; can I?
Any help is appreciated.
For information definition of the nodes:
struct schedules{
float WF1;
int TC1;
schedules *prev;
schedules *next;
};
struct schedules *sch;
struct schedules *head;
struct schedules *tail;
struct schedules *schtemp;
struct schedules *search;
Upvotes: 0
Views: 2759
Reputation: 332
If your doubly linked list is correct, in that you can traverse both ways from your search node (both prev and next), then you can do exactly what you said.
search.prev.next = schcurr;
search.next.prev = schurr;
Edit: You need to do null checks if you do this, however. Since search.prev
, for example, could point to null if you're at the head of your linked list already. Similarly, if you're at the end of your linked list, search.next
would be null, so if you do search.next.prev = something
, you would effectively be doing null.next = something
and be in violation.
Upvotes: 1