//define struct student by name by linked list
struct student
{
char name[20];
int age;
struct student *next;
};
//define function to add student
void add_student(struct student **head, char name[], int age)
{
struct student *new_student = (struct student *)malloc(sizeof(struct student));
strcpy(new_student->name, name);
new_student->age = age;
new_student->next = *head;
*head = new_student;
}
//define function to delete student
void delete_student(struct student **head, char name[])
{
struct student *temp = *head;
struct student *prev = NULL;
while (temp != NULL)
{
if (strcmp(temp->name, name) == 0)
{
if (prev == NULL)
{
*head = temp->next;
}
else
{
prev->next = temp->next;
}
free(temp);
break;
}
prev = temp;
temp = temp->next;
}
}
//add some students to linked list and delete some students
void main(){
struct student *head = NULL;
add_student(&head, "John", 20);
add_student(&head, "Jane", 21);
add_student(&head, "Jack", 22);
add_student(&head, "Jill", 23);
add_student(&head, "Joe", 24);
delete_student(&head, "Jane");
struct student *temp = head;
while (temp != NULL)
{
printf("%s %d\n", temp->name, temp->age);
temp = temp->next;
}
}