Hi! I've got this structure struct *cell{int row,int column,int distance, struct cell *next}. By calling a function, I fill a linked list . The problem is that I cannot find how to delete the nodes that have the same row and column.Let me provide an example. Let's say that the linked list has the following nodes:
6 4 0
5 4 0
6 5 0
6 4 1 and so on... in this case,as we see, the first (6 4 0) and the last node (6 4 1) have the same row and column number (I don't care about the distance), so I must delete the last node.Can you show me how to do that?

thank you in advance!

Recommended Answers

All 5 Replies

Instead of having a function that removes duplicates, why not design the linked list functionality so that it will not accept duplicates..e.g. inserting a new node that results in a duplicate will do nothing.

You are right. I've also tried that,but I can't get it to work properly.Any of those two solutions is ok,but I don't know how to do it...

Can we see what you have so far?

I don't see how searching for a duplicate before insertion is problematic. Just traverse the list and if you get to the end, append a new node:

struct node *append_no_dup(struct node *head, struct node *new)
{
    if (head== NULL)
        head = new;
    else if (compare(head, new) != 0) {
        struct node *curr = head;

        while (curr->next != NULL && compare(curr->next, new) != 0)
            curr = curr->next;

        if (curr->next == NULL)
            curr->next = new;
    }

    return head;
}

It's basically the same idea as inserting into a sorted list.

I have an already existing list ,lets say list1,with 6 4 0, 5 4 0, 6 5 0, and another list, list 2 is produced by calling a function. The list that is produced is of the same structure as list 1. How can I compare them? I'm interested in comparing the first 2 numbers of every node of the first list with the first 2 numbers of every node of the second list.Shouldn't it be something like that?

current1=head1;
current2=head2;
while(current1!=NULL)
 {while(current2!=NULL
    {if ((current1->row)!=(current2->row)||(current1->column)!=(current2->column))
       {"update the first list with the node from the second list"
         current2=current2->next;}
   else current2=current2->next;}
current1=current1->next;}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.