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
|
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Read a line from stdin, strip the eventual newline and return a pointer
// to the string. Remember to free the pointer.
char *read_input() {
char *buf = NULL;
size_t buf_size = 0;
size_t read = getline(&buf, &buf_size, stdin);
if (read == -1) {
free(buf);
buf = NULL;
} else if (read >= 1 && buf[read - 1] == '\n') {
buf[read - 1] = '\0';
}
return buf;
}
int read_int() {
char *buf = read_input();
int res = -1;
if (!buf) {
printf("Failed to read int from stdin\n");
} else {
res = atoi(buf);
}
free(buf);
return res;
}
// Fetch a student name from the terminal input and add it to the list.
void insert(struct list *student_list) {
struct student *student = malloc(sizeof(struct student));
char *name = read_input();
if (!name) {
printf("Failed to read student name\n");
free(student);
} else {
student->name = name;
printf("Inserted student with name '%s'\n", student->name);
list_push_front(student_list, &student->elem);
}
}
// Get a student name from the terminal input, remove it from the list and
// deallocate the appropriate memory (if needed).
void delete(struct list *student_list) {
char *name = read_input();
if (!name) {
printf("Failed to read student name\n");
} else {
struct list_elem *e;
for (e = list_begin(student_list); e != list_end(student_list);
e = list_next(e))
{
struct student *student = list_entry(e, struct student, elem);
if (strcmp(student->name, name) == 0) {
printf("Removing student '%s'\n", student->name);
list_remove(&student->elem);
free(student->name);
free(student);
break;
}
}
}
free(name);
}
// Print the entire list.
void list(struct list *student_list) {
struct list_elem *e;
for (e = list_begin(student_list); e != list_end(student_list);
e = list_next(e))
{
struct student *student = list_entry(e, struct student, elem);
printf("Student: %s\n", student->name);
}
}
// Clear the list and deallocate the memory.
void quit(struct list *student_list) {
struct list_elem *e;
struct student *student;
while (!list_empty(student_list)) {
e = list_pop_front(student_list);
student = list_entry(e, struct student, elem);
printf("Removing '%s'\n", student->name);
free(student->name);
free(student);
}
exit(0);
}
int main() {
struct list student_list;
list_init(&student_list);
int opt;
do {
printf("Menu:\n");
printf("1 - Insert student\n");
printf("2 - Delete student\n");
printf("3 - List students\n");
printf("4 - Exit\n");
switch (read_int()) {
case 1:
{
insert(&student_list);
break;
}
case 2:
{
delete(&student_list);
break;
}
case 3:
{
list(&student_list);
break;
}
case 4:
{
quit(&student_list);
break;
}
default:
{
printf("Quit? (1/0):\n");
if (read_int())
quit(&student_list);
break;
}
}
} while(1);
return 0;
}
|