You are here:

C++/c++linked list

Advertisement


Question
Go to Previous message | Go to Next message | Back to MessagesMark as Unread | Print  ReplyReply AllMove...
Flag this messageAllExperts Reply to your questionThursday, 21 May, 2009 2:03 PM
From: "AllExperts" <>Add sender to ContactsTo:
Hello! The expert can't answer your question.

Your Question was:

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).



---------- 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

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
You need to go through the program line by line to make sure it incorporates everything in the specification.  One thing I see lacking is a field in the struct for the average score.  It would be easier for you to verify this software if the average was actually in the struct.

Next thing you need to do is run the program to see if it actually works and does exactly what you expect it to do.

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.