-
Notifications
You must be signed in to change notification settings - Fork 0
/
123.c
153 lines (132 loc) · 3.82 KB
/
123.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
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
// The primary goal of this code is to demonstrate the implementation of
// a basic student management system using linked lists, focusing on functionality,
// memory management, and maintaining sorted order of student records.
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct snode {
int id;
char *name;
struct snode *next;
};
struct slist {
struct snode *front;
};
// Function prototypes
bool is_sorted(struct slist *lst);
struct slist *create_list();
bool insert_student(int id, char name[], struct slist *lst);
bool remove_student(int id, struct slist *lst);
char *find_student(int id, struct slist *lst);
void free_list(struct slist *lst);
struct slist *create_list() {
struct slist *lst = (struct slist *)malloc(sizeof(struct slist));
lst->front = NULL;
return lst;
}
bool is_sorted(struct slist *lst) {
if (lst == NULL || lst->front == NULL)
return true;
struct snode *current = lst->front;
while (current->next != NULL) {
if (current->id > current->next->id)
return false;
current = current->next;
}
return true;
}
bool insert_student(int id, char name[], struct slist *lst) {
if (find_student(id, lst) != NULL)
return false;
struct snode *new_node = (struct snode *)malloc(sizeof(struct snode));
new_node->id = id;
new_node->name = strdup(name);
new_node->next = NULL;
if (lst->front == NULL || lst->front->id > id) {
new_node->next = lst->front;
lst->front = new_node;
assert(is_sorted(lst));
return true;
}
struct snode *current = lst->front;
while (current->next != NULL && current->next->id < id)
current = current->next;
new_node->next = current->next;
current->next = new_node;
assert(is_sorted(lst));
return true;
}
bool remove_student(int id, struct slist *lst) {
struct snode *current = lst->front;
struct snode *prev = NULL;
while (current != NULL && current->id != id) {
prev = current;
current = current->next;
}
if (current == NULL)
return false;
if (prev == NULL)
lst->front = current->next;
else
prev->next = current->next;
free(current->name);
free(current);
assert(is_sorted(lst));
return true;
}
char *find_student(int id, struct slist *lst) {
struct snode *current = lst->front;
while (current != NULL) {
if (current->id == id)
return strdup(current->name);
current = current->next;
}
return NULL;
}
void free_list(struct slist *lst) {
struct snode *current = lst->front;
while (current != NULL) {
struct snode *temp = current;
current = current->next;
free(temp->name);
free(temp);
}
free(lst);
}
int main() {
struct slist *lst = create_list();
char command[10];
int id;
char name[31];
while (scanf("%s", command) != EOF) {
if (strcmp(command, "insert") == 0) {
scanf("%d %s", &id, name);
if (insert_student(id, name, lst))
printf("success\n");
else
printf("failure\n");
} else if (strcmp(command, "remove") == 0) {
scanf("%d", &id);
if (remove_student(id, lst))
printf("success\n");
else
printf("failure\n");
} else if (strcmp(command, "find") == 0) {
scanf("%d", &id);
char *found = find_student(id, lst);
if (found != NULL) {
printf("%s\n", found);
free(found);
} else {
printf("failure\n");
}
} else if (strcmp(command, "free") == 0) {
free_list(lst);
break;
}
assert(is_sorted(lst));
}
return 0;
}