You are here:

C++/c++linklist

Advertisement


Question
QUESTION: my problem is the insertStudent.hw do i change it,for the studentid it has to be char *no instead of a int.how do i make sure that wen i insert the student node it is in the appropriate location in the list to ensure order is according to descending order of the average score.pls correct mi,if i am wrong.could you pls help mi.i would be appreciated for the help from you.

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



struct studentNode
{
   char *no;
  
   int score1;
   int score2;
   int score3;
  
   studentNode *next ;

};


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


int main()
{
  
  ifstream inFile;
  inFile.open("test.txt");
  
  if (!inFile)
  {
   cerr << "Unable to open file test.txt";
   exit(1);   // call system to stop
  }
  
  inFile.close();
  return 0;
}


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



//To insert a node into the list
void insertStudent( studentNode*& head,  char *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 ;
      }
    }
 }
}




ANSWER: Use a std::string to store the student id. see http://www.cprogramming.com/tutorial/string.html

That is the only change required for the earlier code.

#include <string>
struct studentNode
{
 std::string student_no ;

 int score1 ;
 int score2 ;
 int score3 ;

 studentNode *next ;

 explicit studentNode( const char* 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 ; }
};

void insertStudent( studentNode*& head,  const char* no, int s1=0, int s2=0, int s3=0 )
{
  if( head == 0 ) // empty list
      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
         prev->next = new studentNode( no, s1, s2, s3, curr ) ;
         return ;
       }
       else
       {
         prev = curr ;
         curr = curr->next ;
       }
     }
  }
}



---------- FOLLOW-UP ----------

QUESTION: You can assume the following format for the text file :
U0001 45 89 55
U0002 89 11 8
U0003 77 23 11
U0009 22 90 99
U0010 67 45 23
U0011 86 34 56

For the printStudent function, the printout format should be as follows :

The list contains :
Student No: U0001
Average Score : 96
Student No: U0005
Average Score : 76
Student No: U0009
Average Score : 67

The functional specfications of the functions are:
updateStudent updates the student identified by the student no, to the marks indicated.If the average
score of the marks requires a change in the position of the node, the node needs to be shifted in order
to preserve the descending order of the list. If the student cannot be found, nothing is done.

printStudentList prints the items in the list according to the order from the head of the list.

my problem is the function updateStudent,it must implement it tis way,
void updateStudent(studentNode *head, const char *no, int s1,int s2, int s3).i realli do nt knw wat
to do.i am so stuck.as for the function void printStudentList(studentNode *s),could you pls correct mi,if i am wrong.i realli need alot of your help.could you pls help mi.i am greatly appreciated for all the help from you.

code:
void printStudentList(studentNode *s)
{
studentNode curr = head;
while (curr != NULL)
{
cout << curr->s << ",";
curr = curr->next;
}
cout << endl;
}



ANSWER: The function printStudentList() is easy - just iterate through every studentNode in the list, printing out the information as you go along.

#include <string>
#include <iostream>

struct studentNode
{
 std::string student_no ;

 int score1 ;
 int score2 ;
 int score3 ;

 studentNode *next ;

 explicit studentNode( const char* 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 ; }
};

void printStudentList( const studentNode* head )
{
 // for each student in the list
 for( const studentNode* n = head ; n != 0 ; n = n->next )
 {
   // print out the information
   std::cout << "number: " << n->student_no << "\tscores: "
         << n->score1 << '\t' << n->score2 << '\t' << n->score3
         << "\taverage: " << n->average() << '\n' ;
 }
}


The function updateStudent() is more interesting because the position of the student in the list might change. A simple (albeit not the most efficient) way to implement this is:
a. locate the student in the list
b. remove the student from the list
c. reinsert into the list in the right position.

Because the head of the list may change when you modify the student position, pass the pointer by reference.

void insertStudent( studentNode*& head,  const char* no, int s1, int s2, int s3 ) ;

void updateStudent( studentNode*& head, const char* no, int s1, int s2, int s3 )
{
 // locate the student in the list
 studentNode* prev = head ;
 studentNode* curr = head->next ;

 while( curr != 0 )
 {
   if( curr->student_no == no ) // is this the student?
   {
      // yes, remove the student from the list
      prev->next = curr->next ;
      delete curr ;

      // and reinsert into the list in the right position
      insertStudent( head, no, s1, s2, s3 ) ;

      return ; // we are done
   }
   else // look in the next node for the student
   {
      prev = curr ;
      curr = curr->next ;
   }
 }
}



---------- FOLLOW-UP ----------

QUESTION: my problem is wen i compile there is so many errors that i do nt understand.there is so many errors in the insertStudent function.i do nt knw how to solve it.for tis code:explicit studentNode( const char* no, int s1=0 , int s2=0 , int s3=0 , studentNode* nxt=0 ):student_no(no),score1(s1),score2(s2),score3(s3),(next) {}.i hv nv learn tis code (explicit) at all.is there another way of writing the code without the word,(explicit)?
could you pls help mi,i realli realli need alot of your help.i will be so appreciated for all the help from you.


//QUINCY 2005
#include<iostream>
#include<fstream>
#include<conio.h>
#include <string>
using namespace std;


