//取硬盘信息,建立链表
linkNode CreateList()
{
FILE *fp;
fp = fopen("student.txt","r");
if(fp == NULL)
{
printf("cannot open this file/n");
exit(0);
}
//建立链表
linkNode head;
linkNode ptr,ptr1;
head = (linkNode)malloc(sizeof(StudentNode));//分配头结点
head->next=NULL;
ptr = head;
struct Student stuTest;
//如果没到文件末尾,就继续读
ptr1 = (linkNode)malloc(sizeof(StudentNode));
while(fread(&stuTest,sizeof(struct Student),1,fp)==1)
{
ptr1->data=stuTest;
ptr1->next = NULL;
ptr->next = ptr1;
ptr = ptr->next;
ptr1 = (linkNode)malloc(sizeof(StudentNode));
}
fclose(fp);
return head;
}
//存文件
void WriteMyFile(linkNode head)
{
remove("student.txt");
FILE *fp;
fp = fopen("student.txt","w");
if(fp == NULL)
{
printf("cannot open this file/n");
exit(0);
}
//遍历链表,存储信息到硬盘
linkNode ptr = head->next;
Student stu;
do
{
stu = ptr->data;
fwrite(&stu,sizeof(struct Student),1,fp);
ptr = ptr->next;
}
while(ptr != NULL);
fclose(fp);
}
//添加结点
linkNode AddNode(linkNode head,Student stu)
{
linkNode AddNode;
AddNode=(linkNode)malloc(sizeof(StudentNode));
AddNode->data = stu;
linkNode ptr;
//判断链表是否为空
if(head->next == NULL)
{
head->next = AddNode;
}
else
{
ptr = head->next;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = AddNode;
}
return head;
}
//添加信息
void AddMyFile(linkNode head)
{
FILE *fp;
fp = fopen("student.txt","a");
if(fp == NULL)
{
printf("cannot open this file/n");
exit(0);
}
//遍历链表,存储信息到硬盘
linkNode ptr = head->next;
Student stu;
while(ptr != NULL)
{
stu = ptr->data;
fwrite(&stu,sizeof(struct Student),1,fp);
ptr = ptr->next;
}
fclose(fp);
}
//修改链表
linkNode AmendList(linkNode head,char stuId[15])
{
//判断链表是否为空
if(head->next == NULL)
{
return NULL;
}
//遍历链表,找到要修改的学生学号
linkNode lengthNode = head->next;
while((lengthNode->next != NULL)&&(strcmp(lengthNode ->data.studentId,stuId)!=0))
{
lengthNode = lengthNode->next;
}
if(lengthNode == NULL)
{
printf("/n无此结点");
}
//修改结点的data域信息
else
{
Student stu;
printf("请输入学生学号:");
scanf("%s",stu.studentId);
printf("请输入学生姓名:");
scanf("%s",stu.studentName);
printf("请输入学生性别:");
scanf("%s",stu.sex);
printf("请输入学生SQL成绩:");
scanf("%d",&stu.sql);
printf("请输入学生C#成绩:");
scanf("%d",&stu.CSharp);
printf("请输入学生C成绩:");
scanf("%d",&stu.C);
printf("请输入学生班级:");
scanf("%s",stu.studentClass);
printf("请输入学生年级:");
scanf("%s",stu.studentGrade);
lengthNode->data = stu;
}
return head;
}
//删除结点
linkNode DeleList(linkNode head,char stuId[15])
{
//判断链表是否为空
if(head->next == NULL)
{
return NULL;
}
linkNode lengthNode,fontNode;//前驱结点;
lengthNode = head->next;
fontNode = head;
while((lengthNode->next != NULL)&&(strcmp(lengthNode ->data.studentId,stuId)!=0))
{
fontNode = lengthNode;
lengthNode = lengthNode->next;
}
if((lengthNode->next == NULL)&&(strcmp(lengthNode->data.studentId,stuId)!=0))
{
printf("/n无此结点");
}
else
{
fontNode->next = lengthNode->next;//删除链表中间或结尾的结点
free(lengthNode);
}
return head;
}
//获得链表长度
int GetLength(linkNode head)
{
int count = 0;
linkNode lengthNode = head->next;
while(lengthNode != NULL)
{
++count;
lengthNode = lengthNode->next;
}
return count;
}
//冒泡升序排序
linkNode BubbleUpSort(linkNode head)
{
linkNode r,p,fp,q;
r = NULL;
while(r!=head->next->next)
{
fp = head;
p = head->next;
q = p->next;
while(q!=r)
{
if(strcmp((p->data.studentId),(q->data.studentId))>0)
{
p->next = q->next;
q->next = p;
fp->next = q;
q = p->next;
fp = fp->next;
}
else
{
p = q;
q = q->next;
fp = fp->next;
}
}
r = p;
}
return head;
}
//链表排序反序
linkNode DisSort(linkNode head)
{
linkNode p,q,r;
p = head->next;
q = p->next;
r = q->next;
while(r != NULL)
{
q->next = p;
p = q;
q = r;
r = r->next;
}
q->next = p;
head->next ->next=NULL;
head->next = q;
return head;
}