栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言 从文本中读取记录到数组,数组转为非降次序单链表,后写将链表写入二进制文件,再读出来。

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言 从文本中读取记录到数组,数组转为非降次序单链表,后写将链表写入二进制文件,再读出来。

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 
#include 
#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 filen");
        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条记录-------------nnn",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-------------观察是否按成绩的非降次序排序-------------nnn");
}

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("nn从二进制文件读取的信息如下:n");

    while(fread(&stu, sizeof(struct Stu), 1, fp))
    {
        printf("%4d %20s  %6d n", stu.ID, stu.name, stu.result);
    }
    fclose(fp);
    printf("nn-----读取完毕-----nn");
    
}

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("nn程序运行完毕~!n");
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/890122.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号