struct studentNode
{
  string student_no;
  char no[20];
  int score1;
  int score2;
  int score3;
  studentNode *next;
  
   explicit studentNode( const char* no, int s1=0 , int s2=0 , int s3=0 , studentNode* nxt=0  )
   :student_no(no),score1(s1),score2(s2),score3(s3),(next) {}

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

};

void initializeList(studentNode*& head);
void insertStudent( studentNode*& head, char *no, int s1=0, int s2=0, int s3=0);
void updateStudent(studentNode*& head, const char *no, int s1,int s2, int s3);
void printStudentList(studentNode *s);


int main()
{
  
  ifstream inFile;
  studentNode *head, *current=NULL;
  
  inFile.open("test.txt",ios::in);
  
  if (!inFile)
  {
   cerr << "Unable to open file test.txt";
   exit(1);   // call system to stop
  }
   
  studentNode *s;
  
  inFile >> s->no;
  inFile >> s->score1;
  inFile >> s->score2;
  inFile >> s->score3;
  
  inFile.close();
  return 0;
}


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


//To insert a node into the list
void insertStudent( studentNode*& head, const char *no, int s1=0, int s2=0, int s3=0)
{
 if(head == 0) // empty list
    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(s1, s2, s3, curr);
         return ;
    }
    else
    {
         prev = curr ;
         curr = curr->next ;
    }
   }
 }
}


void updateStudent(studentNode*& head, const char* no, int s1, int s2, int s3)
{
// locate the student in the list
studentNode* prev = head ;
studentNode* curr = head->next ;

while( curr != 0 )
{
  if( curr->student_no == no ) // is this the student?
  {
     // yes, remove the student from the list
     prev->next = curr->next ;
     delete curr ;

     // and reinsert into the list in the right position
     insertStudent( head, no, s1, s2, s3 ) ;

     return ; // we are done
  }
  else // look in the next node for the student
  {
     prev = curr ;
     curr = curr->next ;
  }
}
}


void printStudentList(studentNode *s)
{
// for each student in the list
for(studentNode * s; s != 0 ; s = s->next )
{
  // print out the information
  cout << "The list contains :" << endl;
  cout << endl;
  cout << "Student No: " << s->student_no << "   Average Score: " << s->average() << '
';
}
}








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


struct studentNode
{
  string student_no;
  char no[20];
  int score1;
  int score2;
  int score3;
  studentNode *next;
  
   explicit studentNode( const char* no, int s1=0 , int s2=0 , int s3=0 , studentNode* nxt=0  )
   :student_no(no),score1(s1),score2(s2),score3(s3),(next) {}

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

};

void initializeList(studentNode*& head);
void insertStudent( studentNode*& head, const char *no, int s1=0, int s2=0, int s3=0);
void updateStudent(studentNode *head, const char *no, int s1,int s2, int s3);
void printStudentList(studentNode *s);


int main()
{
  
  ifstream inFile;
  studentNode *head, *current=NULL;
  
  inFile.open("test.txt",ios::in);
  
  if (!inFile)
  {
   cerr << "Unable to open file test.txt";
   exit(1);   // call system to stop
  }
   
  studentNode *s;
  
  inFile >> s->no;
  inFile >> s->score1;
  inFile >> s->score2;
  inFile >> s->score3;
  
  inFile.close();
  return 0;
}


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


//To insert a node into the list
void insertStudent( studentNode*& head, const char *no, int s1=0, int s2=0, int s3=0)
{
 if(head == 0) // empty list
    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(s1, s2, s3, curr);
         return ;
    }
    else
    {
         prev = curr ;
         curr = curr->next ;
    }
   }
 }
}


void updateStudent(studentNode*& head, const char* no, int s1, int s2, int s3)
{
// locate the student in the list
studentNode* prev = head ;
studentNode* curr = head->next ;

while( curr != 0 )
{
  if( curr->student_no == no ) // is this the student?
  {
     // yes, remove the student from the list
     prev->next = curr->next ;
     delete curr ;

     // and reinsert into the list in the right position
     insertStudent( head, no, s1, s2, s3 ) ;

     return ; // we are done
  }
  else // look in the next node for the student
  {
     prev = curr ;
     curr = curr->next ;
  }
}
}


void printStudentList(studentNode *s)
{
// for each student in the list
for(studentNode * s; s != 0 ; s = s->next )
{
  // print out the information
  cout << "The list contains :" << endl;
  cout << endl;
  cout << "Student No: " << s->student_no << "   Average Score: " << s->average() << '
';
}
}  

Answer
> i hv nv learn tis code (explicit) at all.is there another way of writing the code without the word,(explicit)?

Yes, you could just omit using the keyword explicit altogether. ie. just remove 'explicit'. What 'explicit' means, and why it is a good idea, is explained here: http://www.devx.com/tips/Tip/12493


> wen i compile there is so many errors that i do nt understand.

One error is because of this:

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

Modify that to:

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

If you get other errors,

a. make sure that you are using a conforming C++ compiler (not a dead fossil like some old version of Turbo C++).

b. for me to be able to take look at it, include the complete error diagnostic from the compiler and identify the line causing the error.

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.