In: Computer Science
1) a. Write down a C++ program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a gradepoint average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function you wrote (by printing student’s information on screen).
b. Write the psudecode for the program
PLEASE GIVE THUMBS UP, THANKS
input data with sample output :
code:
#include<iostream>
#include<fstream>
using namespace std;
//student structure
struct student
{
int id;
float gpa;
};
//node structure
struct node
{
student *data;
node *link;
};
//linklist class
class List
{
private:
node *head;
public:
List()
{
head=NULL;
}
//function to insert data into
List
void Insert(student *S)
{
node
*ptr=head;
node *temp=new
node();
temp->data=S;
temp->link=NULL;
if(ptr==NULL)
{
head=temp;
}
else
{
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
ptr->link=temp;
}
}
//function to display data
void display()
{
node
*ptr=head;
if(ptr==NULL)
{
cout<<"NO DATA EXISTS"<<endl;
}
else
{
while(ptr!=NULL)
{
student
*s=ptr->data;
cout<<s->id<<"
"<<s->gpa<<endl;
ptr=ptr->link;
}
}
}
};
int main()
{
ifstream infile;
string filename;
List L;
int id;
float gpa;
cout<<"Enter Filename : ";
cin>>filename;
infile.open(filename.c_str());
if(!infile)
{
cout<<"Unable to open
file"<<endl;
return 0;
}
while(infile>>id>>gpa)
{
student *ptr=new student();
ptr->id=id;
ptr->gpa=gpa;
L.Insert(ptr);
}
//displaying data to Console
cout<<"STUDENT DATA"<<endl;
L.display();
}