遍历内部结构的链表

时间:2016-02-25 22:10:58

标签: c++ struct linked-list

所以我是一个包含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();
}

2 个答案:

答案 0 :(得分:2)

在下面的功能中,你需要使用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;
}

答案 1 :(得分:-1)

您应该创建一个构造函数,将成员变量初始化为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;   

};

相关问题