each person have a Rank[4]
struct node
{
int value;
int Rank[4];
struct node *next;
};
i want to know how many people choose the same Rank ?
for example :
person 1 : Rank[4] = {1, 2, 3, 4}; & person 2 : Rank[4] = {1, 2, 3, 4};
that should count as duplicate
CodePudding user response:
The following code does the job, in this case, I used a 'this_rank' to count how many matches this in the list, but you can use any rank that already exists in the list.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int value;
int Rank[4];
struct node *next;
};
int rank_test(int *rank_1, int *rank_2, int rank_counts)
{
int r = 1;
for(int n = 0; n < rank_counts; n )
{
if(rank_1[n] != rank_2[n])
{
return 0;
}
}
return r;
}
int rank_count(struct node *my_node, int *rank, int rank_length)
{
int rank_counter = 0;
while(my_node != NULL)
{
rank_counter = rank_test(my_node->Rank, rank, rank_length);
my_node = my_node->next;
}
return rank_counter;
}
int main(void)
{
struct node *n1;
struct node *n2;
struct node *n3;
int r;
int this_rank[] = {1, 2, 3, 4};
n1 = (struct node *) malloc(sizeof(struct node));
n2 = (struct node *) malloc(sizeof(struct node));
n3 = (struct node *) malloc(sizeof(struct node));
n1->next = n2;
n2->next = n3;
n3->next = NULL;
n1->Rank[0] = 1;
n1->Rank[1] = 2;
n1->Rank[2] = 3;
n1->Rank[3] = 4;
n2->Rank[0] = 1;
n2->Rank[1] = 2;
n2->Rank[2] = 3;
n2->Rank[3] = 4;
n3->Rank[0] = 1;
n3->Rank[1] = 2;
n3->Rank[2] = 3;
n3->Rank[3] = 5;
r = rank_count(n1, this_rank, 4);
printf(" r = %d", r);
return 0;
}
CodePudding user response:
Here is a bit lengthy proof of principle program that sets up some test data with duplicates that you might use for determining people that are duplicates.
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int val;
int Rank[4];
struct node *next;
} node_t;
node_t * push(node_t *nd, int val)
{
node_t *new_node; // New pointer to a node
new_node = (node_t *)malloc(sizeof(node_t));
new_node->val = val;
new_node->Rank[0] = val *5 1;
new_node->Rank[1] = val *5 4;
new_node->Rank[2] = val *5 8;
new_node->Rank[3] = val *5 9;
new_node->next = NULL;
/* Create some duplicates */
if ((val % 17) == 0)
{
new_node->Rank[0] = 4;
new_node->Rank[1] = 5;
new_node->Rank[2] = 6;
new_node->Rank[3] = 7;
}
return new_node;
}
void find_dupe(node_t *person, node_t *list)
{
node_t *comp;
comp = list;
while (comp->next != NULL)
{
if (person->Rank[0] == comp->Rank[0] && person->Rank[1] == comp->Rank[1] && person->Rank[2] == comp->Rank[2] &&person->Rank[3] == comp->Rank[3] && person != comp)
{
printf("Person %d matches with Person %d\n", person->val, comp->val);
}
comp = comp->next;
}
return;
}
int main()
{
node_t *work; // Pointer work variable for building a linked list of nodes
node_t *head;
head = (node_t *)malloc(sizeof(node_t));
head->val = 2;
head->Rank[0] = 1;
head->Rank[1] = 2;
head->Rank[2] = 3;
head->Rank[3] = 4;
head->next = NULL;
// Create a set of fifty nodes.
work = head;
for (int i = 0; i < 50; i )
{
work->next = push(work, (2 * i i 12));
work = work->next;
}
// Now travel down the chain and print out the pertinent statistics of the nodes.
work = head;
while (work->next != NULL)
{
find_dupe(work, head);
work = work->next;
}
return 0;
}
This was the output from the test run.
@Una:~/C_Programs/Console/Ranked/bin/Release$ ./Ranked
Person 51 matches with Person 102
Person 51 matches with Person 153
Person 102 matches with Person 51
Person 102 matches with Person 153
Person 153 matches with Person 51
Person 153 matches with Person 102
Give that a try if you want.