3 zhangshan 80
7 lisi 70
1 wangwu 90
2 zhaoliu 95
4 fanglei 98
6 guojia 85
5 maxiaojun 75
将文件内的记录读到数组中:
3 zhangshan 80
7 lisi 70
1 wangwu 90
2 zhaoliu 95
4 fanglei 98
6 guojia 85
5 maxiaojun 75
-------------总计7条记录-------------
转为有头单链表后的结果如下:
4 fanglei 98
2 zhaoliu 95
1 wangwu 90
6 guojia 85
3 zhangshan 80
5 maxiaojun 75
7 lisi 70
-------------观察是否按成绩的非降次序排序-------------
有头单链表信息写入二进制文件:
写入完毕!!
从二进制文件读取的信息如下:
4 fanglei 98
2 zhaoliu 95
1 wangwu 90
6 guojia 85
3 zhangshan 80
5 maxiaojun 75
7 lisi 70
-----读取完毕-----
程序运行完毕~!
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define N 70
struct Stu
{
int ID;
char name[12];
int result;
};
struct Class
{
struct Stu stu[N];//最大人数70
int n;//实际人数
}c;
struct Node
{
struct Stu data;
struct Node* next;
};
void ReadTxtFile()//将文件数据读入结构数组中
{
int i;
FILE* fp = fopen("stu.txt", "r");
if (!fp)
{
printf("can't open file\n");
return ;
}
printf("将文件内的记录读到数组中:\n");
while (!feof(fp))
{
fscanf(fp, "%d%s%d", &c.stu[c.n].ID, c.stu[c.n].name, &c.stu[c.n].result);
c.n++;
}
fclose(fp);
for (i = 0; i < c.n; i++)
{
printf("%4d %20s %6d \n",c.stu[i].ID, c.stu[i].name, c.stu[i].result);
}
printf("\n-------------总计%d条记录-------------\n\n\n",c.n);
}
void ArrayToList(struct Node** head)
{
struct Node* pp=NULL, * pnode=NULL,*pnew=NULL;//pp为要插入的前一个位置,pnode为要插入的位置
int i;
if (*head == NULL)//如果表头是空,建立有头链表
{
(*head) = (struct Node*)malloc(sizeof(struct Node));
(*head)->next = NULL;
}
for (i = 0; i < c.n; i++)
{
pnew = (struct Node*)malloc(sizeof(struct Node));//数组元素转为链表节点
pnew->data = c.stu[i];
pnew->next = NULL;
pp = *head;
pnode = (*head)->next;
while (pnode != NULL)//表不空
{
if (pnew->data.result > pnode->data.result)//找到位置结束循环
break;
pp = pnode;//保存要插入的前一个位置
pnode = pnode->next;//指针下移
}
pnew->next = pnode;
pp->next = pnew;
}
pnode = (*head)->next;
printf("转为有头单链表后的结果如下:\n");
while (pnode)
{
printf("%4d %20s %6d \n", pnode->data.ID, pnode->data.name, pnode->data.result);
pnode = pnode->next;//指针下移
}
printf("\n-------------观察是否按成绩的非降次序排序-------------\n\n\n");
}
void WriteToBinaryFile(struct Node* head)
{
FILE* fp;
struct Node* pnode = NULL;
if (head == NULL)//如果表头是空
{
return;
}
fp = fopen("stu.dat", "wb");
if (NULL == fp) {
return ;
}
pnode = head->next;
printf("有头单链表信息写入二进制文件:\n");
while (pnode)
{
fwrite(&pnode->data, sizeof(struct Stu), 1, fp);
pnode = pnode->next;//指针下移
}
fclose(fp);
printf("写入完毕!!\n");
}
void ReadToBinaryFile()//读二进制文件,内容显示出来
{
FILE* fp;
struct Stu stu;
fp = fopen("stu.dat", "rb");
if (NULL == fp) {
return;
}
printf("\n\n从二进制文件读取的信息如下:\n");
while(fread(&stu, sizeof(struct Stu), 1, fp))
{
printf("%4d %20s %6d \n", stu.ID, stu.name, stu.result);
}
fclose(fp);
printf("\n\n-----读取完毕-----\n\n");
}
void MakeEmpty(struct Node** head)
{
struct Node* temp= NULL;
int i;
i=0;
while((*head)->next)
{
temp=(*head)->next;
(*head)->next=temp->next;
free(temp);
printf("释放了%d个节点\n",++i);
}
}
int main()
{
struct Node* head=NULL;
c.n = 0;
ReadTxtFile();//读取文本内容,放到数组中,打印出来。
ArrayToList(&head);//将数组数据,转为按成绩的非降次序有头单链表
WriteToBinaryFile(head);//将单链表内容写入二进制文件
ReadToBinaryFile();//读二进制文件,内容显示出来
MakeEmpty(&head); //删节点
free(head); //释放head
printf("释放了head节点\n");
printf("\n\n程序运行完毕~!\n");
return 0;
}