0

I have a linked list created and my goal is to delete repetition of data in the linked list. There is only a data and pointer component in each node. The algorithm might not be perfect, but my concern is why am I getting the error SIGABRT in Xcode when trying to delete a node.The error occurs in code when the if condition is true. No idea what SIGABRT means.

void LinkedList::deleteRepetition(const LinkedList& list)
{
    node* temp_value = nullptr;
    node* traverser = nullptr;
    temp_value = head;
    traverser = head;
    for (int i = 0; i < NODES; i++)
    {
        for (int j = 0; j < NODES - i; j++)
        {
            traverser = temp_value->next;
            if (traverser->data == temp_value->data)
            {
                delete traverser;
            }
        }
        traverser = temp_value;
        temp_value = temp_value->next;
    }
}
J.E.C.
  • 2,616
  • 2
  • 18
  • 21

2 Answers2

0

Here your approach is wrong. Inner for loop, after iteration again accessing same memory and get corrupted. SO you need to update it inside for loop.

My approach for the same, May be it will help you

void removeDuplicates(struct node *start)
{
  struct node *ptr1, *ptr2, *dup;
  ptr1 = start;

  /* Pick elements one by one */
  while(ptr1 != NULL && ptr1->next != NULL)
  {
     ptr2 = ptr1;

     /* Compare the picked element with rest of the elements */
 while(ptr2->next != NULL)
 {
   /* If duplicate then delete it */
   if(ptr1->data == ptr2->next->data)
   {
      /* sequence of steps is important here */
      dup = ptr2->next;
      ptr2->next = ptr2->next->next;
      free(dup);
   }
     else /* This is tricky */
      {
        ptr2 = ptr2->next;
       }
     }
     ptr1 = ptr1->next;
  }
}

Using while loop i am trying to save time for checking some condition which we can ignore

someone
  • 1,638
  • 3
  • 21
  • 36
0

You second loop is iterating over removed elements once again. And you need to swap pointers when you found same node as well. Right version:

void LinkedList::deleteRepetition(const LinkedList& list)
{
  node* node1 = head;
  node* node2 = nullptr;
  node* duplicate = nullptr;

  while(node1 != nullptr && node1->next != nullptr)
  {
     node2 = node1;

     while(node2->next != nullptr)
     {
       if(node1->data == node2->next->data)
       {
          duplicate = node2->next;
          node2->next = node2->next->next;
          delete duplicate
       }
       else 
       {
          node2 = node2->next;
       }
     }
     node1 = node1->next;
  }
}

And be aware that next in last element must be NULL.

Megamozg
  • 975
  • 9
  • 15
  • Tried this out and it didn't work. Not sure what you meant by "iterating over removed elements". The error occurs the very first time it tries to remove a node, not after. And yes, this is for academic purposes. Just FYI, the NODES is a const int equal to 10, representing the number of nodes in the list. – J.E.C. Mar 22 '16 at 21:44
  • Edited answer, changed implementation – Megamozg Mar 23 '16 at 06:25