-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlinear_search.c
More file actions
259 lines (201 loc) · 4.5 KB
/
linear_search.c
File metadata and controls
259 lines (201 loc) · 4.5 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
Program to search in a set of student records by considering a specified field (Hall Ticket
Number, Name, or Team Number) by using Linear search techniques.
Author: Ananta Srikar
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
// Doubly linked list
struct student
{
char *ht_no;
char *name;
int team_no;
struct student *next;
};
typedef struct student student;
int main(int argc, char **argv)
{
// Initial code to get all command line values
if(argc != 3)
{
printf("Wrong number of arguments. Please go through README.md");
return -1;
}
for(int j = 0; j < strlen(argv[1]); j++)
if(!isdigit(argv[1][j]))
{
printf("Invalid arguments! Please go through README.md"); // Enter only numbers!
return -1;
}
int field = atoi(argv[1]);
if(!(field >= 1 && field <= 3))
{
printf("Invalid numbers entered. Please go through README.md");
return -1;
}
if(field == 3)
{
for(int j = 0; j < strlen(argv[2]); j++)
if(!isdigit(argv[2][j]))
{
printf("Enter a number for searching wrt team numebr"); // Enter only numbers!
return -1;
}
}
// End of command line arguments
// Function prototypes
student *getLinkedList(FILE*);
student *linearSearch(student*, int, void*);
void writeToFile(FILE*, student*);
// File I/O
FILE *inFPtr = NULL, *outFPtr;
inFPtr = fopen("DAALab_input1.txt", "r");
outFPtr = fopen("DAALab_output1.txt", "w");
if(inFPtr == NULL)
{
fprintf(stderr, "File 'DAALab_input1.txt' doesn't exist, exiting...");
return -1;
}
// End of file I/O
// Making a linked list from the file
student *root = getLinkedList(inFPtr);
fclose(inFPtr);
student *results = linearSearch(root, field, argv[2]);
// Write the sorted list into file
writeToFile(outFPtr, results);
fclose(outFPtr);
return(0);
}
// Function to get a linked list from the contents of the input file
student *getLinkedList(FILE *inFPtr)
{
student *head = NULL, *temp, *new_student;
char str[15];
int cnt = 0;
while(!feof(inFPtr))
{
switch(cnt % 3)
{
case 0:
new_student = (student*)malloc(sizeof(student));
new_student -> ht_no = (char*)(malloc(sizeof(char) * 10));
fscanf(inFPtr, "%s ", new_student -> ht_no);
break;
case 1:
fscanf(inFPtr, "%s ", str);
new_student -> name = (char*)malloc(sizeof(char) * strlen(str));
strcpy(new_student -> name, str);
break;
case 2:
fscanf(inFPtr, "%d", &(new_student -> team_no));
// Adding the new node to the linked list
if(head == NULL)
head = new_student;
else
temp -> next = new_student;
temp = new_student;
temp -> next = NULL;
}
cnt++;
}
return head;
}
void copyStudent(student *node_1, student *node_2)
{
if(node_1 == node_2)
return;
node_1 -> ht_no = node_2 -> ht_no;
node_1 -> name = node_2 -> name;
node_1 -> team_no = node_2 -> team_no;
node_1 -> next = NULL;
}
void appendNode(student **root, student *node)
{
student *temp = (student*)malloc(sizeof(student));
copyStudent(temp, node);
if(*root == NULL)
*root = temp;
else
{
student *temp2 = *root;
while(temp2 != NULL)
{
if(temp2 -> next == NULL)
{
temp2 -> next = temp;
break;
}
temp2 = temp2 -> next;
}
}
}
student *linearSearch(student* root, int field, void *search_data)
{
student *found = NULL, *temp = root;
switch(field)
{
case 1:
{
char *search_ht_no = (char*)search_data;
while(temp != NULL)
{
if(strcmp(search_ht_no, temp -> ht_no) == 0)
appendNode(&found, temp);
temp = temp -> next;
}
break;
}
case 2:
{
char *search_name = (char*)search_data;
while(temp != NULL)
{
if(strcmp(search_name, temp -> name) == 0)
appendNode(&found, temp);
temp = temp -> next;
}
break;
}
case 3:
{
int search_team_no = atoi(search_data);
while(temp != NULL)
{
if(search_team_no == temp -> team_no)
appendNode(&found, temp);
temp = temp -> next;
}
break;
}
}
return found;
}
// Function to write the linked list into the output file
void writeToFile(FILE *outFPtr, student *root)
{
if(root == NULL)
{
fprintf(outFPtr, "No results found!");
return;
}
int count = 0;
student *temp = root;
while(temp != NULL)
{
count++;
temp = temp -> next;
}
if(count == 1)
fprintf(outFPtr, "Found 1 result:\n");
else
fprintf(outFPtr, "Found %d results:\n", count);
temp = root;
while(temp != NULL)
{
fprintf(outFPtr, "%s %s %d\n", temp -> ht_no, temp -> name, temp -> team_no);
temp = temp -> next;
}
}