Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void countDuplicates(struct node* head) {
- struct numberCounts {
- int value;
- int count;
- };
- typedef struct numberCounts numberCounts;
- struct node *temp = head;
- int length = 0;
- while(temp != NULL) {
- length++;
- temp = temp->next;
- }
- numberCounts *numCts = (numberCounts*) calloc (length, sizeof(numberCounts));
- length = 0;
- temp = head;
- while(temp != NULL) {
- int i = 0;
- for(i = 0; i < length; i++)
- if (numCts[i].value == temp->data) {
- numCts[i].count++;
- i = -1;
- break;
- }
- if (i != -1) {
- length++;
- numCts = (numberCounts*) realloc (length, sizeof(numberCounts));
- numCts[length - 1].value = temp->data;
- numCts[length - 1].count = 1;
- }
- temp = temp->next;
- }
- for (int i = length - 1; i > 0; i--) {
- for (int j = 1; j <= i; j++) {
- if (numCts[j-1].value > numCts[j].value) {
- int tempVal = numCts[j-1].value;
- int tempCount = numCts[j-1].count;
- numCts[j-1].value = numCts[j].value;
- numCts[j-1].count = numCts[j].count;
- numCts[j].value = temp->value;
- numCts[j].count = temp->count;
- }
- }
- }
- for (int i = 0; i < length; i++)
- printf("The %d data was found %d times\n\n", numCts[i].value, numCts[i].count);
- free(numCts);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement