Home > Blockchain >  Print duplicate entries in C linked list with location number
Print duplicate entries in C linked list with location number

Time:03-14

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--> d %s\n",loc1, ptr1->num, ptr1->name); // print original

                printf(" %d--> d %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.

CodePudding user response:

8 --> 7001 Sanjana
9 --> 7017 Gurudas
12 --> 7020 Rajni
Total (3) duplicates.

If you are trying to print something like that then check below code otherwise add your required output for your given Example Case on your description.

struct node {
    int num;
    char name[20];
    struct node* link;
} * root;


// function

void duplicate(void){
    int count = 0;
    int loc = 0; // Location in the list.
    struct node * ptr1, * ptr2;
    bool vis[200000] = {false};
    ptr1 = root; // root is global pointer variable.
    while (ptr1->link!=NULL) {
        loc  ;
        if(vis[ptr1->num]) { // if i already processed for same num value then we don't need to check duplicate for this again.
            ptr1 = ptr1->link;
            continue;
        }
        vis[ptr1->num] = true;
        ptr2 = ptr1->link;
        int Ptr2Location = loc   1, lastDupLocation = 0;
        struct node *temp; // to store last duplicate node
        while(ptr2 != NULL) {
            if ((ptr1->num == ptr2->num) && !(strcmpi(ptr1->name, ptr2->name))){
                temp = ptr2;
                lastDupLocation = Ptr2Location;
            }
            Ptr2Location  ;
            ptr2 = ptr2->link;
        }
        if(lastDupLocation)
            printf(" %d --> d %s\n", lastDupLocation, temp->num, temp->name), count  ;
        ptr1 = ptr1->link;
    }

    if (!count) {
        printf("No duplicates found in the list!\n");
    }else {
        printf("\nTotal (%d) duplicates.\n", count);
    }
}
void insert(int num, char str[20], int len)
{
    if(root == NULL) { //If the list is empty
        root = new node();
        root->num = num;
        strcpy(root->name, str);
        root->link = NULL;
    }
    else{
        node* current_node = root; //make a copy of root node
        while(current_node->link!=NULL) //Find the last node
        {
            current_node=current_node->link; //go to next address
        }
        node *newnode = new node(); //create a new node
        newnode->num = num;
        strcpy(newnode->name, str);
        newnode->link = NULL;

        current_node->link=newnode; //link the last node with new node
}

}

int main()
{
    int num, n, i;
    cin>>n;
    char name[20];
    for (i = 0; i<n; i  )
    {
        scanf("%d %s", &num, name);
        insert(num, name, strlen(name));
    }
    duplicate();
    return 0;
}

You can sort this according to their location by storing these values in a data structure or a structure.

1 --> 7001 Sanjana
8 --> 7001 Sanjana
4 --> 7017 Gurudas
7 --> 7017 Gurudas
9 --> 7017 Gurudas
11 --> 7020 Rajni
12 --> 7020 Rajni

Code

void duplicate(void){
    int count = 0;
    int loc = 0; // Location in the list.
    struct node * ptr1, * ptr2;
    bool vis[200000] = {false};
    ptr1 = root; // root is global pointer variable.
    while (ptr1->link!=NULL) {
        loc  ;
        if(vis[ptr1->num]) { // if i already processed for same num value then we don't need to check duplicate for this again.
            ptr1 = ptr1->link;
            continue;
        }
        ptr2 = ptr1->link;
        int Ptr2Location = loc   1;
        while(ptr2 != NULL) {
            if ((ptr1->num == ptr2->num) && !(strcmpi(ptr1->name, ptr2->name))){
                if(!vis[ptr1->num])
                    printf(" %d --> d %s\n", loc, ptr1->num, ptr1->name), count  ;
                printf(" %d --> d %s\n", Ptr2Location, ptr2->num, ptr2->name);
                vis[ptr1->num] = true;
            }
            Ptr2Location  ;
            ptr2 = ptr2->link;
        }
        ptr1 = ptr1->link;
    }
}
  • Related