c++ - Having problems creating function that copies linked list while in template -


assignment make list of integers , functions various things. finished one.

after figured out how make function searches list specific value , node, have make function duplicates said list. however, i'm having serious trouble doing material have right now.

this class i'm working (t template)

    template <class t>     class integerlist{     private:     struct listnode {         t value;         struct listnode *next;     };      listnode *head;     public:     //this constructor.     integerlist()     {head = null;}      //destructor     ~integerlist();      void appendnode(t);     void insertnode(t);     void deletenode(t);     void searchnode(t);     void duplicatenode(t);     void displaylist() const; }; 

the functions far:

//==appendnode definition== template<class t> void integerlist<t>::appendnode(t newvalue) {     listnode *newnode;     listnode *nodeptr;      newnode = new listnode;     newnode->value = newvalue;     newnode->next = null;      if (!head) head = newnode;     else {         nodeptr = head;         while (nodeptr->next)nodeptr = nodeptr->next;         nodeptr->next = newnode;     }  }  //==displaylist definition== template<class t> void integerlist<t>::displaylist() const {     listnode *nodeptr;     nodeptr = head;      while (nodeptr) {         cout << nodeptr->value << "";         nodeptr = nodeptr->next;     }     cout << endl; }  //==insertnode definiton== template<class t> void integerlist<t>::insertnode(t newvalue) {     listnode *newnode;     listnode *nodeptr;     listnode *previousnode = null;      newnode = new listnode;     newnode->value = newvalue;     if (!head) {         head = newnode;         newnode->next = null;     }     else {         nodeptr = head;         previousnode = null;          while (nodeptr != null && nodeptr->value < newvalue) {             previousnode = nodeptr;             nodeptr = nodeptr->next;         }         if (previousnode == null) {             head = newnode;             newnode->next = nodeptr;         }         else {             previousnode->next = newnode;             newnode->next = nodeptr;         }     } }  //==deletenode definition== template<class t> void integerlist<t>::deletenode(t searchvalue) {     listnode *nodeptr;     listnode *previousnode = null;      if (!head) return;     if (head->value == searchvalue) {         nodeptr = head->next;         delete head;         head = nodeptr;     }     else {         nodeptr = head;     while (nodeptr != null && nodeptr->value != searchvalue) {         previousnode = nodeptr;         nodeptr = nodeptr->next;         }         if (nodeptr) {             previousnode->next = nodeptr->next;             delete nodeptr;         }     } }  //searchnode definiton template <class t> void integerlist<t>::searchnode(t searchvalue) {     listnode *nodeptr=0;     nodeptr = head;     int = searchvalue;                 //this variable initiated remember number search for. use in if statement.     int j = 0;                           //this variable dedicated position number, starting 0. increments 1 when while loop loops.         while (nodeptr)         {             if (i == nodeptr->value) {                 //this if statemtent return success message position number if number found.                 cout << "\nthe value "<< nodeptr->value <<" found in list, in position " << j <<" of list.\n";                 return;             }             else             {                 nodeptr = nodeptr->next;                 j++;             }         }         //this message plays when goes through list without finding value.         cout << "\nthe value " << << " not found in list.\n"; }  //==duplicatenode definition== template<class t> void integerlist<t>::duplicatenode(t) {     if (list == null) return null;      listnode* result = new listnode;     result->value = list->value;     result->next = clone(list->next);     return result; }    //==destructor definition== template<class t> integerlist<t>::~integerlist() {     listnode *nodeptr;     listnode *nextnode;     nodeptr = head;     while (nodeptr != null) {         nextnode = nodeptr->next;         delete nodeptr;         nodeptr = nextnode;     } } 

and main function testing happens.

int main() {     integerlist<int> list1;       list1.appendnode(1);     list1.appendnode(2);     list1.appendnode(5);      list1.displaylist();     list1.insertnode(4);      list1.displaylist();      list1.deletenode(2);     list1.displaylist();      cout << "\nthis line breaks denote searchnode function running.\n";      list1.searchnode(5);     list1.searchnode(3);      cout << "\nline break denote copynode function running.\n";      integerlist<int> list2(list1);     list2.displaylist();      cin.ignore();     cin.get();      return 0; } 

searching around has not yielded me useful or usable answers. there way can while keeping template?

when looking @ provided source code, way duplicate list1 list2 has not been completed.

analysis - existing class integerlist, use of integerlist<int> list2(list1); duplicate performed @ data level. class contains 1 pointer data listnode *head; , default copying constructor copy list1->head list2->head. result is:

  1. using appendnode() or insertnode() list1 add same node list2,
  2. using deletenode() list1 remove same node list2,
  3. using displaylist() list2 same list1.

solution - perform copy of list1 list2 , allow manage both lists independently, necessary add own copy-constructor.

in class integerlist declaration, append copy-constructor. source list passed-by reference.

integerlist(integerlist& src); 

then add copy-constructor implementation.

  1. the destination listnode *head; initialize null before starting copy,
  2. the source list explored src.head until null in displaylist() function,
  3. each source node add in destination list using appendnode(),

the proposed copy-constructor:

template<class t> integerlist<t>::integerlist(integerlist& src) : head(null) {     listnode *nodeptr;     nodeptr = src.head;      while (nodeptr) {         appendnode(nodeptr->value);         nodeptr = nodeptr->next;     } } 

no change needed in main() function. use of integerlist<int> list2(list1); automatically call specific copy-constructor instead of default one.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -