问题描述:

I keep getting caught in a loop but i dont know how to fix it. We have to traverse through the list. It prints but it doesn't move on to the next test function. I don't think the code is recognizing the end of the list and isn't exiting the while loop.

void traverse_int(struct s_node* head)

{

struct s_node* next_node;

int* x;

next_node=head;

while(next_node != NULL)

{

if (next_node->elem == NULL)

{

my_str("NULL");

}

else

{

x=(int*) next_node->elem;

my_int(*x);

}

my_str(" ");

next_node=next_node->next;

}

my_char('\n');

}

this is the test case that i am using:

 a = 17;

b = 10;

c = 16;

d = 95;

add_elem(&a, &head);

add_elem(&b, &head);

add_elem(&c, &head);=

add_elem(&d, &head);

traverse_int(head); my_str("\nshould print 95 16 10 17\n");

It does not print my_str it just gets caught and prints:

95 16 10 17

it should print:

95 16 10 17

should print 95 16 10 17

Please help!!

网友答案:

I'm old but with link lists like your stack thing here I see you need to assign a NULL value to the tail of your linked list first and then every time a node is added then add a NULL to the new tail. You did not assign a NULL value to your end node so you have an endless loop.

相关阅读:
Top