Thread: Deleting duplicates in a linked list

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    4

    Deleting duplicates in a linked list

    I need to create a function that deletes the duplicates from a linked list this , I think looks right but it causes an application error saying the memory at a certain location could not be read

    void delrep (node *&head)
    { node *previous = head;

    for (node *head2 = head ; head2->link != NULL;head2=head2->link)
    {

    for (node *compare =head->link;compare ->link !=NULL; compare=compare ->link)
    {

    if ( compare ->data == head2->data)
    {

    previous->link = compare->link;

    delete compare;

    if(compare->link == NULL)

    break;

    }

    previous = compare;

    }

    }

    }
    Any ideas whats wrong with it?

  2. #2
    Unregistered
    Guest
    first guess--look at the lines below as copied from posted code:

    delete compare;

    if(compare->link == NULL)

    break;

    }

    previous = compare;


    Now ask yourself what is the value of compare->link and compare after compare is deleted?????

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Is this an ordered list? You might have an easier time getting rid of duplicates in a sorting algorithm.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    4
    Is it possible to stp the pointer at the node before the one I want to delete like loop until the pointer ->link-> data, sort of.I need to create a pointer pointing at the node before the one I want to remove. How?

  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I'm not sure what you mean, but if you want to traverse the list backwards then you can either make your list doubly linked, or use a further copy(iterator) of the head node to traverse your list until iterator->next is equal to the node that you require the previous node. Something like -

    node* iterator = head;
    node* prevnode;

    while(iterator->next!=compare)
    iterator=iterator->next;

    prevnode=iterator;
    zen

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    4
    is this any closer

    void delrep (node *&head)
    { node *remove = head;

    for (node *head2 = head ; head2->link != NULL;head2=head2->link)
    {
    int i =1;
    for (node *compare =head->link;compare ->link !=NULL; compare=compare ->link)
    { i++;

    if ( compare ->data == head2->data)
    { node *bridge = head ;
    for( int x =1 ;x < i-1; x++)
    {
    bridge = bridge ->link;
    }

    remove->link = compare->link;
    bridge ->link = compare ->link ;

    delete remove;

    //if(compare->link == NULL)

    //break;

    }

    //previous = compare;

    }

    }

    }

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    4
    Zen,
    I tried adding your code , I get some error message when I run it

    did I add it right?

    void delrep (node *head)
    { node *remove = head;

    node *head2;
    head2 = head;
    while (head2->link!=NULL)
    {

    for (node *compare =head;compare ->link !=NULL; compare=compare ->link)
    {


    if ( compare ->data == head2->data)
    {
    node* iterator = head;
    node* prevnode;
    prevnode = new node;

    if (iterator != NULL)
    {
    while(iterator->link!=compare)
    iterator=iterator->link;
    };

    prevnode=iterator;

    compare->link= remove ;
    prevnode ->link = compare ->link ;

    delete remove;
    remove = NULL;


    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  4. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM