-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList Sum.c
125 lines (102 loc) · 2.37 KB
/
LinkedList Sum.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
/* Coding Assignment 1 */
// Samuel Ambani 1001694453
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
typedef struct node
{
int number;
struct node *next_ptr;
}
NODE;
void AddNodeToLL(int Number, NODE **LinkedListHead)
{
NODE *NewNode;
NewNode = malloc(sizeof(NODE));
NewNode -> number = Number;
NewNode ->next_ptr = NULL;
if (*LinkedListHead == NULL)
{
*LinkedListHead = NewNode;
}
else
{
NODE *Lastnode = *LinkedListHead;
while (Lastnode ->next_ptr != NULL)
{
Lastnode = Lastnode ->next_ptr;
}
Lastnode ->next_ptr = NewNode;
}
}
void ReadFileIntoLL(int argc, char *argv[], NODE **LLH)
{
char Filename[100];
strcpy(Filename,argv[1]);
int Count;
int num;
int sum;
FILE *fp;
fp = fopen(Filename,"r");
while (fgets(Filename,sizeof(Filename),fp))
{
num = atoi(Filename);
AddNodeToLL(num,LLH);
Count++;
sum += atoi(Filename);
}
printf("%d Records were read for a total sum of %d\n",Count,sum);
fclose(fp);
}
void PrintLL(NODE *LLH)
{
struct node *temp;
temp = LLH;
while (temp != NULL)
{
printf("\n%p %d %p\n",temp,temp->number,temp->next_ptr);
temp = temp ->next_ptr;
}
}
void FreeLL(NODE **LLH)
{
#ifdef PRINT
NODE *Temp_ptr = *LLH;
NODE *next_ptr;
while (Temp_ptr != NULL)
{
next_ptr = Temp_ptr -> next_ptr;
printf("\nFreeing %p %d %p \n",Temp_ptr,Temp_ptr-> number,Temp_ptr->next_ptr);
free(Temp_ptr);
Temp_ptr= next_ptr;
}
#endif
}
int main(int argc , char* argv[])
{
NODE *LLH = NULL;
clock_t start, end;
if (argc==2)
{
start = clock();/* capture the clock in a start time */
ReadFileIntoLL(argc, argv, &LLH);
end = clock();/* capture the clock in an end time */
printf("\n%ld tics to write the file into the linked list\n", end-start);
#ifdef PRINT
start = clock();/* capture the clock in a start time */
PrintLL(LLH);
end = clock();
#endif /* capture the clock in an end time */
printf("\n%ld tics to print the linked list\n", end-start);
start = clock();/* capture the clock in a start time */
FreeLL(&LLH);
end = clock(); /* capture the clock in an end time */
printf("\n%ld tics to free the linked list\n", end-start);
}
else
{
printf("File must be provided on command line... exiting\n");
}
return 0;
}