-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.c
215 lines (195 loc) · 6.09 KB
/
list.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Structure to represent a User
typedef struct {
int floor_call; // Floor where the user called the elevator
int destination_floor; // Floor to which the user wants to go
} User;
// Node structure for the linked list of Users
typedef struct UserNode {
User* user; // Pointer to the User structure
struct UserNode* next; // Pointer to the next UserNode in the list
} UserNode;
// Linked list of Users
typedef struct {
UserNode* head; // Pointer to the first UserNode in the list
int size; // Number of elements in the list
} UserList;
// Function to create a new UserNode
UserNode* createUser(User* user) {
UserNode* newUser = (UserNode*)malloc(sizeof(UserNode));
newUser->user = user;
newUser->next = NULL;
return newUser;
}
// Function to delete the head of the list
void deleteHead(UserList* list) {
if (list->head == NULL) {
return;
}
UserNode* newHead = list->head->next;
free(list->head);
list->head = newHead;
list->size--;
}
// Function to delete a specific User from the list
void deleteUser(UserList* list, User* user) {
// Special case: if the list is empty
if (list->head == NULL)
return;
// Special case: if the value to delete is in the first node
if (list->head->user == user) {
UserNode* temp = list->head;
list->head = list->head->next;
free(temp);
list->size--;
return;
}
// Traverse the list to find the node containing the value to delete
UserNode* current = list->head;
UserNode* prev = NULL;
while (current != NULL && current->user != user) {
prev = current;
current = current->next;
}
// If the value is not present in the list
if (current == NULL)
return;
// Delete the node containing the value
prev->next = current->next;
free(current);
list->size--;
}
// Function to add a User at the head of the list
void addToHead(UserList* list, User* newUser) {
UserNode* node = createUser(newUser);
node->next = list->head;
list->head = node;
list->size++;
}
// Function to add an element at the end of the list
void addToEnd(UserList* list, User* newUser) {
UserNode* node = createUser(newUser);
if (list->head == NULL) {
// If the list is empty, the new element becomes the head of the list
list->head = node;
} else {
// Otherwise, traverse the list to the end and add the new element at the end
UserNode* current = list->head;
while (current->next != NULL) {
current = current->next;
}
current->next = node;
}
list->size++;
}
// Function to traverse the list and print the elements
void printList(UserList* list) {
if (list->size == 0)
printf("empty list");
UserNode* current = list->head;
while (current != NULL) {
printf("Call: %d , Destination: %d || ", current->user->floor_call, current->user->destination_floor);
current = current->next;
}
printf("\n");
}
// Function to return the element at the head of the list
User* getHeadElement(UserList* list) {
if (list->size == 0) {
return NULL;
}
return list->head->user;
}
// Function to return the element at the end of the list
User* getEndElement(UserList* list) {
UserNode* current = list->head;
while (current != NULL && current->next != NULL) {
current = current->next;
}
return current->user;
}
// Function to free the memory used by the list
void freeList(UserList* list) {
UserNode* current = list->head;
UserNode* next = NULL;
while (current != NULL) {
next = current->next;
free(current->user);
free(current);
current = next;
}
}
// Function to add a User to the list in ascending order of destination floor
void addToAscendingDestination(UserList* list, User* newUser) {
UserNode* node = createUser(newUser);
UserNode* current = list->head;
UserNode* previous = NULL;
while (current != NULL && current->user->destination_floor < newUser->destination_floor) {
previous = current;
current = current->next;
}
if (previous == NULL) {
node->next = list->head;
list->head = node;
} else {
previous->next = node;
node->next = current;
}
list->size++;
}
// Function to add a User to the list in descending order of destination floor
void addToDescendingDestination(UserList* list, User* newUser) {
UserNode* node = createUser(newUser);
UserNode* current = list->head;
UserNode* previous = NULL;
while (current != NULL && current->user->destination_floor > newUser->destination_floor) {
previous = current;
current = current->next;
}
if (previous == NULL) {
node->next = list->head;
list->head = node;
} else {
previous->next = node;
node->next = current;
}
list->size++;
}
// Function to add a User to the list in ascending order of floor call
void addToAscendingCall(UserList* list, User* newUser) {
UserNode* node = createUser(newUser);
UserNode* current = list->head;
UserNode* previous = NULL;
while (current != NULL && current->user->floor_call < newUser->floor_call) {
previous = current;
current = current->next;
}
if (previous == NULL) {
node->next = list->head;
list->head = node;
} else {
previous->next = node;
node->next = current;
}
list->size++;
}
// Function to add a User to the list in descending order of floor call
void addToDescendingCall(UserList* list, User* newUser) {
UserNode* node = createUser(newUser);
UserNode* current = list->head;
UserNode* previous = NULL;
while (current != NULL && current->user->floor_call > newUser->floor_call) {
previous = current;
current = current->next;
}
if (previous == NULL) {
node->next = list->head;
list->head = node;
} else {
previous->next = node;
node->next = current;
}
list->size++;
}