ZBLOG

#include using namespace std; // 定义学生结构体 struct Student{ int id; string name; int age; }; // 定义链表节点结构体 struct Node{ Student data; // 数据域,存储学生信息 Node* next; // 指针域,指向下一个节点 }; // 创建链表并返回头节点指针 Node* createList(){ ...

可以在主函数中添加一个读取文本文档的程序,例如:

#include <fstream>

// 读取学生信息并添加到链表中
void readFromFile(Node* head, string fileName){
    ifstream file(fileName); // 打开文件
    if(file.is_open()){ // 判断文件是否成功打开
        int id, age;
        string name;
        while(file >> id >> name >> age){ // 逐行读取文件中的每个学生信息
            Student s = {id, name, age};
            insertNode(head, s); // 将学生信息插入链表末尾
        }
        cout << "\n从文件中读取学生信息成功!\n";
    }
    else{
        cout << "\n无法打开文件!\n";
    }
}

int main(){
	// 创建空链表
	Node* head = createList();
	int choice;

	// 读取保存在"students.txt"文件中的学生信息并添加到链表中
	readFromFile(head, "students.txt");

	do{
		// 打印菜单选项...

上述代码将会在程序运行时自动从”students.txt”文件中读取学生信息,并将其添加到链表中。如果无法打开该文件,则输出错误消息。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=5133

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?