Searching link list recursiverly in c -


I'm new to data structures I've written a simple program to search for an integer in a link list function It is believed to bring the pointer back to the relevant position, but when it is called in the main () it can be empty (or just waste) (printf message is inserted for diagnosis.)

Link list:

  structure list {int data; Structure list * link; };  

Search function:

  struct list * search_list (struct list * head_ptr, int x) {printf ( "\ nfunc looking head addr: 0x% x ", Head_ptr); If (head_ptr == NULL) {printf ("got tap. \ N"); Return tap; } And if (head_prit-> data == x) {printf ("retaining element: 0x% x", head_ptr); Return head_ptr; } Else {printf ("not found, next"); Search_list (head_ptr-> link, x); }}  

Call inside the main:

  tmp_list_ptr = list_head_ptr; Tmp_list_ptr = search_list (list_head_ptr, 15); If (tmp_list_ptr == NULL) printf ("main: not found: 0x% x \ n", tmp_list_ptr); Other printf ("main: found: 0x% x \ n", tmp_list_ptr);  

return 0;

The program output:

  [0X1dd33250: 19,] [0X1dd33230: 18,] [0X1dd33210: 17,] [0X1dd331f0: 16,] [0X1dd331d0: 15,] [0X1dd331b0: 14,] [0X1dd33190: 13,] [0X1dd33170: 12,] [0X1dd33150: 11,] [0X1dd33130: 1,] [0X1dd33110: 2,] [0X1dd330f0: 3,] [0X1dd330d0: 4,] [0X1dd330b0 : 5,] [0X1dd33090: 6,] [0X1dd33070: 7,] [0X1dd33050: 8,] [0X1dd33030: 9,] [0X1dd33010: 10,] to search for 15. function looking head addr: 0X1dd33250 not found, the next event Search head addr: 0X1dd33230 not found, the function searches head addr: 0X1dd33210 not found, the function searches head addr: 0X1dd331f0 not found, the function searches head addr: 0X1dd331d0 element found Retaining: 0X1dd331d0 key was not found: 0X0  

I have reasons to return mainly back? could you help me please? Note: Taken from Example: Algorithm Design Manual, Steven S. Skiana, 2nd Edition

Your recursive calls search_list () should actually be the result of return instead, your function is calling search_list () recursively and then indirectly removes your return value Throw

It should have generated a compiler warning, such that "control reaches the end of non zero function."

Depending on the architecture of your computer, The results can end with this, this is what we see as "undefined behavior"! For example, on 32-bit Intel processors, the return value of the function is stored in the eax register. How to use the processor's registers based on the program set by the compiler, your function will have returned the results of any other computation.


Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -