c++ - Changing linked list into template -
i've written linked list stores informations students. how can change template, have store ints or other type? have overload methods in linked list class, cause take 6 arguments? mean, method inserting @ end(for student data) looks this:
void insertatend(int index, string name, string surname, int yearofstudy, string fieldofstudy, string specialty);
and if want store ints, this:
void insertatend(int data);
so if want use templates adding students , example integers, should this?
template <class t> class llist{ void insertatend(int index, string name, string surname, int yearofstudy, string fieldofstudy, string specialty); void insertatend <t data>; }
here implementation of student , linkedlist class:
class student { public: int index; string name; string surname; int yearofstudy; string fieldofstudy; string specialty; student *next; //pointer next item student(); }; student::student() { next = 0; } class llist { public: void insertatend (int index, string name, string surname, int yearofstudy, string fieldofstudy, string specialty); void insertatbeginning (int index, string name, string surname, int yearofstudy, string fieldofstudy, string specialty); void insertatgivenposition(int a, string n, int index, string name, string surname, int yearofstudy, string fieldofstudy, string specialty); void findstudent(int index, string surname); void deletelast (); void deleteselected(int index, string surname); void deleteall (); void displaylist (); student *first; //pointer on first elem llist(); };
within constraints of question, if me, change class structure to:
class student { public: int index; string name; string surname; int yearofstudy; string fieldofstudy; string specialty; //student *next; //pointer next item student(); }; template <class t> class node { t data; node<t> *next; } template <class t> class llist { public: void insertatend (const t &data); . . . private: node<t> *list; }
take @ above psuedo code , see how/if answers questions.
Comments
Post a Comment