用单链表制作学生信息管理系统
# 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