c - Deleting node from linked list by index -


This is my code to delete a node from a linked list.

vec_store holds seq and size . Variable keeps the seq vector and an indicator.

For any reason, else if (i <-..> Size -1) does not work which is the final position.

Can anyone solve the problem? The way this is the C code.

  zero delete_vec (vec_store s, int i) {if (i & lt; 0 | s-> Size-1 & lt; i) {printf ("vector can not be deleted Because index% d is out of range \ n ", i); } And if (i == 0) {node is temporary; Temp = s- & gt; Seq; S- & gt; Seq = s- & gt; Seq-> next; S- & gt; Size--; Free (temporary); } And if (i == s-> size -1) {node temp1, temp2; // temp1 = malloc (node); Temp2 = Molok (node); Temp1 = s- & gt; Seq; If (temp1-> Next == faucet) {Free (temp1); S- & gt; Seq = zero; S- & gt; Size--; Printf ("s-> Size -1 \ n"); } And {while (temp1-> Next! = Null) {temp2 = temp1; Temp1 = temp1- & gt; next; } Free (temp1); Temp2- & gt; Next = zero; S- & gt; Size--; Printf ("s-> Size -1 2 \ n"); }} And if (i <-> Size -1) {node temp1, temp2; Int j; Temp1 = s- & gt; Seq; Temp2 = Molec (size (structure node_record)); For (j = 0; j; l; = i-1; j ++) {temp2 = temp1; Temp1 = temp1- & gt; next; } Free (temp1); Temp2- & gt; Next = zero; S- & gt; Size--; }}   

This code is not inaccessible:

  if (temp1-> Next == tap) {free (temp1); S- & gt; Seq = zero; S- & gt; Size--; Printf ("s-> Size -1 \ n"); }  

... because temp1 is the first element in the list, and so it will only happen if the first element was also the last element - which means S- & gt; Size is <1, so it would have been previously caught by the if (i == 0) case.

This allocation temp2 (which is in two places) is false - the value of temp2 is overwritten anyway, which gives you the allocated memory Leaked:

  temp2 = molok (node); Finally, what is the problem that you are asking about the lie ( if (i <-..- Size -1)  in the case): 

< Code> free (temp1); Temp2- & gt; Next = zero;

This closes the whole closing of the list. You want to keep the tail of the list nearby - like:

  temp2-> next = temp1-> next; Free (temp1);  

By the way, temp2 and temp1 are very ineffective variable names - past and current or something else? In addition, if (i == s-> Size-1) is completely special case for unnecessary - it is controlled by you for code properly Should be done if (i <-> Size -1) case.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -