学生成绩信息的管理

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/* 用链表实现学生成绩信息的管理*/

#include

#include

#include

struct stud_node{

int num;

char name[20];

int score;

struct stud_node *next;

};

struct stud_node * Create_Stu_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_Stu_Doc(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);

printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~\n");

printf("\n");

printf(" Welcome to use the management of students' performance information\n"); printf("\n");

printf(" Make by Aibilly\n");

printf("\n");

printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~\n");

do{

printf("please choose 1:Create 2:Insert \n 3:Delete 4:Print 0:Exit\n");

scanf("%d", &choice);

switch(choice){

case 1:

head=Create_Stu_Doc();

break;

case 2:

printf("Input num,name and score:\n");

scanf("%d%s%d", &num,name, &score);

p = (struct stud_node *) malloc(size);

p->num = num;

strcpy(p->name, name);

p->score = score;

head=InsertDoc(head, p);

break;

case 3:

printf("Input num:\n");

scanf("%d", &num);

head = DeleteDoc(head, num);

break;

case 4:

Print_Stu_Doc(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 stud_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;

}

/* 插入操作*/

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 != NULL)){

ptr1 = ptr2; /* ptr1, ptr2各后移一个结点*/

ptr2 = ptr2->next;

}

if(ptr->num <= ptr2->num){ /* 在ptr1与ptr2之间插入新结点*/ if(head==ptr2) head = ptr;

else ptr1->next = ptr;

ptr->next = ptr2;

}

else{ /* 新插入结点成为尾结点*/

ptr2->next =ptr;

ptr->next = NULL;

}

}

return head;

}

/* 删除操作*/

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;

相关文档
最新文档