Hi there,

I'm having a problem with adding a data structure to an existing linked list that has highlighted a gap in my knowledge about the latter. Briefly, what I am trying to is add a 'particle' to the end (i.e. appending) of an existing linked list of particles. My 'particle' structure however contains a smaller linked list of neighbours i.e.

struct particle {
    int Id;
    double Mass;
    double Pos[4];   // (r,x,y,z)
    double Vel[3];
    double neighbourdist; //distance of neighbour to particle (only used for those in neighbourlist)
    int neighbourlistnum; //number of neighbouring particles
    struct node_def *neighbourlist;

    int cid; //Cluster id number for particle when it acts as a cluster


When adding a particle, then, I do the following:

node* add_to_clust(struct cluster *clust, struct particle *part) { 
    node *after = NULL;
    node *current;
    node *target;
    node *added;
    int i;

    //First, don't add a particle if its id is already there

    for(after = clust->clusterlist; after != NULL; after = after->next) {
	if(after->p.Id == part->Id) {
	    return clust->clusterlist;
	//previous = after;

    // Create the new node for the particle

    added = malloc(sizeof(node));
    // Update fields of particle structure
    added->p.Id = part->Id;
    added->p.Mass = part->Mass;
    for(i=0;i<4;i++) {
	added->p.Pos[i] = part->Pos[i];
    for(i=0;i<3;i++) {
	added->p.Vel[i] = part->Vel[i];

    // Need to also update neighbourlist field!

    added->p.neighbourlist = part->neighbourlist;

    // Assign cid to particle

    added->p.cid = part->cid;

    // Now append it to end of list

    current = clust->clusterlist;

    if(current == NULL) {
	//List has no elements
	printf("List currently empty\n");
	current = added;
	(clust->clusterlist) = current;
    else if(current->next == NULL) {
	//List has one element
	printf("List has one element\n");
	current->next = added;
    else {
	while(current->next != NULL) {
	    //Iterate through
	    current = current->next;
	current->next = added;
    added->next = NULL;

    return clust->clusterlist;

When I later look at the final list and the properties of all the particles in it, I find that their Id, Mass, Pos, Vel, etc. fields are all there correctly, but the neighbourlist field is empty. This clearly has something to do with neighbourlist being a pointer to another linked list, but I can't figure out why it wouldn't copy that pointer over as it did with the other fields.

Any ideas would be greatly appreciated, has anyone come across this problem before?


8 Years
Discussion Span
Last Post by Salem

Having passed a particle pointer to add_to_clust(), do you then go on to modify (or free) the neighbour list outside the function?

Because at the point you do this
added->p.neighbourlist = part->neighbourlist;
you have two particles pointing at the SAME neighbour list. If you change/delete that neighbour list in one of them, then the other is going to change in well (or be left with a garbage pointer).

Have you considered adding 'part' itself, rather than just a partial copy of it?

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.