i have written the following code for an add fucntion for a linked list implementation. when i run it, it runs correctly. when i went to class today, i found i had made a mistake. my add function adds only a student data type, whereas the assignment was to make the add function able to add a student(string), a university (string) and an id(int).
how can i modify my code in order to satisfy this?
any help is appreciated.
thankyou!

ps. if i didnt explain myself correctly, ill be more than happy to provide more details.

this is my code:

void add(string student)
       {
           //create a new node
           studentnode * somenode;
           somenode = new studentnode;
           
           //put student in the new node!
           somenode->data = student;     
           
           //put new node at front of list!
           somenode->next = start;
           start = somenode;
       }

Have you dealt any with operator overloading?

I'm not sure exactly what you're asking though - are you wanting to add members of the same class (e.g., Create one large string of "Student - University - ID") or are you wanting to add members from other classes (e.g., string temp = StudentA.name + StudentB.name)?

my assignment is to implement basic functions of a linked list. simply put, i must make a function to add a student to the list, then make a function to retrieve a certain student while providing only the id of that student.

Ok, I'm a bit out of practice, so if I'm incorrect in my information, it would be helpful if someone corrected me here.

It looks like you need to add a new node onto the linked list with the student name, the university, and the id. That is what you will need to pass to the fuction of the linked list class.

void add(string student, sting university, int id){
...
}

Once you have the information passed to the function, create the node, add the information into the node, point the previous nodes next pointer to the new node, and point the current node's point to EOF, null, or whatever.

Edit:

What does your .h file look like for this class?

i think i see where your going with this.
i have everything in one file. im using devc++ so i dont think i can make a .h file.
here is my entire code as i have it so far:

#include <iostream>
#include <string>


using namespace std;


//new class for studentnode
class studentnode
{
public:
       string data;
       studentnode * next;  
	   

	   // string to hold student name
	   string student;

};


//new class for studentlist
class studentlist
{
private:      
      studentnode * start;
      
      
public:
       studentlist()
       {
           start = NULL;            
       }
       
   
       
       // function to add a new student to list
       void add(string student)
       {
           //create a new node
           studentnode * somenode;
           somenode = new studentnode;
           
           //put student in the new node!
           somenode->data = student;     
           
           //put new node at front of list!
           somenode->next = start;
           start = somenode;
       }
       

       //function to a specified student, if not traverse the list until found.
void getStudent (string student)
{ 
     studentnode* current;
     current = start; 
     while(current!=NULL)
     {   
         if(current->data == student)   
         {    
               cout << "The Specified Student Has Been Found: "<< endl;
               cout << student <<endl;  
                 break;  
                 }  
                  current = current->next;  
                  }
                  }

       

       //display all the items in the list
       void print()
       {
            studentnode * current;
            current = start;
            
           //loop through all nodes, each time doing something
           while(current != NULL )
           {
               //display the data of the current node
               cout << current->data << endl;
               
               //move current to next node
               current = current->next;               
           }     
       }
};







//main program thats ran.
int main()
{
    cout <<"This linked list should display students as they are added" << endl;
    
    
    studentlist listofstudents;
    
    listofstudents.add("Greg");
    listofstudents.add("James");
    listofstudents.add("Carlos");
    listofstudents.add("Bryan");
    listofstudents.add("Hector");
    
 
    
    //listofstudents.print();
    //displays all names on the list
    
    listofstudents.print();
       cout << "Did it work? :)" << endl;
       cout << "Lets find out!" << endl;
       
       cout << " " <<endl;
    
   listofstudents.getStudent("Bryan");
  
  
    
    cin.get();
    return 0;
}

You're closer than you think with this.

void getStudent (string student)
{ 
     studentnode* current;
     current = start; 
     while(current!=NULL)
     {   
         if(current->data == student)   
         {    
               cout << "The Specified Student Has Been Found: "<< endl;
               cout << student <<endl;  
                 break;  
                 }  
                  current = current->next;  
                  }
                  }

       

       //display all the items in the list
       void print()
       {
            studentnode * current;
            current = start;
            
           //loop through all nodes, each time doing something
           while(current != NULL )
           {
               //display the data of the current node
               cout << current->data << endl;
               
               //move current to next node
               current = current->next;               
           }     
       }
};

What if you modified the parameter to getStudent(int id) - then you could, instead of cout << student << yada, just do current->printData(), right?

i understand that part.
but how will the program know to retrieve an id if i never specified it to store that memory in the add function.
is this where i should edit the add function and make it to something like:

void add(string student, sting university, int id)
{
    ...//blah blah blah
}

Whoa whoa wait... No. :)

You can still add Student objects to your list. You just need to modify your Student class to have the new data you need.

e.g.,

string FirstName;
string LastName;
etc.
etc.

Now, if you are wanting to both declare a new Student and add it to your list, you are correct in your new parameter list.

