c++ - How to create and execute a copy constructor for a linked list? -


i have tried looking @ videos , older posts still difficult understand concept of copy constructors. clear me? class did not cover part 100% professor focused on constructors , destructors.

main cpp

#include <iostream> #include "header.h" using namespace std;   int main() {     node access;      access.getdata();     access.outdata();      system("pause");     return 0; } 

header file

#include <iostream>  using namespace std;  class node { public:     node(); // had create own default constructor because of copy constructor.     node(const node &n); // copy constructor.     ~node();     void getdata();     void outdata(); private:     int num;     int lcount = 0; // counts number of nodes, increments after each user input.     int *ptr; // linked list copied     node *next;     node *first;     node *temp;     node *point; };  node::node() {     num = 0; }  node::node(const node &n) {     temp = first;      ptr = new node;     (int = 0; < lcount; i++)     {         ptr[i] = temp->num;         temp = temp->next;     }  }  node::~node() // deletes linked list. {     while (first != null)     {         node *delp = first; // creates pointer delp pointing first node.         first = first->next; // "removes first node list , declares new first.         delete delp; // deletes node removed.     }     cout << "list deleted" << endl; }  void node::getdata() // simple function creates linked list user input. {     int input = 0;     point = new node;     first = point;     temp = point;      while (input != -1)     {         cout << "enter integer, -1 end." << endl;         cin >> input;          if (input == -1)         {             point->next = null;             break;         }         else         {             lcount++;             point->num = input;             temp = new node;             point->next = temp;             point = temp;         }     } }  void node::outdata() {     temp = first;     cout << "original" << endl;     while (temp->next != null)     {         cout << temp->num << endl;         temp = temp->next;     }      cout << "copied" << endl;     (int = 0; < lcount; i++)     {         cout << ptr[i] << endl;     } } 

this little snippet having trouble in particular:

node::node(const node &n) {     temp = first;      ptr = new node;     (int = 0; < lcount; i++)     {         ptr[i] = temp->num;         temp = temp->next;     } } 

i figured out! tinkering simpler copy constructor. having trouble understanding syntax, complicated , overwhelming at.

#include <iostream>  using namespace std;  class node { public:     node(int x); // normal construtor     node(const node &cpy); // copy constructor     void change(); // changes data value     void outdata(); private:     int data; };  int main() {     node var1(123);     var1.outdata();     node var2 = var1;     var2.outdata();      var2.change();     var1.outdata();     var2.outdata();      system("pause");     return 0; }  node::node(int x) {     data = x; }  node::node(const node &cpy) {     data = cpy.data; }  void node::outdata() {     cout << data << endl; }  void node::change() {     int userin;     cin >> userin;     data = userin; } 

output:

123 123 

(input: 4444)

output:

123 4444 

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