Question

C++ Data Structure Write a program to read a list of students from a file and...

C++ Data Structure

Write a program to read a list of students from a file and create a list. The program should use a linked list for implementation. Each node in the linked list should have the student’s name, a pointer to the next student, and a pointer to a linked list of scores. There may be up to four scores for each student.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer summary :

Before writing the code let me explain it briefly. Here in this question we have to construct Linked list within a Linked List. So according to the question, initially we construct a Linked list of the students names. Each time when we create a node in the linked list of student names we initialise the head node for the scores of that student too. Then we run a loop to obtain the scores of the student and then insert in the scores linked list of each student node.

Each line in the input file is considered to be in the following format:

student_name score1 score2 score3 score4 . . . scoreN

student_name score1 score2 score3 score4 . . . scoreN

The proper explanation of the code is given within the comments in code

Input file (Input.txt) :

student1 1 2 3 4
student2 5 6 7 8
student3 9 10 11 12

C++ code:

   #include <bits/stdc++.h>
   #include <fstream>
   // The Input file name is given here
   std::ifstream file("input.txt");
   using namespace std;

   // The Node class for each score of the student
   class ScoreNode
   {
   public:
   string score;
   ScoreNode* next;
   };


   // The Node class for each Student Node
   class StudentNode
   {
   public:
   string name;
   ScoreNode* scores; // Pointer to the Linked List of Scores
   StudentNode* next;
   };

   // Function which Inserts the students name in the linked List
   void pushStudent(StudentNode** head_ref, string new_name)
   {
   StudentNode* new_Studentnode = new StudentNode();
   new_Studentnode->name = new_name;
   new_Studentnode->scores = NULL; //Intiallizes the Score Linked List for each student
   new_Studentnode->next = (*head_ref);
   (*head_ref) = new_Studentnode;
   }


   // Function to insert the score of each students in their respective linked lists
   void pushScore(ScoreNode** head_ref, string new_score)
   {
   ScoreNode* new_Scorenode = new ScoreNode();
   new_Scorenode->score = new_score;
   new_Scorenode->next = (*head_ref);
   (*head_ref) = new_Scorenode;
   }


   int main() {
       StudentNode* head = NULL;
       // First we have to Open the file
       if (file.is_open()) {
       // Now read Line by line
   std::string line;
   while (getline(file, line)) {
       // Each line contains the student Info
       string StudentInfo = line.c_str();
       // Split the info using stringstream
       istringstream ss(StudentInfo);
       // Collect the name of the student
       string name;
       ss >> name;    
       // Insert the name in the linked list
           pushStudent(&head,name);
           // Now collect all the scores of the student
           while(ss) {
       string score;
       ss >> score;
       // Push the collected score to the linked list of that student
       if(score!="")
               pushScore(&head->scores,score);
   // While there is more to read
       }
       cout<<endl;
       }

       }
       // Now the output
       // Each line displays the each students name and their corresponding linked list of scores
       cout<<"List of Students"<<endl;
       StudentNode* node = head;
       while (node != NULL)
       {    
       cout<<node->name<<" ";
       cout<<"Linked List of Scores : ";
       ScoreNode* p = node->scores;
           while (p != NULL)
           {
           cout<<p->score<<"->";
           p = p->next;
           }
           cout<<endl;
       node = node->next;
       }
   return 0;
   }

output :

List of Students
student3 Linked List of Scores : 12->11->10->9->
student2 Linked List of Scores : 8->7->6->5->
student1 Linked List of Scores : 4->3->2->1->

Add a comment
Know the answer?
Add Answer to:
C++ Data Structure Write a program to read a list of students from a file and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Write a program that reads students’ names followed by their test scores from the given...

    C++ Write a program that reads students’ names followed by their test scores from the given input file. The program should output to a file, output.txt, each student’s name followed by the test scores and the relevant grade. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, testScore of type int and grade of type char. Suppose that the class has 20 students. Use an array of...

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

  • c++ File name: 2170 107b.ee Purpose: This program demonstrates the use of multi-linked lists. //This file...

    c++ File name: 2170 107b.ee Purpose: This program demonstrates the use of multi-linked lists. //This file contains the implementation file for the 2170_10_7b.h header file. This implementation uses a dynamic linked list structure implementing the // Multi ListClass object. The implementation uses nodes which have two pointer fields one to point to the next record by name (nextName) and one to point Ito the next record by account number(nextNum). The linked list is also maintained I with a dummy header...

  • Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written...

    Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...

  • please use the c language Assignment 12 The program to write in this assignment is a...

    please use the c language Assignment 12 The program to write in this assignment is a program that calculates score statistics and manages the grades of the students. First, the student list and are given in a file named "student.dat" as in the following: 이성우 77 홍길동 88 scores 201 1710086 2012700091 This program reads the input file "student.dat" and keeps this data in a linear linked list. Each node must be a struct which contains the following: student id...

  • Write code to read student data from a csv file and add (prepend) that into a...

    Write code to read student data from a csv file and add (prepend) that into a linked list. Assume the code is written in only one file - main.c. You are REQUIRED to write the struct definition for the linked list node. . The input file name is taken from command line argument. . You can write everything except the struct definition in the main function, or can create multiple functions up to you. Following is the sample file data....

  • In C++ Write a menu driven C++ program to read a file containing information for a list of Students, process the data, t...

    In C++ Write a menu driven C++ program to read a file containing information for a list of Students, process the data, then present a menu to the user, and at the end print a final report shown below. You may(should) use the structures you developed for the previous assignment to make it easier to complete this assignment, but it is not required. Required Menu Operations are: Read Students’ data from a file to update the list (refer to sample...

  • Write a C++ program to store and update students' academic records in a binary search tree....

    Write a C++ program to store and update students' academic records in a binary search tree. Each record (node) in the binary search tree should contain the following information fields:               1) Student name - the key field (string);               2) Credits attempted (integer);               3) Credits earned (integer);               4) Grade point average - GPA (real).                             All student information is to be read from a text file. Each record in the file represents the grade information on...

  • Write a C program to compute average grades for a course. The course records are in...

    Write a C program to compute average grades for a course. The course records are in a single file and are organized according to the following format: Each line contains a student’s first name, then one space, then the student’s last name, then one space, then some number of quiz scores that, if they exist, are separated by one space. Each student will have zero to ten scores, and each score is an integer not greater than 100. Your program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT