-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_database.c
More file actions
executable file
·995 lines (853 loc) · 19.5 KB
/
Copy pathStack_database.c
File metadata and controls
executable file
·995 lines (853 loc) · 19.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
// This Program allows the user to edit and store
// records in a database, as an example of Stack logic
// By Andrew Colbeck 2017
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>
#define recordsfile "records.txt"
#define records_sorted "records_sorted.txt"
#define records_stack "records_stack.txt"
#define SIZE 3
bool sorted = false;
//---------------------------------------------------
// **** STRUCTURES ****
//---------------------------------------------------
typedef struct record
{
char name[20];
int id;
float num;
}record;
typedef struct stackNode
{
char name[20];
int id;
float num;
struct stackNode *nextPtr;
}stackNode;
typedef stackNode *stackNodePtr;
typedef struct linkedList
{
char name[20];
int id;
float num;
struct linkedList *next;
}linkedList;
typedef linkedList *linkedListPtr;
//---------------------------------------------------
// **** PROTOTYPES ****
//---------------------------------------------------
// RECORD KEEPING
int displayMenu();
void getDetail(record *records, int size);
void printDetail(record *records, int size);
FILE *copyToTextFile(record *records, FILE *fp1);
void readFromTextFile(FILE *fp1, record *records);
void printFromTextFile(FILE *fp1);
// SORTING AND SEARCHING
void bubbleSort(record *a, int array_size, int flow, int elementType);
void selectionSort(record *a, int array_size, int flow, int elementType);
void sort(FILE *fp1, FILE *fp2);
int linearSearch(FILE *fp1, int key);
int binarySearch(FILE *fp1, int searchKey, int low, int high);
void search(FILE *fp1, FILE *fp2);
// STACK AND LINKEDLIST FUNCTIONS
void push(stackNodePtr *topPtr, char *name, int id, float num);
void printStack(stackNodePtr currentPtr);
int isEmpty(stackNodePtr topPtr);
void searchStack(stackNodePtr currentPtr, int key, char *name, int option);
void copyStackToFile(stackNodePtr currentPtr, FILE *fp1);
void copyFileToStack(FILE *fp1, stackNodePtr currentPtr);
stackNodePtr reverse(stackNodePtr *topPtr);
void stackInsert(FILE *fp1, FILE *fp2);
void printList (linkedListPtr list1);
linkedListPtr insertLink(linkedListPtr *startPtr, char *name, int id, float num);
linkedListPtr listInsert(FILE *fp1);
//---------------------------------------------------
// **** MAIN PROGRAM ****
//---------------------------------------------------
int main()
{
int i = 1, answer, option, key;
record records[SIZE];
FILE *fp1;
FILE *fp2;
FILE *fp3;
linkedListPtr list = NULL;
printf("\nWelcome to the Record Keeping Program.\n");
printf("Enter -1 to quit.\n\n");
// GET RECORD DETAILS:
getDetail(records, SIZE);
fp1 = fopen (recordsfile, "w");
fp1 = copyToTextFile(records, fp1);
fclose(fp1);
// DISPLAY MENU:
do
{
option = displayMenu();
switch (option)
{
case 1: // SORT THE ARRAY
fp1 = fopen (recordsfile, "r");
fp2 = fopen (records_sorted, "w");
fp3 = fopen (records_stack, "w");
sort(fp1, fp2);
fclose(fp1);
fclose(fp2);
sorted = true;
option = 0;
break;
case 2: // SEARCH FOR INT
fp1 = fopen (recordsfile, "r");
fp2 = fopen (records_sorted, "w");
search(fp1, fp2);
fclose(fp1);
fclose(fp2);
option = 0;
break;
case 3: // INSERT ARRAY INTO A STACK
fp1 = fopen (recordsfile, "r");
fp3 = fopen (records_stack, "w");
stackInsert(fp1, fp3);
fclose(fp1);
fclose(fp3);
option = 0;
break;
case 4: // INSERT ARRAY INTO LINKED LIST
bubbleSort(records, SIZE, 2, 2);
//Sorts in descending so list is in asc.
fp1 = fopen (recordsfile, "w");
fp1 = copyToTextFile(records, fp1);
fclose(fp1);
// linkedListPtr listInsert(FILE *fp1)
fp1 = fopen (recordsfile, "r");
list = listInsert(fp1);
fclose(fp1);
option = 0;
break;
case 5: // INSERT NEW LINK INTO LINKED LIST
printf("Enter new Record Details:\n");
getDetail(records, 1);
list = insertLink(&list, records[0].name, records[0].id, records[0].num);
printList(list);
option = 0;
break;
case 6: // QUIT
option = -1;
break;
default: printf("Invalid Option, please enter a number:");
break;
}
}while(option !=-1);
}
//---------------------------------------------------
// **** FUNCTIONS ****
//---------------------------------------------------
int displayMenu()
{
int option=0;
printf("Please Select an Option from the following:\n");
printf("1: Sort the Array\n");
printf("2: Search for an ID (int element)\n");
printf("3: Insert Array into Stack\n");
printf("4: Insert Array elements (must be sorted first) into a linked list\n");
printf("5: Insert one more structure into the linked list\n");
printf("6: Quit\n");
scanf("%i", &option);
return option;
}
void getDetail(record *records, int size)
{
int i;
char temp_name[20];
int temp_id;
float temp_num;
for (i = 0; i < size; i++)
{
printf("Enter details for Record No. %i\n", i+1);
printf("Name: ");
scanf("%s", temp_name);
if (strcmp(temp_name, "-1") == 0)
break;
strcpy(records[i].name, temp_name);
printf("ID No.: ");
scanf("%i", &temp_id);
if (temp_id == -1)
break;
records[i].id = temp_id;
printf("Floating Point No.: ");
scanf("%f", &temp_num);
if (temp_num == -1)
break;
records[i].num = temp_num;
}
}
void printDetail(record *records, int size)
{
int i;
printf("\nRecord Details:\n");
for (i = 0; i < size; i++)
{
printf("Record No. %i\n", i+1);
printf("Name: %s\n", records[i].name);
printf("ID No.: %i\n", records[i].id);
printf("Floating Point No.: %f\n", records[i].num);
}
}
FILE *copyToTextFile(record *records, FILE *fp1)
{
int i;
if (fp1 == NULL)
{
// Error check on file:
printf("Error in opening file\n");
return fp1;
}
for (i = 0; i < SIZE; i++)
{
fprintf(fp1, "%s\t%i\t%f\n", records[i].name, records[i].id, records[i].num);
}
return fp1;
}
void readFromTextFile(FILE *fp1, record *records)
{
int i;
for (i = 0; i < SIZE; i++)
{
// Read results into an array:
fscanf(fp1, "%s", records[i].name);
fscanf(fp1, "%i", &records[i].id);
fscanf(fp1, "%f", &records[i].num);
}
}
void printFromTextFile(FILE *fp1)
{
int i;
record records[SIZE];
for (i = 0; i < SIZE; i++)
{
// Read results into an array:
fscanf(fp1, "%s", records[i].name);
fscanf(fp1, "%i", &records[i].id);
fscanf(fp1, "%f", &records[i].num);
}
printf("Information from File is:\n");
for (i = 0; i < SIZE; i++)
{
printf("Record No.: %i\n", i+1);
printf("Name: %s\n", records[i].name);
printf("ID No.: %i\n", records[i].id);
printf("Number: %f\n", records[i].num);
}
}
void bubbleSort(record *a, int array_size, int flow, int elementType)
{
int i, j;
record temp;
if (flow == 1)
{
// SORT IN ASCENDING ORDER:
for (i = 0; i < (array_size - 1); i++)
{
for (j = 0; j < array_size - 1 - i; j++)
{
// NAME SEARCH:
if (elementType == 1)
{
if (a[j].name[0] > a[j+1].name[0])
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
// INT SEARCH:
if (elementType == 2)
{
if (a[j].id > a[j+1].id)
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
// FLOAT SEARCH:
if (elementType == 3)
{
if (a[j].num > a[j+1].num)
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
}
}
if (flow == 2)
{
// SORT IN DESCENDING ORDER:
for (i = 0; i < (array_size - 1); i++)
{
for (j = 0; j < array_size - 1 - i; j++)
{
// NAME SEARCH:
if (elementType == 1)
{
if (a[j].name[0] < a[j+1].name[0])
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
// INT SEARCH:
if (elementType == 2)
{
if (a[j].id < a[j+1].id)
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
// FLOAT SEARCH:
if (elementType == 3)
{
if (a[j].num < a[j+1].num)
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
}
}
}
void selectionSort(record *a, int array_size, int flow, int elementType)
{
int i, j, min;
record temp;
if (flow == 1)
{
// SORT IN ASCENDING ORDER
for (i = 0; i < array_size - 1; i++)
{
//array_size - 1 is in place to save time, see above note
min = i;
//min is the array element number that holds the minimum value
for (j = i+1; j < array_size; j++)
{
//start j at element 1, because currently a[min] = a[0],
//thus it is optimal not to compare two identical values
// NAME SORT:
if (elementType == 1)
{
if (a[j].name[0] < a[min].name[0])
min = j;
}
// INT SORT:
if (elementType == 2)
{
if (a[j].id < a[min].id)
min = j;
}
// FLOAT SORT:
if (elementType == 3)
{
if (a[j].num < a[min].num)
min = j;
}
//min will inevitably hold address of lowest value
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
//a[i] will now hold the minimum value.
}
}
if (flow == 2)
{
// SORT IN DESCENDING ORDER
for (i = 0; i < array_size - 1; i++)
{
//array_size - 1 is in place to save time, see above note
min = i;
//min is the array element number that holds the minimum value
for (j = i+1; j < array_size; j++)
{
//start j at element 1, because currently a[min] = a[0],
//thus it is optimal not to compare two identical values
// NAME SORT:
if (elementType == 1)
{
if (a[j].name[0] > a[min].name[0])
min = j;
}
// INT SORT:
if (elementType == 2)
{
if (a[j].id > a[min].id)
min = j;
}
// FLOAT SORT:
if (elementType == 3)
{
if (a[j].num > a[min].num)
min = j;
}
//min will inevitably NOW hold address of HIGHEST value
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
//a[i] will now hold the minimum value.
}
}
}
void sort(FILE *fp1, FILE *fp2)
{
int i;
int flow, elementType, method;
record records[SIZE];
if (fp1 == NULL)
{
// Error check on file:
printf("Error in opening file\n");
return;
}
if (fp2 == NULL)
{
// Error check on file:
printf("Error in opening file\n");
return;
}
readFromTextFile(fp1, records);
printf("Please choose from:\n");
printf("1. Ascending\n");
printf("2. Descending\n");
printf("3. Quit\n");
scanf("%i", &flow);
if (flow == 3)
return;
printf("Choose which Element to sort by:\n");
printf("1: Name (String)\n");
printf("2: ID (Integer)\n");
printf("3: Number (Float)\n");
printf("4: Quit\n");
scanf("%i", &elementType);
if (elementType == 4)
return;
printf("Lastly, which Sorting Algorithm would you like to use:\n");
printf("1. Bubble Sort\n");
printf("2. Selection Sort\n");
printf("3. Quit\n");
scanf("%i", &method);
if (method == 3)
return;
if (method == 1)
{
bubbleSort(records, SIZE, flow, elementType);
}
if (method == 2)
{
selectionSort(records, SIZE, flow, elementType);
}
fp2 = copyToTextFile(records, fp2);
fclose(fp2);
fp2 = fopen (records_sorted, "r");
printFromTextFile(fp2);
fclose(fp2);
}
int linearSearch(FILE *fp1, int key)
{
// Linear search will begin comparing elements to
// the key starting from 0; excellent for searching
// unsorted Arrays.
int i;
record records[SIZE];
if (fp1 == NULL)
{
// Error check on file:
printf("Error in opening file\n");
return -1;
}
readFromTextFile(fp1, records);
for (i = 0; i < SIZE; i++)
{
if (records[i].id == key)
{
return i;
}
}
return -1;
}
int binarySearch(FILE *fp1, int searchKey, int low, int high)
{
// Binary Search (Array must be sorted), more efficient
// than linear search; especially with larger Arrays.
record records[SIZE];
if (fp1 == NULL)
{
// Error check on file:
printf("Error in opening file\n");
return -1;
}
readFromTextFile(fp1, records);
int middle, i;
while (low <= high)
{
middle = (low + high)/2;
if (searchKey == records[middle].id)
{
return middle;
}
else if (searchKey < records[middle].id)
{
// If key is in lower half of array,
// this if condition will change
// start and end points to reflect only
// the first half of the array.
high = middle - 1;
}
else
{
// If key was found to be higher than
// middle, then start and end points of array
// are changed to reflect only the second half
// of the array.
low = middle + 1;
}
}
return -1;
}
void search(FILE *fp1, FILE *fp2)
{
int answer, key, result;
printf("Which ID (Integer) would you like to search for?\n");
printf("1. Linear\n");
printf("2. Binary\n");
scanf("%i", &answer);
record records[SIZE];
if (fp1 == NULL)
{
// Error check on file:
printf("Error in opening file\n");
return;
}
readFromTextFile(fp1, records);
fclose(fp1);
printf("Enter the search key: ");
scanf("%i", &key);
do
{
if (answer == 1)
{
// LINEAR SEARCH
fp1 = fopen (recordsfile, "r");
result = linearSearch(fp1, key);
fclose(fp1);
}
if (answer == 2)
{
// BINARY SEARCH
bubbleSort(records, SIZE, 1, 2);
sorted = true;
fp2 = copyToTextFile(records, fp2);
fclose(fp2);
fp2 = fopen (records_sorted, "r");
result = binarySearch(fp2, key, 0, (SIZE-1));
}
if (result == -1)
{
printf("No match found.");
}
if (result != -1)
{
printf("Match found for ID %i on record %i\n", key, result+1);
break;
}
printf("Enter a new search key (-1 to quit): ");
scanf("%i", &key);
if (key == -1)
{
break;
}
}while(1);
}
void push(stackNodePtr *topPtr, char *name, int id, float num)
{
stackNodePtr newPtr;
newPtr = malloc( sizeof( stackNode ));
if ( newPtr != NULL)
{
strcpy(newPtr->name, name);
newPtr->id = id;
newPtr->num = num;
newPtr->nextPtr = *topPtr;
*topPtr = newPtr;
}
else
{
printf( "%i not inserted. No memory available.\n", id);
}
}
void printStack( stackNodePtr currentPtr )
{
int i=1;
stackNodePtr topPtr;
topPtr = currentPtr;
if ( currentPtr == NULL )
{
puts( "The stack is empty.\n" );
}
else
{
puts( "Records in stack are:\n" );
while ( currentPtr != NULL )
{
printf("Stack Node %i\n", i);
printf( "Name: %s\n", currentPtr->name);
printf("ID No.: %i\n", currentPtr->id);
printf("Float No.: %f\n", currentPtr->num);
printf("---------------\n");
i++;
currentPtr = currentPtr->nextPtr;
}
}
currentPtr = topPtr;
}
int isEmpty(stackNodePtr topPtr)
{
return topPtr == NULL;
}
void searchStack(stackNodePtr currentPtr, int key, char *name, int option)
{
int i=1;
stackNodePtr topPtr;
topPtr = currentPtr;
while ( currentPtr != NULL )
{
if (option == 1)
{
// SEARCH FOR INT
if (currentPtr->id == key)
{
printf("Match found, value %i in Stack Node No.: %i\n", key, i);
return;
}
}
if (option == 2)
{
// SEARCH FOR NAME
if (strcmp(currentPtr->name, name) == 0)
{
printf("Match found, name %s in Stack Node No.: %i\n", name, i);
return;
}
}
currentPtr = currentPtr->nextPtr;
i++;
}
currentPtr = topPtr;
printf("No Match found\n");
return;
}
void copyStackToFile(stackNodePtr currentPtr, FILE *fp1)
{
stackNodePtr topPtr;
topPtr = currentPtr;
if (fp1 == NULL)
{
// Error check on file:
printf("Error in opening file\n");
return;
}
while (currentPtr != NULL)
{
fprintf(fp1, "%s\t%i\t%f\n", currentPtr->name, currentPtr->id, currentPtr->num);
currentPtr = currentPtr->nextPtr;
}
currentPtr = topPtr;
}
void copyFileToStack(FILE *fp1, stackNodePtr currentPtr)
{
int i;
stackNodePtr topPtr;
topPtr = currentPtr;
record records[SIZE];
char temp_name[20];
int temp_id;
float temp_num;
if (fp1 == NULL)
{
// Error check on file:
printf("Error in opening file\n");
return;
}
for (i = 0; i < SIZE; i++){
fscanf(fp1, "%s", records[i].name);
fscanf(fp1, "%i", &records[i].id);
fscanf(fp1, "%f", &records[i].num);
}
for (i = SIZE; i > 0; i--)
{
push(¤tPtr, records[i].name, records[i].id, records[i].num);
}
currentPtr = topPtr;
printStack(topPtr);
}
stackNodePtr reverse(stackNodePtr *topPtr)
{
//Function to reverse the order of the
//Elements in a stack, returning
//Pointer to the (new) beginning of the stack
stackNodePtr currentPtr, prevPtr, nextPtr;
currentPtr = *topPtr;
prevPtr = NULL;
while (currentPtr != NULL)
{
nextPtr = currentPtr->nextPtr;
currentPtr->nextPtr = prevPtr;
prevPtr = currentPtr;
currentPtr = nextPtr;
}
*topPtr = prevPtr;
return *topPtr;
}
void stackInsert(FILE *fp1, FILE *fp2)
{
int i, option, key;
char name[20], char_option[2];
record records[SIZE];
readFromTextFile(fp1, records);
stackNodePtr stackPtr = NULL;
for (i = 0; i < SIZE; i++)
{
push(&stackPtr, records[i].name, records[i].id, records[i].num);
}
printf("All Elements have been inserted into a Stack.\n");
printStack(stackPtr);
printf("Would you like to search the stack?\n");
printf("Please select from the following options (-1 to quit):\n");
printf("1: Search for ID (Integer)\n");
printf("2: Search for Name (char)\n");
scanf("%i", &option);
if (option == -1)
{
return;
}
if (option == 1)
{
printf("Enter the ID No. to search for: ");
scanf("%i", &key);
}
if (option == 2)
{
printf("Enter the Name to search for: ");
scanf("%s", name);
}
searchStack(stackPtr, key, name, option);
printf("Would you like to print the elements in reverse order from a stack onto a separate file? (y or n): ");
scanf("%s", char_option);
if (strcmp(char_option, "n") == 0)
{
return;
}
printStack(stackPtr);
copyStackToFile(stackPtr, fp2);
fclose(fp2);
fp2 = fopen (records_stack, "a");
stackPtr = reverse(&stackPtr);
copyStackToFile(stackPtr, fp2);
}
void printList (linkedListPtr list1)
{
linkedListPtr tempPtr;
tempPtr = list1;
//Function to print the passed List
while (list1 != NULL)
{
printf("Record name: %s\n", list1->name);
printf("ID No.: %i\n", list1->id);
printf("Number: %f\n", list1->num);
list1=list1->next;
}
list1 = tempPtr;
}
linkedListPtr insertLink(linkedListPtr *startPtr, char *name, int id, float num)
{
linkedListPtr newPtr;
linkedListPtr previousPtr;
linkedListPtr currentPtr;
newPtr = malloc( sizeof( linkedList ));
if ( newPtr != NULL )
{
// CHECK IF SPACE AVAILABLE
strcpy(newPtr->name, name);
newPtr->id = id;
newPtr->num = num;
newPtr->next = NULL;
previousPtr = NULL;
currentPtr = *startPtr;
while ( currentPtr != NULL && id > currentPtr->id)
{
// DETERMINE WHERE TO PLACE LINK
previousPtr = currentPtr;
currentPtr = currentPtr->next;
}
if ( previousPtr == NULL )
{
// IF NULL IS REACHED, PLACE NODE AT TOP
newPtr->next = *startPtr;
*startPtr = newPtr;
}
else
{
// ELSE, NEW NODE BELONGS BETWEEN CURRENT & PREVIOUS PTR
previousPtr->next = newPtr;
newPtr->next = currentPtr;
}
}
else
{
printf( "%i not inserted. No memory available.\n", id );
}
return *startPtr;
}
linkedListPtr listInsert(FILE *fp1)
{
int i = 0;
record records[SIZE];
linkedListPtr startPtr=NULL;
linkedListPtr newPtr;
linkedListPtr currentPtr;
linkedListPtr nextPtr;
linkedListPtr previousPtr;
linkedListPtr tempPtr;
readFromTextFile(fp1, records);
// HEAD OF LIST
newPtr = (linkedList*)malloc(sizeof(linkedList));
strcpy(newPtr->name, records[i].name);
newPtr->id = records[i].id;
newPtr->num = records[i].num;
newPtr->next = NULL;
startPtr = newPtr;
// // REMAINING LINKS
for (i = 1; i < SIZE; i++)
{
newPtr = (linkedList*)malloc(sizeof(linkedList));
strcpy(newPtr->name, records[i].name);
newPtr->id = records[i].id;
newPtr->num = records[i].num;
newPtr->next = startPtr;
currentPtr=startPtr->next;
startPtr=newPtr;
}
printf("Printing List:\n");
printList(startPtr);
return startPtr;
}