【问题标题】:Traversing a linked list with a structure inside遍历内部有结构的链表
【发布时间】:2016-02-25 22:10:57
【问题描述】:

所以我是一个由 4 个学生组成的链表,链表的每个节点内部都有一个结构,其中包含有关学生的一些数据。我想遍历这个链表并打印每个结构内的数据。我可以遍历链表,期望所有数据打印为 0。任何帮助将不胜感激。

#include<stdlib.h>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
using namespace std;

void displayGrades( struct Outer *O);
void calculateGrades(struct Outer *O);
void readGrades( struct Outer *O);

struct Inner{   
int id;
string name;
    double midterm1;
    double midterm2;
    double midtermTotal;
    double lab_H;
    double finalExam;
    double total;   
};  
Inner i1;

struct Outer{
Inner  data;
Outer *next;
};  
struct Outer o1, o2, o3, o4;

int main()
{

 readGrades(&o1);
 calculateGrades(&o1);
 displayGrades(&o1);
 //o1.next = &o2;
 /*
 readGrades(&o2);
 calculateGrades(&o2);
 displayGrades(&o2);
 //o2.next =&o3;

 readGrades(&o3);
 calculateGrades(&o3);
 displayGrades(&o3);
 //o3.next =&o4;

 readGrades(&o4);
 calculateGrades(&o4);
 displayGrades(&o4);
 //o4.next =NULL;
 */


Outer *ptro1;
ptro1 = new Outer;
Outer *ptro2;
ptro2 = new Outer;
Outer *ptro3;
ptro3 = new Outer;
Outer *ptro4;
ptro4 = new Outer;
Outer *head=ptro1;

ptro1->next = ptro2;
ptro2->next = ptro3;
ptro3->next = ptro4;
ptro4->next = NULL;

while(head!=NULL) // && i<=2)
{
  cout<<"Student ID: "<<head->data.id<<endl;
  cout<<"Student Midterm1: "<<head->data.midterm1<<endl;
  cout<<"Student Midterm2: "<<head->data.midterm2<<endl;
  cout<<"Student Labs and Homework: "<<head->data.lab_H<<endl;
  cout<<"Student Final Exam: "<<head->data.finalExam<<endl;
  head = head->next;
}
return 0;


}

void readGrades(struct Outer *O){

cout<<"Enter the student's id: "<<endl;     
cin>>o1.data.id;
cout<<"Enter the student's midterm #1 grade: ";     
cin>>o1.data.midterm1;
cout<<"Enter the student's midterm #2 grade: ";     
cin>>o1.data.midterm2;
cout<<"Enter the student's lab and homework grade: ";       
cin>>o1.data.lab_H;
cout<<"Enter the student's final exam grade: ";     
cin>>o1.data.finalExam;
}

void displayGrades(struct Outer *O){    

cout<<"The students final grade is: ";

if(O->data.total>=90)
    {   
    cout<<"A"<<endl;
    }
else if(O->data.total<=89 && O->data.total>=80)
    {   
    cout<<"B"<<endl;
    }
else if(O->data.total<=79 && O->data.total>=70)
    {   
    cout<<"C"<<endl;
    }
else if(O->data.total<=69 && O->data.total<60)
    {
    cout<<"F"<<endl;
    }
}

    void calculateGrades(struct Outer *O){
     O->data.midtermTotal=(((O->data.midterm1/50)+(O- >data.midterm2/50))/2)*35;
     O->data.lab_H=(O->data.lab_H/20)*25;
     O->data.finalExam=(O->data.finalExam/100)*40;
     O->data.total=O->data.midtermTotal+O->data.lab_H+O->data.finalExam;
    //displayGrades();
}

【问题讨论】:

  • 因此,您将数据保存到 o1 ... o4,然后制作一组全新的结构 ptro1...ptro4 并从中读取数据,您对为什么它们感到困惑'是空的?

标签: c++ struct linked-list


【解决方案1】:

在下面的函数中,您需要在传递 O 时使用它,但您正在使用 o1 每次都会覆盖

void readGrades(struct Outer *O)
{
cout<<"Enter the student's id: "<<endl;     
cin>>O->data.id;
cout<<"Enter the student's midterm #1 grade: ";     
cin>>O->data.midterm1;
cout<<"Enter the student's midterm #2 grade: ";     
cin>>O->data.midterm2;
cout<<"Enter the student's lab and homework grade: ";       
cin>>O->data.lab_H;
cout<<"Enter the student's final exam grade: ";     
cin>>O->data.finalExam;
}

【讨论】:

    【解决方案2】:

    您应该创建一个将成员变量初始化为 0 的构造函数。大多数编译器默认不这样做。

    struct Inner{ 
    
    Inner() : id(0),midterm1(0),midtermTotal(0), lab_H(0), finalExam(0),   total(0)
    {
    }
    
    int id;
    string name;
        double midterm1;
        double midterm2;
        double midtermTotal;
        double lab_H;
        double finalExam;
       double total;   
    

    };

    【讨论】:

    • 这不回答问题,应该是评论。
    • 我相信他期望未设置的值为 0,而是得到垃圾。此代码在 Visual C++ 中的输出输出 ....学生实验室和作业:-6.27744e+66 学生期末考试:-6.27744e+66 学生 ID:-842150451...
    猜你喜欢
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 2017-09-03
    相关资源
    最近更新 更多