-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_memory.c
241 lines (218 loc) · 5.11 KB
/
main_memory.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#ifndef MEMORY
#define MEMORY
#include <stdio.h>
#include <stdlib.h>
//#include "main.c"
#include "log.c"
#include "generate_students.c"
typedef struct Node
{
struct Student student;
struct Node* next;
}node;
node* newNode(struct Student newStudent);
void addNode(node** head, struct Student std);
void removeHeadNode(node** head, int update);
void search(node** head, int id);
void update(node** head, int id, int hostelRoomNumber);
void printList(node* head);
void freeList(node** head);
struct Student searchFile(int id, int flag);
void updateFile(int id, int newRoomNumber);
void deleteNode(node** head, int id);
// int main()
// {
// // node* head = malloc(sizeof(node));
// // head->student = std;
// // head->next = NULL;
// node *head = NULL;
// for (int i = 1; i <= 5; i++)
// {
// struct Student std = generateStudent(i);
// addNode(&head, std);
// }
// deleteNode(&head, 3);
// printList(head);
// //search(head, 15);
// }
// Search the file and return 1 if found and 0 if not
struct Student searchFile(int id, int flag)
{
FILE* db = fopen("disk", "r");
if (db == NULL)
{
perror("Error opening database file");
exit(0);
}
struct Student temp;
while(fread(&temp, 1, sizeof(struct Student), db) && sizeof(temp) == sizeof(struct Student))
{
if (temp.id == id)
{
if (flag == 0)
{
// printf("%d, %s, %s, %s, %d, %s, Year %d\n", temp.id, temp.name,
// temp.hostel, temp.course, temp.roomNumber, temp.dob, temp.yearOfStudy);
}
fclose(db);
return temp;
}
}
// printf("NOT FOUND!!\n");
fclose(db);
temp.id = -1;
return temp;
}
// Update the file.
void updateFile(int id, int newRoomNumber)
{
/*
*
* Implement the logic for updating here.
* First search file, but instead of returning a number, get the file pointer.
* Use fseek to reposition the file pointer. The number of bytes will be similar.
* And write the new struct there.
*
*/
FILE* db = fopen("disk", "r+");
if (db == NULL)
{
perror("Error opening database file");
exit(0);
}
struct Student temp;
while(fread(&temp, 1, sizeof(struct Student), db) && sizeof(temp) == sizeof(struct Student))
{
if (temp.id == id)
{
temp.roomNumber = newRoomNumber;
fseek(db, -sizeof(struct Student), SEEK_CUR);
fwrite(&temp, 1, sizeof(struct Student), db);
fclose(db);
return;
}
}
fclose(db);
return;
}
void addNode(node** head, struct Student std)
{
node* newNode = malloc(sizeof(node));
newNode->next = NULL;
newNode->student = std;
if (*head == NULL)
{
*head = newNode;
return;
}
node* temp = *head;
int count = 1;
while(temp->next != NULL)
{
count++;
temp = temp->next;
}
//printf("Count: %d\n", count);
if (count >= 5)
removeHeadNode(head, 1);
temp->next = newNode;
}
void removeHeadNode(node** head, int update)
{
struct Student std = (*head)->student;
*head = (*head)->next;
if (update == 1)
{
updateFile(std.id, std.roomNumber);
}
}
void printList(node* head)
{
while(head != NULL)
{
printf("%d ", head->student.id);
head = head->next;
}
printf("\n");
}
void search(node** head, int id)
{
writeOne();
node* temp = *head;
int f = 0;
while (temp != NULL)
{
if (temp->student.id == id)
{
f = 1;
break;
}
temp = temp->next;
}
if (f == 1)
{
// struct Student tempStd = temp->student;
// printf("%d, %s, %s, %s, %d, %s, Year %d\n", tempStd.id, tempStd.name,
// tempStd.hostel, tempStd.course, tempStd.roomNumber, tempStd.dob, tempStd.yearOfStudy);
}
else
{
writeTwo();
struct Student tempStd = searchFile(id, 0);
addNode(head, tempStd);
}
return;
}
void update(node** head, int id, int hostelRoomNumber)
{
node* temp = *head;
int f = 0;
writeOne();
while (temp != NULL)
{
if (temp->student.id == id)
{
f = 1;
break;
}
temp = temp->next;
}
if (f == 1)
{
temp->student.roomNumber = hostelRoomNumber;
}
else
{
writeTwo();
struct Student tempStd = searchFile(id, 1);
tempStd.roomNumber = hostelRoomNumber;
addNode(head, tempStd);
}
return;
}
void deleteNode(node** head, int id)
{
node* prev = *head;
if (prev->student.id == id)
{
*head = (*head)->next;
free(prev);
}
else
{
node* temp = prev->next;
while (prev != NULL)
{
temp = prev->next;
if (temp != NULL)
{
if (temp->student.id == id)
{
prev->next = temp->next;
}
}
prev = prev->next;
}
}
}
#endif