-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmallocstructlinked.c
More file actions
62 lines (49 loc) · 1.05 KB
/
mallocstructlinked.c
File metadata and controls
62 lines (49 loc) · 1.05 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct island {
char *name;
char *opens;
char *closes;
struct island *next;
} island;
void display(island *start){
island *i = start;
for (; i != NULL; i = i->next){
printf("Name: %s open %s-%s\n", i->name, i->opens, i->closes);
}
}
island* create(char *name){
island *i = malloc(sizeof(island));
i->name = strdup(name);
i->opens = "09:00";
i->closes = "17:00";
i->next = NULL;
return i;
}
void release (island *start)
{
island *i = start;
island *next = NULL;
for(; i!= NULL; i=next){
next = i->next;
free(i->name);
free(i);
}
}
int main(){
island *start = NULL;
island *i = NULL;
island *next = NULL;
char name[80];
for(; fgets(name, 80, stdin) != NULL; i = next){
next = create(name);
if (start == NULL)
start = next;
if(i != NULL)
i->next = next;
}
display(start);
release(start);
return 0;
}