c++ - 遍历内部结构的链表

标签 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();
}

最佳答案

在下面的函数中,你需要在传递它时使用 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;
}

关于c++ - 遍历内部结构的链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35639402/

相关文章:

c++ - 在 C++ 中打印一个简单的链表,数据结构

c++ - Gdb。程序集生成文件中的标志 -g 不起作用

c++ - Assimp 错误地导入了 OBJ 索引?

c - C 中变量声明的排列决定了哪一个被执行?

c - 涉及链接列表和结构的示例程序不起作用

ios - CoreData链表?

c - 如何在 C 中找到两个单链表的析取

c++ - 覆盖说明符作为模板参数 - 它有效吗?

c++ - 在 C++ 程序中合并两个数组

c++ - 如何传递二维结构数组给函数?