As 2kaud pointed out, you need to learn to use the debugger. You cannot code without it. However, the first problem I see is these two lines.

nodeptr *k;
...
q=*k;