/*用链表实现学生成绩信息的管理*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stud_node {
int num;
char name[20];
int score;
struct stud_node* next;
};
struct stud_node* Create_stud_Doc(); /*新建链表*/
struct stud_node* InsertDoc(struct stud_node* head, struct stud_node* stud);/*插入操作*/
struct stud_node* DeleteDoc(struct stud_node* head,int num);/*删除操作*/
void Print_Stud_Doc(struct stud_node* head);/*遍历*/
void search_num(struct stud_node* head);/*按学号查找*/
void search_name(struct stud_node* head);/*按姓名查找*/
void search_score(struct stud_node* head);/*按分数查找*/
int main(void) {
struct stud_node* head, * p;
int choice, num, score;
char name[20];
int size = sizeof(struct stud_node);
do {
printf("1:Create\n");
printf("2:Insert\n");
printf("3:Delete\n");
printf("4:Print\n");
printf("5:search\n");
printf("0:Exit\n");
scanf("%d", &choice);
switch(choice) {
case 1:head = Create_stud_Doc();
break;
case 2:
printf("Input num,name,score:");
scanf("%d%s%d", &num, &name,&score);
head = InsertDoc(head,p);
break;
case 3:
printf("Input num:");
scanf("%d", &num);
head = DeleteDoc(head,num);
break;
case 4:Print_Stud_Doc(head);
break;
case 5:
printf("5.1 按学号查找\n");
printf("5.2 按姓名查找\n");
printf("5.3 按分数查找\n");
scanf("%d", &choice);
if (choice == 5.1)
search_num(head);
else if (choice == 5.2)
search_name(head);
else if (choice == 5.3)
search_score(head);
break;
case 0:break;
}
} while (choice!=0);
return 0;
}
struct stud_node* Create_Stu_Doc()
{
struct stud_node* head, * p;
int num, score;
char name[20];
int size = sizeof(struct stud_node);
head = NULL;
printf("Input num, name and score:\n");
scanf("%d %s %d", &num, &name, &score);
while (num != 0) {
p = (struct stu_node*)malloc(size);
p->num = num;
strcpy(p->name, name);
p->score = score;
head = InsertDoc(head, p);
scanf("%d %s %d", &num, &name, &score);
}
return head;
}
void Print_Stu_Doc(struct stud_node* head)
{
struct stud_node* ptr;
if (head == NULL) {
printf("\nNo Records\n");
return;
}
printf("\nThe Students' Records Are:\n");
printf("Num\t Name\t Score\n");
for (ptr = head; ptr != NULL; ptr = ptr->next) {
printf("%d\t%s\t%d\n", ptr->num, ptr->name, ptr->score);
}
}
struct stud_node* DeleteDoc(struct stud_node* head, int num)
{
struct stud_node* ptr1, * ptr2;
while (head != NULL && head->num == num) {
ptr2 = head;
head = head->next;
free(ptr2);
}
if (head == NULL)
return NULL;
ptr1 = head;
ptr2 = head->next;
while (ptr2 != NULL) {
if (ptr2->num == num) {
ptr1->next = ptr2->next;
free(ptr2);
}
else {
ptr1 = ptr2;
ptr2 = ptr1->next;
}
}
return head;
}
/*插入操作*/
struct stud_node* InsertDoc(struct stud_node* head, struct stud_node* stud) {
struct stud_node* ptr, * ptr1, * ptr2;
ptr2 = head;
ptr = stud; /*ptr指向新的待插入学生记录结点*/
/*原链表为空时的插入*/
if (head == NULL) {
head = ptr;
head->next = NULL;
}
else {
while ((ptr->num > ptr2->num) && (ptr2->next != 0)) {
ptr1 = ptr2;
ptr2 = ptr2->next;
}
if (ptr->num <= ptr2->num) {
if (head == ptr2) head = ptr;
else ptr1->next = ptr;
ptr->next = ptr2;
}
else {
ptr2->next = ptr;
ptr->next = NULL;
}
}
return head;
}
void search_num(struct stud_node* head) {
struct stud_node* q;
int num;
scanf("%d", &num);
q = head->next;
while (q) {
if (num == q->num)
printf("%d%s%d", q->num, q->name, q->score);
else
q = q->next;
}
}
void search_name(struct stud_node* head) {
struct stud_node *q;
char name[20];
scanf("%s", &name);
q = head->next;
while (q) {
if (strcmp(name, q->name) == 0)
printf("%d%s%d", q->num, q->name, q->score);
else
q = q->next;
}
}
void search_score(struct stud_node* head) {
struct stud_node* q;
int score;
scanf("%d", &score);
q = head->next;
while (q) {
if (score == q->score)
printf("%d%s%d", q->num, q->name, q->score);
else
q = q->next;
}
}
哪位大哥能帮我看一下,第一次做课程设计一直没法通过
有两处错误:
第一处:63行此处函数名字应与前面11行保持一直应为:Create_stud_Doc
第二处:83行此处函数名字应与前面14行保持一直应为:Print_Stud_Doc
修改完成后便可以运行出首页结果: