The problem is in this code part:
temp = t;
t->next = temp;
t = t->next;
When execution reaches that code,
t
is the last node. By assigning
temp
to
t
, it is effectively the same as
t->next = t;
which results in an endloss loop when searching for the last node the next time. The solution is to remove the first and last (useless) statement from the above block.
But there are more problems in your code (I did not checked all the code):
- You are mixing C++ memory allocation (
new
) with C allocations (malloc
). Never do such.
- You have
count[er]
and size
local variables in your functions. If you intend to use those globally you have to make them static
or (better) being class members.
- The local (see above)
counter
variable in your delete_node()
function is initialised with zero so that the functions always returns when passing a positive node_no
.
Finally note that a compilation without errors does not mean that the program is working as intended or did not even crash. In fact, most errors are not catched by the compiler.
A tip: Use a high warning level (/W4 with VS or -Wall with GCC). That will find at least some possible problems.