recursion - Removing data from list pointed to by a pointer in C -
I'm trying for a time without a random way to delete a node (anywhere in the link list) < / P>
Is it possible ??
This is my non-recursive method that does not work:
int removeItem (struct ListNode ** headRef, int data) {struct ListNode * temp = * headRef ; // store original head int val = data; While (temp-> Next! = Faucet) {if (temp-> Next-> Data! = Val) {temp = temp- & gt; next; } And if (temp-> next-> data == wal) {temp = temp-> next; Free (temporary); Return 1; } And {return 0; }} Return 0; }
I honestly do not believe that I follow this question, but this what are you trying to do? int removeItem (struct ListNode ** headRef, int data) {if (! * HeadRef) 0; If ((* headRef - - Data! = Data) returns return item (& amp; (headRef) -> Next, data); // found it. Leave it to the composition ListNode * tmp = * headRef; * HeadRef = tmp- & gt; next; Free (tmp); Return 1; } And if so, no . It is better to do this work through repetition (it is difficult to understand that you can understand it small , but I can not imagine that, if you understand the recursive solution): < / p> int removeItem (struct ListNode ** headRef, int data) {While (* headRef & amp; amp; (* headRef) -> Data! = Data) headRef = & amp; (* HeadRef) - & gt; next; If (* headref) {struct listnode * tmp = * headref; * HeadRef = tmp- & gt; next; Free (tmp); Return 1; } Return 0; } How they work Use both of these functions by using headRef The indicator refers to the node that we are testing. Initially the address of the head indicator for the list in headRef is included every time we need to go to the next node, The code loads the code in that code that is in headRef when we finally search the node, then the reference pointer in the headRef (the original Head indicator, or some can be next indicator is somewhere in the list, which really does not matter). The difference between the two functions is straight-forward first, we use recursive to pass the address of the next indicator which we are going to check the recurring call. In the second, we only use a walking loop. Either way, headRef always indicates the indicator that we are testing while pointing to the node. If we get a winner (in this case, losers), note that both functions do the same operation : in * headRef Save the value, it's firing our pointer), populate the * headRef with the value of the current code next , then delete the old node. Pulling it on paper will help in a ton (my ascii art worthless ; sry).
Comments
Post a Comment