单链表制作学生信息管理系统C语言

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

# include "stdio.h"
# include "malloc.h"
#include "stdlib.h"
#include
# define NULL 0
# define LEN sizeof(struct student)

struct student
{ int num;
char name[20];
char sex;
float score;
short age;
struct student *next;
};
int n;

/* 新建链表 */
struct student *creat(void)
{
struct student *head = 0,*p1 = 0,*p2 = 0;
n = 0;
p1 = p2 = (struct student *)malloc(LEN); // 开辟一个新单元
memset( p1, 0, LEN );
scanf("%1d,%c,%d,%f,%s",&p1->num,&p1->sex,&p1->age,&p1->score,p1->name);
head = NULL;
while(p1->num!=NULL) //当输入的学号为0时,停止循环
{
n = n+1;
if(n == 1)
head = p1;

else p2->next = p1;
p2 = p1;
p1 = (struct student *)malloc(LEN);
memset( p1, 0, LEN );
scanf("%1d,%c,%d,%f,%s",&p1->num,&p1->sex,&p1->age,&p1->score,p1->name);

}
p2->next=NULL;
return(head);
}


/* 输出链表 */
void print(struct student *head)
{
struct student *p1 = 0;
printf("\n现在显示输入内容:\n",n);
p1 = head;
while(p1 != NULL)
{
printf("%1d,%s,%c,%d,%1.0f\n",p1->num,p1->name,p1->sex,p1->age,p1->score);
p1 = p1->next;
}
return;

}


/* 输出文件链表----文本形式 */
void file_print_1(struct student *head)
{
FILE *fp;
struct student *p = 0;
fp = fopen("E:\\Student Information Management Records_1.txt","a+");
if(fp == NULL)
{
printf("\n打开错误!\n");
exit(0);
}
p = head;

while(p != NULL)
{
fprintf(fp,"%1d,%s,%c,%d,%1.0f\n",p->num,p->name,p->sex,p->age,p->score);
p = p->next;
}
fprintf(fp,"\n");

fclose(fp);
return;
}


/* 删除节点 */
struct student *del(struct student *head, int num)
{
struct student *p1 = 0,*p2 = 0;

if(head == NULL)
{
printf("\n 没有记录! \n");
goto end;
}
p1=head;

while(p1->num != num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}

if(p1->num == num) //找到删除的学号
{
if(p1 == head) head = p1->next;
else p2->next = p1->next;
n = n-1; //节点数减1
free(p1);
}

else
printf("%1d号找不到!\n",num);
end:
return(head);
}


/* 插入节点 */
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0 = 0,*p1 = 0,*p2 = 0;
p1 = head; //p1指向头节点
p0 = stud; //p0指向要插入的节点
if(head == NULL)
{
head = p0;
p0->next = NULL;
}
else
{
while((p0->num > p1->num) && (p1->next != NULL))
{
p2 = p1;
p1 = p1->next;
} //找到插入的位置
if(p0->num <= p1->num)
{
if (head == p1) head = p0;
else p2->next = p0;
p0->next = p1;
}
else
{
p1->next = p0;
p0->next = NULL;
}
}
n = n+1; //节点数加1
return(head);
}


/* 查找节点 */
int search( int num,struct stud

ent *head)
{
struct student *p = 0;
p = head;
while(p != NULL)
{
if(p->num == num)
{
printf("%1d,%s,%c,%d,%1.0f\n",p->num,p->name,p->sex,p->age,p->score);
return 1;
}

p = p->next;

}

printf("查无此人!\n");
return 0;
}


/* 链表排序 */
struct student *maopao(struct student *head) //冒泡排序
{
if(head == NULL)
{
printf("没有内容!\n");
return head;
}
struct student *p1 = 0,*p2 = 0,*p3 = 0;
int i = 0,j = 0,k = 0,m = 0;
for(p1 = head ; p1->next !=NULL ; p1 = p1->next)
i++;
k=i;
for(j=0 ; j{
p1 = p2 = head;
p3 = p1->next;
for(m=0 ; m{
if(p1->num > p3->num)
{
if(p1 == head)
head = p3;
else
p2->next = p3;
p1->next = p3->next;
p3->next = p1;
p2 = p3;
p3 = p1->next;
}
else
{
p2 = p1;
p1 = p3;
p3 = p1->next;
}

}
k--;
}
return head;
}





void main()
{ struct student *head = 0,*stu = 0;
int del_num,delet_num,search_num;
int k;

while(k != 0)
{
printf("***************************\n");
printf("* *\n");
printf("* * 学生信息管理系统 * *\n");
printf("* *\n");
printf("* 1 ............创建链表 *\n");
printf("* *\n");
printf("* 2 ............删除链表 *\n");
printf("* *\n");
printf("* 3 ............插入节点 *\n");
printf("* *\n");
printf("* 4 ............删除节点 *\n");
printf("* *\n");
printf("* 5 ............查找节点 *\n");
printf("* *\n");
printf("* 6 ............链表排序 *\n");
printf("* *\n");
printf("* 7 ............显示链表 *\n");
printf("* *\n");
printf("* 0 ............退出程序 *\n");
printf("* *\n");
printf("* 输入 0, 1...7 *\n");
printf("* *\n");
printf("***************************\n");

scanf("%d",&k);
switch(k)
{
case 1: printf("按照(学号,性别,年龄,成绩,姓名)来输入学生数据(按0结束):\n");
head=creat(); //创建链表
print(head); //输出所有节点
file_print_1(head);
printf("\n"); ; break;

case 2: printf("按0删除所有链表,按其他返回:"); //删除链表
scanf("%1d",&delet_num);
if(delet_num == 0)
{
head = NULL;
printf("链表清除成功!!\n");
file_print_1(head);
}
else printf("返回菜单!!\n");
; break;

case 3: printf("\n请输入要插入学生的信息,包括学号,性别,年龄,成绩,姓名(按0结束):"); //链表插入
stu=(struct student *)malloc(LEN);
scanf("%1d,%c,%d,%f,%s",&stu->num,&stu->sex,&stu->age,&stu->score,stu->name);
while(stu->num != NULL)
{

head = insert(head,stu);
print(head);
file_print_1(head);
printf("\n请输入要插入学生的信息,包括学号,性别,年龄,成绩,姓名(按0结束):");
stu = (struct student *)malloc(LEN);
scanf("%1d,%c,%d,%f,%s",&stu->num,&stu->sex,&stu->age,&stu->score,stu->name);
}
; break;

case 4: printf("\n请输入要删除学生的学号(按0结束):"); //链表删除
scanf("%1d",&del_num);
while(del_num != NULL)
{
head = del(head,del_num);
print(head);
file_print_1(head);
printf("\n请输入要删除学生的学号(按0结束):");
scanf("%1d",&del_num);
}
printf("\n"); ; break;

case 5: printf("请输入要查找学生的学号(按0结束):\n"); //节点查找
scanf("%1d",&search_num);
search(search_num,head);
while(search_num != NULL)
{
printf("请输入要查找学生的学号(按0结束):\n");
scanf("%1d",&search_num);
search(search_num,head);
};
printf("\n") ; break;

case 6: head=maopao(head); //链表排序
print(head);
file_print_1(head); ; break;

case 7: print(head); //输出链表
file_print_1(head);
printf("\n"); ; break;

case 0: printf("\n\n\n 谢谢~!欢迎下次使用 !\n\n\n\n") ;

; break;
}


}



}