#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct stu
{
char name[20];
int no;
char sex;
int age;
stu *next;
};
stu *CreatList()
{
int n;
printf("Please input the number of students:");
scanf_s("%d", &n);
stu *head, *pre,*cur;
head = new stu();
//head = (stu*)malloc(sizeof(stu));
head->next = NULL;
pre = head;
for(int i = 0; i < n; i++)
{
cur = new stu();
//cur = (stu*)malloc(sizeof(stu));
printf("Please input the information of students: name no age sex(b|g) :\n");
scanf_s("%s",cur->name, 20);
scanf_s("%d %d %c", &cur->no, &cur->age, &cur->sex);
cur->next = NULL;
pre->next = cur;
pre = pre->next;
}
return head;
}
void DeleteByAge(stu* &head)
{
int age;
printf("Please input the age of students that you want to delete:");
scanf_s("%d", &age);
stu *pre, *cur;
pre = head;
cur = pre->next;
while(cur != NULL)
{
if(cur->age == age)
{
pre->next = cur->next;
delete cur;
//free(cur);
//cur->next = NULL;
cur = pre->next;
}
else
{
pre = cur;
cur = cur->next;
}
}
}
void Display(stu* &head)
{
stu *s;
s = head->next;
while(s != NULL)
{
printf("%s %d %d %c\n", s->name, s->no, s->age, s->sex);
s = s->next;
}
}
int main()
{
stu *head;
head = CreatList();
Display(head);
DeleteByAge(head);
Display(head);
}
一个学生的信息是:姓名,学号,性别,年龄等信息,用一个链表,把这些学生信息连在一起, 给出一个age, 在些链表中删除学生年龄等于age的学生信息
最新推荐文章于 2022-12-04 22:28:22 发布