0

i want to append a node in the linked list
when i append this as

struct node*p;
p=NULL;
append(p,1);

where function is declared as

append(struct node*q int num)

i append three node,
after this i call the function for counting the node,
i get the total no node is 0, while i have appended the node.
what is the error...
please help me...

2
Contributors
1
Reply
4
Views
4 Years
Discussion Span
Last Post by Schol-R-LEA
0

This issue is simple: you are trying to append() a NULL pointer, which (depending on how your append() function works) will either return an error or silently fail, as a NULL node is the same as not appending a node at all.

In order to actually append a new node, you need to allocate that node first:

p = malloc(sizeof(struct node));

Otherwise, you are saying, 'Add a node that does not exist', which is the same as not adding a node in the first place.

Is the int argument of the append() function supposed to be the number of nodes added, or the location to add the node to? EIther one presents possible problems. If it is a number of nodes to be appended, then how are you appending more than one node at a time without using varargs? If, on the other hand, it is a location index, how do you handle the case where there are fewer nodes in the list than the index value?

Could you post the code for your append() and count() functions, please? That way we could tell you if there are any problems with them.

Edited by Schol-R-LEA

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.