I am trying to print all the occurrence of duplicates in the linked-list, but I'm not able to print the last one.
I have also tried to store last duplicate string ptr1->name
in a temporary variable and print it after while loop ends but it doesn't work with different pair of values, only the last one, also I cannot find a way to print the location of last duplicate string.
If there's no better way to print the last duplicate strings, how I can ignore only first occurrences ?
Example:
List :
1 7001 Sanjana
2 7014 Aishwarya
3 7025 Ranjit
4 7017 Gurudas
5 7101 Deeksha
6 7023 Indu
7 7017 Gurudas
8 7001 Sanjana
9 7017 Gurudas
10 7016 Kiran
11 7020 Rajni
12 7020 Rajni
Output Desired :
1 7001 Sanjana
4 7017 Gurudas
7 7017 Gurudas -- Dup
8 7001 Sanjana -- Dup
9 7017 Gurudas -- Dup
11 7020 Rajni
12 7020 Rajni -- Dup
Edit:
Found Solution with help of @peal-mazumder
Solution:
void duplicate(void){
int count = 0;
int loc1 = 0; // 1st Pointer location
int loc2 = 0; // 2nd Pointer location
struct node * ptr1, * ptr2;
bool vis[5000] = {false};
ptr1 = root;
while (ptr1!=NULL){
loc1++;
// if node is already marked for duplicate it will skip compare and move to next node.
if (vis[loc1]) {
ptr1 = ptr1->link;
continue;
}
ptr2 = ptr1->link;
loc2 = loc1;
while(ptr2!=NULL){
loc2++;
if ((ptr1->num == ptr2->num) && !(strcmpi(ptr1->name, ptr2->name))){
count++;
if (count == 1) printf("Duplicate Search Results: \n");
// delete below if statement if original not to be printed.
if (!vis[loc1]) printf(" %d--> %02d %s\n",loc1, ptr1->num, ptr1->name); // print original
printf(" %d--> %02d %s --> Dup\n", loc2, ptr2->num, ptr2->name); // print duplicate
vis[loc2] = true; // marked for duplicate.
vis[loc1] = true; // not required if original not to be printed.
}
ptr2 = ptr2->link;
}
ptr1 = ptr1->link;
}
if (!count) {
printf("No duplicates found in the list!\n");
}else{
printf("Total (%d) duplicates.\n", count);
}
}
Output :
1--> 7001 Sanjana
8--> 7001 Sanjana --> Dup
4--> 7017 Gurudas
7--> 7017 Gurudas --> Dup
9--> 7017 Gurudas --> Dup
11--> 7020 Rajni
12--> 7020 Rajni --> Dup
Total (4) duplicates.