-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvolunteer.c
More file actions
144 lines (123 loc) · 4.58 KB
/
Copy pathvolunteer.c
File metadata and controls
144 lines (123 loc) · 4.58 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
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
//Name: Hasin Shadab Rahman UID: U87513234
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "volunteer.h"
#include "read_line.h"
// Function to add a new volunteer to the list
struct volunteer *add_to_list(struct volunteer *list) {
struct volunteer *current = list;
struct volunteer *new_volunteer = malloc(sizeof(struct volunteer));
if (new_volunteer == NULL) {
printf("Memory allocation failed.");
return list; // Return the original list if memory allocation fails
}
// Get volunteer information from the user
printf("Enter last name: ");
read_line(new_volunteer->last, NAME_LEN);
printf("Enter first name: ");
read_line(new_volunteer->first, NAME_LEN);
printf("Enter email address: ");
read_line(new_volunteer->email, EMAIL_LEN);
printf("Enter grade level: ");
scanf("%d", &new_volunteer->grade_level);
// Check if the volunteer already exists in the list
while (current != NULL) {
if (strcmp(current->email, new_volunteer->email) == 0 && strcmp(current->last, new_volunteer->last) == 0) {
printf("Volunteer already exists.");
free(new_volunteer);
return list;
}
current = current->next;
}
// Initialize the new node
new_volunteer->next = NULL;
new_volunteer->prev = NULL;
// If the list is empty, return the new node
if (list == NULL)
return new_volunteer;
// Add the new node to the end of the list
current = list;
while (current->next != NULL)
current = current->next;
current->next = new_volunteer;
new_volunteer->prev = current;
return list;
}
// Function to search volunteers by grade level
void search_list(struct volunteer *list) {
int grade;
printf("Enter grade level: ");
scanf("%d", &grade);
struct volunteer *current = list;
int found = 0;
// Traverse the list and print volunteers with the specified grade level
while (current != NULL) {
if (current->grade_level == grade) {
printf("%-12s%-12s%-30s\n", current->last, current->first, current->email);
found = 1;
}
current = current->next;
}
// If no volunteers with the specified grade level are found, print a message
if (!found)
printf("Not found");
}
// Function to print the list of volunteers
void print_list(struct volunteer *list) {
struct volunteer *current = list;
// Traverse the list and print volunteer information
while (current != NULL) {
printf("%-12s%-12s%-30s%5d\n", current->last, current->first, current->email, current->grade_level);
current = current->next;
}
}
// Function to clear the list and deallocate memory
void clear_list(struct volunteer *list) {
while (list != NULL) {
struct volunteer *temp = list;
list = list->next;
free(temp); // Free the memory occupied by the current node
}
}
// Function to delete a volunteer from the list
struct volunteer *delete_from_list(struct volunteer *list) {
char last[NAME_LEN + 1];
char first[NAME_LEN + 1];
char email[EMAIL_LEN + 1];
int grade_level;
// Get volunteer information from the user
printf("Enter last name: ");
read_line(last, NAME_LEN);
printf("Enter first name: ");
read_line(first, NAME_LEN);
printf("Enter email address: ");
read_line(email, EMAIL_LEN);
printf("Enter grade level: ");
scanf("%d", &grade_level);
struct volunteer *current = list;
struct volunteer *prev = NULL;
// Traverse the list and find the volunteer to delete
while (current != NULL) {
if (strcmp(current->last, last) == 0 && strcmp(current->first, first) == 0 &&
strcmp(current->email, email) == 0 && current->grade_level == grade_level) {
// If the volunteer is found, remove them from the list
if (prev == NULL) {
list = current->next;
if (list != NULL)
list->prev = NULL;
} else {
prev->next = current->next;
if (current->next != NULL)
current->next->prev = prev;
}
free(current); // Free the memory occupied by the deleted node
return list;
}
prev = current;
current = current->next;
}
// If the volunteer is not found, print a message
printf("Volunteer does not exist.");
return list;
}