-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortlistbuyers.c
91 lines (85 loc) · 2.21 KB
/
sortlistbuyers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "template.h"
seller_buyer* divideList(seller_buyer *buyerlisthead) {
seller_buyer *fast,*slow,*temp=NULL;
fast=slow=buyerlisthead;
if(buyerlisthead==NULL || buyerlisthead->next==NULL) temp=NULL;
else {
fast=slow->next;
while(fast!=NULL && fast->next!=NULL) {
fast=fast->next->next;
if(slow!=NULL) slow=slow->next;
}
temp=slow->next;
slow->next=NULL;
}
return temp;
}
seller_buyer* mergeList(seller_buyer* first, seller_buyer* second) {
seller_buyer *retval=NULL, *ptr1=first, *ptr2=second, *tail=NULL;
if(first==NULL) retval=second;
else if(second==NULL) retval=first;
else {
if(ptr1->totalenergy<ptr2->totalenergy) {
retval=tail=ptr1;
ptr1=ptr1->next;
}
else {
retval=tail=ptr2;
ptr2=ptr2->next;
}
while(ptr1!=NULL && ptr2!=NULL) {
if(ptr1->totalenergy<ptr2->totalenergy) {
tail->next=ptr1;
ptr1=ptr1->next;
}
else {
tail->next=ptr2;
ptr2=ptr2->next;
}
tail=tail->next;
}
if(ptr1!=NULL) tail->next=ptr1;
else tail->next=ptr2;
}
return retval;
}
seller_buyer* sortListBuyers(seller_buyer *buyerlisthead) {
seller_buyer* retval,*second;
if(buyerlisthead==NULL || buyerlisthead->next==NULL) retval=buyerlisthead;
else {
second=divideList(buyerlisthead);
buyerlisthead=sortListBuyers(buyerlisthead);
second=sortListBuyers(second);
retval=mergeList(buyerlisthead,second);
}
return retval;
}
seller_buyer* calculateEnergy(seller_buyer *buyerlisthead) {
seller_buyer* temp=buyerlisthead;
TransNode* temp2;
int energy=0;
if(temp==NULL) printf("Please create the list of buyers first\n");
while(temp!=NULL) {
temp2=temp->memberhead;
while(temp2!=NULL) {
energy=energy+temp2->entry.amtOfEnergy;
temp2=temp2->next;
}
temp->totalenergy=energy;
energy=0;
temp=temp->next;
}
if(buyerlisthead!=NULL) {
buyerlisthead=sortListBuyers(buyerlisthead);
temp=buyerlisthead;
while(temp!=NULL) {
printf("\nBuyer ID: %u\n",temp->id);
printf("Energy Bought: %f\n",temp->totalenergy);
temp=temp->next;
}
}
return buyerlisthead;
}