文件内容存入链表

xiaoge2030 2015-10-17 10:45:09

我想把如图所示的txt文件内容存到链表中去,例如201570494 张三 男 90 89 78,依次是学号,姓名,性别,成绩(不同的元组,成绩个数可能不同)。自己用vc++编了个程序,调试时只能读到第一行元组;运行程序并得不到输出。我自己觉得这里面应该要用到链表的嵌套吧,但程序出错检查不出来。请各位大神指导!
...全文
103 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoge2030 2015-10-18
  • 打赏
  • 举报
回复
已解决!!!
xiaoge2030 2015-10-17
  • 打赏
  • 举报
回复
while((ifile>>number>>name>>sex)&&(score_head=score_list()))调试的时候在这个地方有问题。 自己编的代码如上,初学很多地方写的不规范,还请大神们指导!
xiaoge2030 2015-10-17
  • 打赏
  • 举报
回复
#include <iostream> #include<string> #include <fstream> using namespace std; ifstream ifile("成绩.txt",ios_base::in); struct score //学生成绩结构体 { int score_num; score *score_next; }; struct student //学生信息的结构体 { int Sid; string Name; string Sex; score data; /*int data;*/ student *next; }; void score_ShowList(student *L); void ShowList(student *L); score* score_list(); student* list(); int main() { student* head; head=list(); cout<<"文件内容存入离链表结束。"<<endl; cout<<"文件内容:"<<endl; ShowList(head); return 0; } score* score_list()//分数存入分数链表 { int score_num; int i=1; score *score_head=NULL; score_head=new score; score *score_p0,*score_p1;//p0指向最后的元素,p1是临时存储地址 while ((ifile>>score_num)&&(score_num<=100)) { if (i==1) { score_head->score_next=new score; score_head->score_next->score_num=score_num; score_head->score_next->score_next=NULL; score_p1=score_head->score_next;//p0指向最后的元素,p1是临时存储地址 } else { score_p0=new score; score_p0->score_num=score_num; score_p0->score_next=NULL; score_p1->score_next=score_p0; score_p1=score_p0; } i++; } return score_head; } student* list()//学生信息存入链表 { student *head=NULL; student *p0,*p1;//q指向头指针,p开辟student的单元 int i=1; /*ifstream ifile("成绩.txt",ios_base::in);*/ if(ifile) { while(!ifile.eof()) { string name,sex; int number; /*int data;*/ score *score_head; while((ifile>>number>>name>>sex)&&(score_head=score_list())) /*while(ifile>>number>>name>>sex>>data)*/ { if(i==1) { head=new student; head->Sid=number; head->Name=name; head->Sex=sex; head->data=*score_head; /*head->data=data;*/ head->next=NULL; p1=head;//p0指向最后的元素,p1是临时存储地址 } else { p0=new student; p0->Sid=number; p0->Name=name; p0->Sex=sex; p0->data=*score_head; /*p0->data=data;*/ p0->next=NULL; p1->next=p0; p1=p0; } i++; } } ifile.close(); } cout<<"取出成功!"<<endl; return head; //返回头节点 } void ShowList(student *L)//输出函数 { student *p; p=L; if(p==NULL) { cout<<"空链表!"<<endl; } else { while (p!=NULL) { cout<<"学号:"<<p->Sid<<"\t"<<"姓名:"<<p->Name<<"\t"<<"性别:"<<p->Sex<<"\t"<<"成绩:"; score_ShowList(p); /*cout<<"学号:"<<p->Sid<<"\t"<<"姓名:"<<p->Name<<"\t"<<"性别:"<<p->Sex<<"\t"<<"成绩:"<<p->data; p=p->next;*/ cout<<endl; } } cout<<"链表内容输出完毕:"; } void score_ShowList(student *L)//分数链表的输出函数 { score a; a=L->data; score *b; b=a.score_next; if(b==NULL) cout<<"没有输入分数!"<<endl; else { while (b!=NULL) { cout<<b->score_num<<"\t"; b=b->score_next; } } cout<<endl; }

64,678

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