本帖最后由 YorkWaugh 于 2022-12-11 15:15 编辑
要求:
实验内容2::试编写一个程序完成:建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。
实验要求:(1)用链表实现。
(2)输入事先已编好的程序,并运行该程序。分析运行结果是否正确。
我的代码:
- #include <stdio.h>
- #include <stdlib.h>
- #define LEN sizeof(struct Student)
- struct Student
- {
- int num;
- char name[20];
- char gender;
- int age;
- struct Student *next;
- };
- int main()
- {
- int t;
- struct Student *pt;
- struct Student *creat(void);
- void del(int x, struct Student *s);
- void print(struct Student * s);
- printf("Input the information: number, name, gender, age\n");
- pt = creat();
- printf("Input a number:\n");
- scanf("%d", &t);
- del(t, pt);
- print(pt);
- return 0;
- }
- struct Student *creat()
- {
- struct Student *head, *p1, *p2;
- int n = 0;
- p1 = p2 = (struct Student *)malloc(LEN);
- scanf("%d %s %c %d", &p1->num, p1->name, &p1->gender, &p1->age);
- head = NULL;
- while (p1->num != 0)
- {
- n += 1;
- if (n == 1)
- head = p1;
- else
- p2->next = p1;
- p2 = p1;
- p1 = (struct Student *)malloc(LEN);
- scanf("%d %s %c %d", &p1->num, p1->name, &p1->gender, &p1->age);
- }
- p2->next = NULL;
- return (head);
- }
- void del(int x, struct Student *s)
- {
- struct Student *ps;
- ps = s;
- while (ps->next != NULL)
- {
- if (ps->age == x)
- ps->next = (*(*ps).next).next;
- ps = ps->next;
- }
- }
- void print(struct Student *s)
- {
- struct Student *ps;
- ps = s;
- while (ps->next != NULL)
- {
- printf("%d %s %c %d\n", ps->num, ps->name, ps->gender, ps->age);
- ps = ps->next;
- }
- }
复制代码
大概是- scanf("%d %s %c %d", &p1->num, p1->name, &p1->gender, &p1->age);
复制代码 这里不对
麻烦帮我修改下,急
update:已解决
- #include <stdio.h>
- #include <stdlib.h>
- #define LEN sizeof(struct Student)
- struct Student
- {
- int num;
- char name[20];
- char gender;
- int age;
- struct Student *next;
- };
- int main()
- {
- int t;
- struct Student *pt;
- struct Student *creat(void);
- void del(int x, struct Student *s);
- void print(struct Student * s);
- printf("Input the information: name, number, gender, age\n");
- pt = creat();
- print(pt);
- printf("Input a number:\n");
- scanf("%d", &t);
- del(t, pt);
- if (pt->age == t)
- pt = pt->next;
- print(pt);
- return 0;
- }
- struct Student *creat()
- {
- struct Student *head, *p1, *p2;
- int n = 0;
- p1 = p2 = (struct Student *)malloc(LEN);
- scanf("%s %d %c %d", p1->name, &p1->num, &p1->gender, &p1->age);
- head = NULL;
- while (p1->num != 0)
- {
- n += 1;
- if (n == 1)
- head = p1;
- else
- p2->next = p1;
- p2 = p1;
- p1 = (struct Student *)malloc(LEN);
- scanf("%s %d %c %d", p1->name, &p1->num, &p1->gender, &p1->age);
- }
- p2->next = NULL;
- return (head);
- }
- void del(int x, struct Student *s)
- {
- struct Student *b1, *b2;
- b1 = s->next, b2 = s;
- while (b1->next != NULL)
- {
- if (b1->age == x)
- b2->next = b1->next, b1 = b1->next;
- else
- b2 = b1, b1 = b1->next;
- }
- if (b1->age == x)
- b2->next = NULL;
- }
- void print(struct Student *s)
- {
- struct Student *p1, *p2;
- p1 = s;
- do
- {
- printf("%d %s %c %d\n", p1->num, p1->name, p1->gender, p1->age);
- p2 = p1, p1 = p1->next;
- } while (p2->next != NULL);
- }
复制代码 |