Monday 15 July 2013

c - Stuck on linked list exercise -


I wrote this function that creates nodes.

When I add only one node, the program works, but if I add another node I get a split mistake, so obviously the problem is "second" of "add_node ()" Is in the part, but I can not understand it

  #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; Typedf Structure node {four * city; Structure node * next; } Node; Node * start = null; Node * on; Zero add_node (char * cityname) {node * y = molok (node)); Y-> city = city name; Y-> gt; Next = null; Current = start; If (start == tap) {start = y; } And {while (current! = Null) {current = current-> next; } Current-> Next = y; }} Int main () {add_node ("Paris"); Add_node ("London"); Current = start; While (current! = Null) {printf ("% s \ n", current-> city); Current = current-> next; }}    

You have a loop that is current Takes the tap up ... then you set the current-> to y , but current is essentially zero.

A simple way to fix this is to change the loop

  (current-> next! = Null) {  

I also note that you should avoid the global current should be a local variable and start should be a parameter ... and I just call it list Call because it reflects that it should return the value (possibly new) to add_node to list .

No comments:

Post a Comment