-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
150 lines (88 loc) · 2.13 KB
/
main.cpp
File metadata and controls
150 lines (88 loc) · 2.13 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <stdlib.h>
typedef struct Date{
}Date;
typedef struct Car{
int avabileForRent;
int mileage;
int isInRepair;
}Car;
typedef struct CarNode{
Car * data;
CarNode * next;
}CarNode;
void printSingleNode(const CarNode *head){
if(head !=NULL){
printf("mileage: %d\n",head->data->mileage);
}
}
void printList(const CarNode *head){
while(head != NULL){
printSingleNode(head);
head = head->next;
}
}
int hasLessMileage(const Car*a , const Car*b ){
if(a ->mileage < b -> mileage){
return 1;
}
else{
return 0;
}
}
int push(CarNode* head, Car * input,int(*compar)(const Car*,const Car*)){
if(head -> data ==NULL&& head->next ==NULL){
head ->data = input;
return -1;
}
while(head->next !=NULL){
if(compar(input,head->next->data )){
CarNode * temp = (CarNode*)malloc(sizeof(CarNode));
temp->data = input;
temp->next = head->next;
head->next = temp;
return -1;
}
head = head-> next;
}
CarNode * add = (CarNode*)malloc(sizeof(CarNode));
head -> next = add;
head -> next-> data =input;
head -> next ->next = NULL;
return -1;
}
void freeBoth(CarNode *head){
free(head->data);
free(head);
}
void freeAllCarNodes(CarNode * head){
CarNode*temp = (CarNode*)malloc(sizeof(CarNode));
while(head ->next !=NULL){
temp = head;
head = head->next;
freeBoth(temp);
}
freeBoth(head);
}
int main(void){
int (*mileagefunc)(const Car*,const Car*);
mileagefunc = &hasLessMileage;
CarNode * headtest = (CarNode*)malloc(sizeof(CarNode));
headtest->data = NULL;
headtest->next = NULL;
Car * data1 = (Car*)malloc(sizeof(Car));
Car * data2 = (Car*)malloc(sizeof(Car));
Car * data3 = (Car*)malloc(sizeof(Car));
Car * data4 = (Car*)malloc(sizeof(Car));
data1->mileage = 8;
data2->mileage = 7;
data3->mileage =5;
data4 ->mileage = 10;
push(headtest,data1,mileagefunc);
push(headtest,data2,mileagefunc);
push(headtest,data3,mileagefunc);
push(headtest,data4,mileagefunc);
printList(headtest);
freeAllCarNodes(headtest);
return 0;
}