-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathcovid_list.c
More file actions
103 lines (81 loc) · 2.06 KB
/
Copy pathcovid_list.c
File metadata and controls
103 lines (81 loc) · 2.06 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node{
char date[10];
int cases;
int deaths;
struct node *next;
}node_t;
//This function adds new node to beginning of the linked list
//The string data is parsed inside the function
void push(node_t** head_ref, char* date, int cases, int deaths)
{
node_t* new_node = malloc(sizeof(node_t));
strcpy(new_node->date, date);
new_node->cases = cases;
new_node->deaths = deaths;
new_node->next = *head_ref;
*head_ref = new_node;
}
//This function pops the first node of the list
char* pop(node_t **head_ref)
{
char date[10];
int cases;
int deaths;
char *return_buffer;
return_buffer = (char*) malloc(sizeof(char) * 30);
node_t *temp = *head_ref;
//Check if head reference is NULL
if(*head_ref == NULL)
{
puts("List is empty");
return;
}
//This pops the node, most important part
*head_ref = (*head_ref)->next;
strcpy(date, temp->date);
cases = temp->cases;
deaths = temp->deaths;
sprintf(return_buffer, "%s %d %d\n", date, cases, deaths);
return return_buffer;
}
//This function lists the linked list
void list(node_t *head)
{
node_t *temp = head;
if(head == NULL)
{
puts("List is empty\n");
return;
}
while (temp != NULL)
{
printf("Date: %s, Cases: %d, Deaths: %d\n", temp->date, temp->cases, temp->deaths);
temp = temp->next;
}
}
int main(void)
{
node_t *head = NULL;
FILE *f;
char buffer[30];
char date[10];
int cases;
int deaths;
push(&head, "12.03.2020", 251, 46);
push(&head, "13.03.2020", 241, 47);
puts(pop(&head));
puts(pop(&head));
//open the file for reading
f = fopen("covid.txt", "r");
while(!feof(f))
{
fgets(buffer, 30, f);
sscanf(buffer, "%s %d %d\n", date, &cases, &deaths);
push(&head, date, cases, deaths);
}
list(head);
fclose(f);
}