You are here:

C++/c++linkedlist

Advertisement


Question
Write a C++ program to read in a set of data from a text file which contains the student number, along with 3 scores for 3 subjects. You are to implement a
linked list, which allows you to insert each of the student data as a struct. However, the student data are to be ordered according to the average score of the 3 subjects, in descending order.

my problem is i nt sure whether i hv implement the function for the insertStudent is correct anot.i hv to impement the function in tis way,void insertStudent(studentNode&* head, char *no, int s1=0, int s2=0, int s3=0).could u pls help mi,i realli need your help!pls correct the code if i am wrong.i will be greatly appreciated for all the help from you!!!


#include<iostream>
#include<fstream>
#include<conio.h>
using namespace std;


struct studentNode
{
  int s1;
  int s2;
  int s3;
  char no;
  struct studentNode *next;
};


void initializeList(studentNode&* head);
void read(studentnode*& head);
void insertStudent(studentNode&* head, char *no, int s1=0, int s2=0, int s3=0);

int main()
{
  studentnode* head = NULL;

  cout << "Reading from text file" << endl;
  read(head);

  cout << "Printing out linked list" << endl;
  print(head);

  return 0;
}


void initializeList(studentNode&* head)
{    
  head=NULL;//empty linked list
}

void read(studentnode*& head)
{
  studentnode* temp;
  char filename[40];
  ifstream ins;

  cout << "Filename: ";
  cin >> filename;

  ins.open(filename, ios::in);
  temp = new node;
  
  if (temp == NULL)
  {
     cout << "An error occured: " << endl;
     exit(0);
  }
  if(ins.good())
  {
     while(!ins.eof())
     {
        ins >> temp->base;
        temp->next = NULL;
     

        if (head == NULL)
         head == temp;
        else
        {
         studentnode* z;
         z = head;
  
         while (z->next != NULL)
         {
         z = z->next;
         }
         z->next=temp;
        }
     }
  }
}

void insertStudent(studentNode&* head, char *no, int s1=0, int s2=0, int s3=0)
{
  struct studentNode *newnode;
  struct studentNode *crnt;
  
  //create node that will be inserted
  newnode = (struct studentNode*)malloc(sizeof(struct studentNode));
  newnode->no = no;
  newnode->s1 = 0;
  newnode->s2 = 0;
  newnode->s3 = 0;
  
  // Insert at the front of the list
  if(no != (*head)->no)
  {
     newnode->next = *head;
     *head = newnode;
   
     return;
  }

   
  // Find the correct place to insert the node
  crnt = *head;
  while(crnt->next != NULL && crnt->next->no != no)
  //
  {
     crnt = crnt->next;
  }


}  

Answer
Make the student number an int, not a char.
Write a constructor for studentNode.
Write a convenience function to get the average score.

struct studentNode
{
 int student_no ;

 int score1 ;
 int score2 ;
 int score3 ;

 studentNode *next ;

 explicit studentNode( int no, int s1 = 0 , int s2 = 0 , int s3 = 0 , studentNode* nxt = 0  )
    : student_no(no), score1(s1), score2(s2), score3(s3), next(nxt) {}

 double average() const { return ( score1 + score2 + score3 ) / 3.0 ; }
};

To insert a node into the list:

void insertStudent( studentNode*& head,  int no, int s1=0, int s2=0, int s3=0 )
{
  if( head == 0 ) // empty list, insert at head
      head = new studentNode( no, s1, s2, s3, 0 ) ;

  else
  {
     double avg = ( s1 + s2 + s3 ) / 3.0 ; // average score

     // search in list to determine correct place
     studentNode* prev = head ;
     studentNode* curr = head->next ;

     while( prev != 0 )
     {
       if( ( curr == 0 ) || ( curr->average() < avg ) )
       {
         // insert after prev, before curr
         prev->next = new studentNode( no, s1, s2, s3, curr ) ;
         return ;
       }
       else
       {
         prev = curr ;
         curr = curr->next ;
       }
     }
  }
}

Also note that the first parameter is studentNode*& (a reference to a pointer).  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.