-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassmanmain.c
More file actions
101 lines (97 loc) · 1.92 KB
/
classmanmain.c
File metadata and controls
101 lines (97 loc) · 1.92 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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
typedef struct STRclass
{
char classes [20];
int class_id[20];
struct stu * next;
} classes;
classes * insert(int);
void show(classes *);
classes * delete(classes *, int);
int main(int argc, const char * argv[])
{
int n;
printf("请输入课程数");
scanf("%d", &n);
classes *head = insert(n);
show(head);
printf("请输入要删除的课程号");
scanf("%d", &n);
delete(head, &n);
printf("删除后的数据:\n");
show(head);
system("pause");
return 0;
}
classes * insert(int n)
{
classes *head = NULL, *pre = NULL;
for (int i = 0; i<n; i++)
{
classes * temp = malloc(sizeof(classes));
printf("请输入课程名称:");
scanf("%s", temp->classes);
printf("请输入课程编号:");
scanf("%s", temp->class_id);
if (head == NULL)
{
head = temp;
}
if (pre != NULL)
{
pre->next = temp;
}
pre = temp;
}
pre->next = NULL;
return head;
}
void show(classes * head)
{
classes *p = head;
while (p)
{
printf("%s\t",p->classes);
printf(p->class_id);
printf("\n");
p = p->next;
}
}
classes * delete(classes * head, int id)
{
classes *p = head;
classes *before = NULL, *me = NULL, *t = NULL;
while (p)
{
if ((p->class_id)==id)
{
before = t;
me = p;
break;
}
t = p;
p = p->next;
}
if (p)
{
if (before == NULL&&me != NULL)
{
head = me->next;
free(me);
}
else if (before != NULL&&me->next != NULL)
{
before->next = me->next;
free(me);
}
else if (before != NULL&&me->next == NULL)
{
before->next = NULL;
free(me);
}
}
return head;
}