I've written the function for removing duplicate elements in a doubly linked circular list. However when traversing the list after removing, I am not getting the desired output.
I have used typedef to enclose
#define MAX 10
typedef struct node {
int data;
struct node *next, *prev;
} NODE;
NODE *rem(NODE *head) {
NODE *node = head;
int hash[MAX] = { 0 };
while (node != NULL) {
hash[node->data]++;
node = node->next;
}
node = head;
NODE *node2 = head->next;
while (node2 != NULL) {
if (hash[node2->data] != 1) {
NODE *r = node2;
node->next = node2->next;
node2->next->prev = node;
free(r);
hash[node->data]--;
}
node = node2;
node2 = node2->next;
}
return head;
}
Using the debugger shows segmentation fault in line:
hash[node->data]++;
I have created a hash array to keep count of duplicate elements and keep removing the elements until the count of each element is 1.
But traversing the list after the function does not give any output.
hash[node->data]++;-> If you code giving segmentation fault here then there is a possibility thatnode->datahas value greater than or equal to10or less than0. You need to provide more information around the problem occurring. Is your linked list sorted or unsorted? There are few problems in your code like -whileloop starting withhead->next, missing theheadnode duplicate check -NODE \*node2=head-\>next;. Post the minimal verifiable code. Also, while posting, ensure the proper indentation in your code.