bobaloogie
bobaloogie

Reputation: 45

How to remove duplicates from an unsorted linked list

I am getting a segfault with my current remove_repeats function.

remove_repeats function:

void remove_repeats(node*& head){
    node* cursor = head;
    node* ptr;
    node* duplicate;

    while(cursor != NULL){
        ptr = cursor;
        while(ptr -> link != NULL){
            if(cursor -> data == ptr -> link -> data){
                duplicate = ptr -> link;
                ptr -> link = ptr -> link -> link;
                delete duplicate;
            }
            ptr = ptr -> link;
        }
        cursor = cursor -> link;
    }
}

main:

int main(){
    int start, stop;
    int split;
    node* head = NULL;
    node *lesser;
    node * greater;

    start = time(NULL);
    build_list(head);
    stop = time(NULL);
    cout<<"Time to build list = "<<stop - start <<"seconds.\n";
    start = time(NULL);
    show_list(head);
    stop = time(NULL);
    cout<<"Time to print list = "<<stop - start<<"seconds.\n";
    cout<<"Size of the list = "<<size(head)<<endl;
    remove_repeats(head);



return 0;
}

In the main, the build_list function builds a linked list of 2000 random integers ranging from 1 to 500.

The show_list function outputs the contents of the linked list to the screen.

The size function returns the number of nodes in the linked list.

I think the problem is when the last nodes data is a repeat and there is no node after that to be assigned to the link of ptr. That may not be the case, but if it is I am not sure how to deal with this.

Upvotes: 2

Views: 673

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

This statement

ptr = ptr -> link;

shall be a sub-statement of the else part of the preceding if statement. Here you are.

void remove_repeats( node*&head )
{
    for ( node *cursor = head; cursor != nullptr ; cursor = cursor->link )
    {
        for ( node *ptr = cursor; ptr->link != nullptr; )
        {
            if ( cursor->data == ptr->link->data )
            {
                node *duplicate = ptr->link;
                ptr = ptr->link->link;
                delete duplicate;
            }
            else
            {
                ptr = ptr->link;
            }
        }
    }
}

An alternative function definition can look the following way.

void remove_repeats( node*&head )
{
    for ( node *cursor = head; cursor != nullptr ; cursor = cursor->link )
    {
        for ( node **ptr = &cursor->next; *ptr != nullptr; )
        {
            if ( cursor->data == ( *ptr )->data )
            {
                node *duplicate = *ptr;
                *ptr = ( *ptr )->link;
                delete duplicate;
            }
            else
            {
                ptr = &( *ptr )->link;
            }
        }
    }
}

Upvotes: 2

Tung Nguyen
Tung Nguyen

Reputation: 104

If the last element of linked-list is removed -> p = p-> link will lead to null pointer

while(ptr -> link != NULL){
            if(cursor -> data == ptr -> link -> data){
                ** delete ptr-> link (which is last element)
            }
            ptr = ptr -> link ( p will be null);
}

Upvotes: 0

Related Questions