You are here:

C++/c++linkedlist

Advertisement


Question
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

The functional specfications of the functions are:
insertStudent insert a student with the relevant information. The student node is to be inserted into the appropriate location in the list to ensure order is according to descending order of the average score.

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.


my problem is the studentid it must be in char*no,instead of int.as for the function,void insertStudent(studentNode*& head, char *no, int s1=0, int s2=0, int s3=0)i nt sure whether i hv impement the function right or wrong.i nt sure hw to ensure the list to be in order according to descending order of the average score.
pls correct mi if i am wrong.as for the updateStudent the function,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.could you pls help mi.i am greatly appreciated for all 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 read(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);


int main()
{
   studentNode* head = NULL;

   read(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);
   
   if (temp == NULL)
   {
       cout << "An error occured: " << endl;
       exit(0);
   }
}


//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
I have already mentioned that you need to put average in your structure.  That way it will be easier to verify your software.

Moreover, it is good that you are using char string because that is what isdigit () takes.  Also, you would not have to verify an int number because you already know it has to contain digits by definition.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Titus B. Ledbetter, Jr.

Expertise

C , MFC, Object Oriented, Artificial Intelligence

Experience

I have over ten years experience in the field of Computer Science, five years experience developing C/C++. I recently wrote a chess program using Object Oriented, C++, MFC.

Education/Credentials
Master's Degree Computer Science from Johns Hopkins

©2016 About.com. All rights reserved.