| | |
Help with getting multiple copies(copy constructor help)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 58
Reputation:
Solved Threads: 2
Hello everyone,
My project consists of reading 2 lines of integers separated by white spaces corresponding coefficents and exponents,
each line representing a polynominal.
I have to add both polynominals and subtract them.
I have made a linked list class to hold each coefficent and exponent in a node.
The problem I have is making multiple copies of the original polynominal.
I have a the polynominal sorted in Linkedlist current and I made another Linkedlist call add
when I add the polynomials in LinkedList add, it does the same to my current LinkedList.
I want to know how can I make separate copies so each is dependent?
I know it passes the same pointer to each Linkedlist but is there a way not to?
my output
Line: 1
-1 0 5 1 20 3 -9 2 -2 1 1 2 -2 3 1 9 6 3 2 2 -1 1 10 0
Line: 2
5 2 -2 9 10 3 4 0 -3 1 1 1 6 3 -4 3 -5 2 8 8 9 4 4 4
Sorted List:
-2 9 1 9 8 8 4 4 9 4 -4 3 6 3 10 3 6 3 -2 3 20 3 -5 2 5 2 2 2 1 2 -9 2 1 1 -3 1 -1 1 -2 1 5 1 4 0 10 0 -1 0
-1 9 8 8 13 4 36 3 -6 2 0 1 13 0 --add->display
-1 9 8 8 13 4 36 3 -6 2 0 1 13 0 -- current->display
I only called operationAdd on the "add" linkedlist but it does the same to my current linkedlist.
I want current to stay
-2 9 1 9 8 8 4 4 9 4 -4 3 6 3 10 3 6 3 -2 3 20 3 -5 2 5 2 2 2 1 2 -9 2 1 1 -3 1 -1 1 -2 1 5 1 4 0 10 0 -1 0
so I can use that to subtract but whatever I do to "add" linkedlist it does the same to "current".
Please help.
My project consists of reading 2 lines of integers separated by white spaces corresponding coefficents and exponents,
each line representing a polynominal.
I have to add both polynominals and subtract them.
I have made a linked list class to hold each coefficent and exponent in a node.
The problem I have is making multiple copies of the original polynominal.
I have a the polynominal sorted in Linkedlist current and I made another Linkedlist call add
when I add the polynomials in LinkedList add, it does the same to my current LinkedList.
I want to know how can I make separate copies so each is dependent?
I know it passes the same pointer to each Linkedlist but is there a way not to?
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; class Node{ public: int coef; int exp; Node* next; Node(){ coef = 0; exp = 0; next = NULL; }//default constructor Node(int c, int e){ if(e < 0){ cout << "Exponent is negative" << endl; exit(1); }//if check if exponent is < 0 coef = c; exp = e; next = NULL; }//2 param constructor ~Node(){ delete next; }//destructor }; class LinkedList{ public: Node* start; LinkedList(){ start = NULL; }//default constructor LinkedList(const LinkedList& l){ Node* temp = l.start; if(temp == NULL){ cout << "List is empty"; exit(1); }//if else{ while(temp != NULL){ this->add(temp); temp = temp->next; }//while }//else }//copy constructor ~LinkedList(){ delete start; }//destructor void add(Node*& n){ if(start == NULL){ start = n; start->next = NULL; }//if else{ Node* temp = start; if(n->exp >= temp->exp){ n->next = temp; start = n; return; }//if else{ while(temp!= NULL){ if(n->exp >= temp->next->exp){ n->next = temp->next; temp->next = n; return; }//if temp = temp->next; }//while }//else }//else }//add void display(){ Node* temp = start; do{ if(temp == NULL){ cout << "List is empty" << endl; return; }//if else{ cout << temp->coef << " "; cout << temp->exp << " "; }//else temp = temp->next; }//do while(temp != NULL);//while cout << endl; }//display void operationAdd(){ Node* temp = start; //if empty list or only 1 node if(temp == NULL || temp->next == NULL) return; while(temp != NULL){ if(temp->next == NULL) break; if(temp->exp == temp->next->exp){ temp->coef += temp->next->coef;//add the coef Node* temp1 = temp->next;//temp1 holding it temp->next = temp1->next;//set temp next = to temp->next->next }//if else{ if(temp->next == NULL) break; temp = temp->next;//if not keep going }//else }//while }//operationAdd }; int main() { LinkedList* current = new LinkedList; string line; int repeat = 0; ifstream myfile("input.txt"); if(myfile.is_open()){ while(!myfile.eof()){ getline(myfile, line); repeat++; cout << "\nLine: " << repeat << " \n"; cout << line << endl; int num=0, c=0, e=0; stringstream str(line); while(str >> num){ c = num; str >> num; e = num; Node* temp = new Node(c,e); current->add(temp); }//while }//while myfile.close(); }//if else cout << "Unable to open file" << endl; cout << "\nSorted List: " << endl; current->display(); cout << endl; LinkedList* add = new LinkedList; add = current;//how can I make add be a separate copy of current? add->operationAdd(); add->display(); current->display(); return 0; }
my output
Line: 1
-1 0 5 1 20 3 -9 2 -2 1 1 2 -2 3 1 9 6 3 2 2 -1 1 10 0
Line: 2
5 2 -2 9 10 3 4 0 -3 1 1 1 6 3 -4 3 -5 2 8 8 9 4 4 4
Sorted List:
-2 9 1 9 8 8 4 4 9 4 -4 3 6 3 10 3 6 3 -2 3 20 3 -5 2 5 2 2 2 1 2 -9 2 1 1 -3 1 -1 1 -2 1 5 1 4 0 10 0 -1 0
-1 9 8 8 13 4 36 3 -6 2 0 1 13 0 --add->display
-1 9 8 8 13 4 36 3 -6 2 0 1 13 0 -- current->display
I only called operationAdd on the "add" linkedlist but it does the same to my current linkedlist.
I want current to stay
-2 9 1 9 8 8 4 4 9 4 -4 3 6 3 10 3 6 3 -2 3 20 3 -5 2 5 2 2 2 1 2 -9 2 1 1 -3 1 -1 1 -2 1 5 1 4 0 10 0 -1 0
so I can use that to subtract but whatever I do to "add" linkedlist it does the same to "current".
Please help.
Last edited by ff4930; Jun 13th, 2008 at 11:48 pm.
•
•
Join Date: Jan 2008
Posts: 3,836
Reputation:
Solved Threads: 503
So is this the input file?
If I understand your problem correctly, you have a linked list:
The above linked list has four nodes total. start points to the first node, which contains (2,2) as its data. Your two linked lists are identical since start points to the same node after this copy constructor has finished. The linked lists contain the same four nodes, so anything you do to one of them you are doing to the other. I think you need to create another four nodes in your copy constructor. You should create a Node copy constructor that creates a new Node from another Node, then makes the data held by the new Node the same as the data held by the copied Node. Traverse through the original Linked List as you do, but this time when you hit a new node, call your Node copy constructor. So you end up with eight separate Nodes rather than four. No Node in either of the Linked Lists points to a Node in the other Linked List. start also points to different Nodes in the two Linked Lists. Then you can do something to one linked list and have it not affect the other. I can't think of any way that you can have a Linked List copy constructor without creating new Nodes, which you do not do in your code, and have it do what I think you want it to do.
C++ Syntax (Toggle Plain Text)
-1 0 5 1 20 3 -9 2 -2 1 1 2 -2 3 1 9 6 3 2 2 -1 1 10 0 5 2 -2 9 10 3 4 0 -3 1 1 1 6 3 -4 3 -5 2 8 8 9 4 4 4
C++ Syntax (Toggle Plain Text)
LinkedList(const LinkedList& l){ Node* temp = l.start; if(temp == NULL){ cout << "List is empty"; exit(1); }//if else{ while(temp != NULL){ this->add(temp); temp = temp->next; }//while }//else }//copy constructor
If I understand your problem correctly, you have a linked list:
C++ Syntax (Toggle Plain Text)
2,2 -> 4,2 -> 6,2 -> 8,8
The above linked list has four nodes total. start points to the first node, which contains (2,2) as its data. Your two linked lists are identical since start points to the same node after this copy constructor has finished. The linked lists contain the same four nodes, so anything you do to one of them you are doing to the other. I think you need to create another four nodes in your copy constructor. You should create a Node copy constructor that creates a new Node from another Node, then makes the data held by the new Node the same as the data held by the copied Node. Traverse through the original Linked List as you do, but this time when you hit a new node, call your Node copy constructor. So you end up with eight separate Nodes rather than four. No Node in either of the Linked Lists points to a Node in the other Linked List. start also points to different Nodes in the two Linked Lists. Then you can do something to one linked list and have it not affect the other. I can't think of any way that you can have a Linked List copy constructor without creating new Nodes, which you do not do in your code, and have it do what I think you want it to do.
![]() |
Other Threads in the C++ Forum
- Previous Thread: external resolution errors during linking
- Next Thread: file writing speed (API)
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int integer java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