@OP: Are you allowed to do this :

struct StudentInfo{
  string name;
  int id;
  //more stuff if you need it

};
struct StudentNode{
 StudentInfo data;
 StudentNode *next;
 StudentNode() : data(), next(NULL){}
 StudentNode( const StudentInfo& info, StudentNode *pNext = NULL)
  : data(info), next( pNext){}
};

class StudentList{
 //...details
public:
 void add(const StudentInfo& info){
    if(start == NULL){
      start= new StudentNode(info,NULL);
    }
    else {
        StudentNode *lastNode = moveToLastNode(start); //returns a Node to the last member
        lastNode->next = StudentNode(info,NULL);
    }
  }
}

i think i see where your going with this.
i have everything in one file. im using devc++ so i dont think i can make a .h file.
here is my entire code as i have it so far:

#include <iostream>
#include <string>


using namespace std;


//new class for studentnode
class studentnode
{
public:
       string data;
       studentnode * next;  
	   

	   // string to hold student name
	   string student;

};


//new class for studentlist
class studentlist
{
private:      
      studentnode * start;
      
      
public:
       studentlist()
       {
           start = NULL;            
       }
       
   
       
       // function to add a new student to list
       void add(string student)
       {
           //create a new node
           studentnode * somenode;
           somenode = new studentnode;
           
           //put student in the new node!
           somenode->data = student;     
           
           //put new node at front of list!
           somenode->next = start;
           start = somenode;
       }
       

       //function to a specified student, if not traverse the list until found.
void getStudent (string student)
{ 
     studentnode* current;
     current = start; 
     while(current!=NULL)
     {   
         if(current->data == student)   
         {    
               cout << "The Specified Student Has Been Found: "<< endl;
               cout << student <<endl;  
                 break;  
                 }  
                  current = current->next;  
                  }
                  }

       

       //display all the items in the list
       void print()
       {
            studentnode * current;
            current = start;
            
           //loop through all nodes, each time doing something
           while(current != NULL )
           {
               //display the data of the current node
               cout << current->data << endl;
               
               //move current to next node
               current = current->next;               
           }     
       }
};







//main program thats ran.
int main()
{
    cout <<"This linked list should display students as they are added" << endl;
    
    
    studentlist listofstudents;
    
    listofstudents.add("Greg");
    listofstudents.add("James");
    listofstudents.add("Carlos");
    listofstudents.add("Bryan");
    listofstudents.add("Hector");
    
 
    
    //listofstudents.print();
    //displays all names on the list
    
    listofstudents.print();
       cout << "Did it work? :)" << endl;
       cout << "Lets find out!" << endl;
       
       cout << " " <<endl;
    
   listofstudents.getStudent("Bryan");
  
  
    
    cin.get();
    return 0;
}

You can alter your studentnode class (you can also make it a struct) as such:

class studentnode
{
public:
       string data;
       studentnode * next;  
       string university;
       int id;   

	   // string to hold student name
	   string student;

};

This will add the fields you need for your nodes.

Then you can alter this code to add the information to it.

void add(string student, string university, int id)
       {
           //create a new node
           studentnode * somenode;
           somenode = new studentnode;
           
           //put student in the new node!
           somenode->data = student;     
           //add students university
           ....
           //add students ID #
           ....
           
           //put new node at front of list!
           somenode->next = start;
           start = somenode;
       }

Finally you would need to change your function calls in main

listofstudents.add("Greg", "University of Foo", 100);

Greg's ID is 100, and he's attending University of Foo.

yes, i need to declare a new student and add it to the list.
heres what i have so far, but i get an error.

void add(string student, string university, int id)
       {
           //create a new node
           node * somenode;
           somenode = new node;
           
           //put student in the new node!
           somenode->data = (student, university, id)
          
              
           
           //put new node at front of list!
           somenode->next = start;
           start = somenode;
       }

One other thing I noticed is in your class studentnode

class studentnode{
public:
       string data;
       studentnode * next;
 	   // string to hold student name
       string student; };

You don't use student to hold the students name. You use data. You can remove string student; from the program, and it should still function.

yes, i need to declare a new student and add it to the list.
heres what i have so far, but i get an error.

void add(string student, string university, int id)
       {
           //create a new node
           node * somenode;
           somenode = new node;
           
           //put student in the new node!
           somenode->data = (student, university, id)
          
              
           
           //put new node at front of list!
           somenode->next = start;
           start = somenode;
       }

no no ... you need to break that up ...

somenode->data = student;
somenode->university = university;
somenode->id = id;

Edit:

I also recommend you don't use university or id in the function call. I usually found it to be bad programming practice to use class variables and function variables confusing if they were using the same name.

:)

sucess!! it works!
thank you so much!
and yes, i will change the names when i use the function call. thank you very much. it is now running!!
:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.