blank space
blank space

Reputation: 97

Segmentation fault removal duplicate elements in unsorted linked list

#include<iostream>
#include<stdlib.h>
using namespace std;

struct node{

    int data;
    struct node* next;

};

struct node* head;

void traversalLinkedList(struct node* ptr){

    if(ptr==NULL) {cout<<"null hain bhai"<<endl; return;}

    while(ptr!=NULL){
        cout<<ptr->data<<" ";
        ptr=ptr->next;
    }

}

void push(int d){

    //at the front of linked list

    struct node* temp=(struct node*) malloc(sizeof(struct node));

    temp->data=d;
    temp->next=head;

    head=temp;

}


int main(){

    head=NULL;

  //removing duplicates from linked list

  push(10);
  push(11);
  push(12);
  push(11);
  push(11);
  push(12);
  push(10);

  traversalLinkedList(head);

  struct node *ptr;

  struct node* prev;

  struct node* qtr;

  for(ptr=head;ptr->next!=NULL;ptr=ptr->next){

    prev=ptr;

    for(qtr=ptr->next;qtr!=NULL;){


      if(ptr->data==qtr->data){
        //means duplicate nodes
        prev->next=qtr->next;
        free(qtr);
        qtr=prev->next;
      }

      else{
        prev=qtr;
        qtr=qtr->next;
      }



    }

  }

  cout<<endl;

  traversalLinkedList(head);

  cout<<endl;




    return 0;
}

I am not able to understand why I am getting segmentation fault for this code. This code is removal of duplicate elements from unsorted linked list.

Explanation I am using a previous pointer to store the node previous to the inner loop pointer as soon as I find the inner loop node data is equal to outer loop node data, the previous pointer is made to point to inner loop node's next pointer and inner loop node is freed.

Upvotes: 1

Views: 138

Answers (3)

Minions
Minions

Reputation: 1313

for(ptr=head;ptr->next!=NULL;ptr=ptr->next)

In this line when u say ptr->next!=NULL, a null pointer will be dereferenced for the last node. Which gives a segmentation fault because you are trying to access a null pointer.

So, just make the second condition as ptr!=NULL. That is enough as you are anyways doing ptr=ptr->next in your third condition of the for loop

Upvotes: 2

Ishamael
Ishamael

Reputation: 12795

for(ptr=head;ptr->next!=NULL;ptr=ptr->next){

Should be

for(ptr=head;ptr!=NULL;ptr=ptr->next){

It fails because at the end only two 12s are left and the ptr is pointing at one of them. It's next is not null so you enter the iteration. On that iteration you delete the second 12, so ptr's next now is null. Then the iteration ends, you set ptr to ptr->next, so ptr itself becomes null, then you check if ptr->next is null, and crash there.

Edit: if you want to skip the last element, do

for(ptr=head;ptr && ptr->next!=NULL;ptr=ptr->next){

Upvotes: 1

Manoj
Manoj

Reputation: 11

This condition ptr->next!=NULL in the for loop is problematic.

for(ptr=head;ptr->next!=NULL;ptr=ptr->next)

It will dereference NULLpointer for the last node. It should be :

for(ptr=head;ptr!=NULL;ptr=ptr->next)

Upvotes: 1

Related Questions