I am trying to create a linked list. Each node will have a straight, and there will be an indicator on the next node. When trying to remove a node from the list, the program stops due to a split error. I have tried to go through this in a few different ways that the algorithms here have tried to use, after removing the node, which I want to remove.
1. The 'Next' indicator of the previous node of the node should be removed after the node.
// Example node_t * current = head; While (current-> next! = Null) {if (current-> next-> BI <-lt; = 5) {current-> next = current-> next-> next; // first thoughts, current = current-> next; Did not work; }
It was not working. That's why I have adjusted it for
. Create an indicator on the designated node of the template.
2. Copy the node to be deleted in the template.
3. Set the 'Next' indicator of the last node on the 'next' indicator of the temporary.
4. Free temp
// example node_t * current = head; While (current-> next! = Null) {if (current-> gt; next-> gt; BA & lt; = 5) {node_t * temp; Temp = current-> next; Current-gt; Next = temp-> next; Free (temporary); } Current = current->; next; }
It still does not work. I do not really know what is wrong, for me it seems very synthetic right. I know that I should be confused somewhere how I am starting the indicators, or how to remove the node. If someone can tell me why the code is not working then I can fix it. > As mentioned in a comment, you have to see that current
is not redundant and also with current-> next
Add # & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; Typedef struct node_t {struct node_t * next; Int data; } Node_t; Static zero add_node (node_t ** head, int value); Fixed zero free_list (node_t ** head); Static zero dump_list (node_t * head); Int main (zero) {node_t * head = 0; Add_node (and head, 3); Add_node (and head, 6); Add_node (and head, 9); Add_node (and head, 4); Add_node (and head, 8); Add_node (and head, 2); Dump_list (head); Node_t * current = head; While (current! = Null & amp; current-> Next! = Null) {if (current-> next-> data & lt; = 5) {current-> gt; Next = current-> next-> gt; ;the upcoming; } Current = current->; next; } Dump_list (head); Free_list (& amp; head); Dump_list (head); Return 0; } Fixed Zero add_node (node_t ** head, int value) {node_t * node = malloc (sizeof (* node)); Node-> Data = value; Node-> Next = * head; * Head = node; } Static zero dump_list (node_t * head) {char const * pad = ""; While (head! = 0) {printf ("% s% d", pad, head-> data); Pad = "- & gt;"; Head = head-> next; } Cursed ('\ n'); } Fixed zero free_list (node_t ** head) {while (* head! = 0) {node_t * next = (* head) - & gt; next; Free (* head); * Head = next; }}
This on
and current-> > both > code>. The problem is that if you delete the last node, then the faucet is assigned to
current
, which you can not do.
NB: Returns from the code given above are not checked malloc ()
, but doing so is both lazy and bad.
Comments
Post a Comment