Help with getting multiple copies(copy constructor help)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 58
Reputation: ff4930 is an unknown quantity at this point 
Solved Threads: 2
ff4930 ff4930 is offline Offline
Junior Poster in Training

Help with getting multiple copies(copy constructor help)

 
0
  #1
Jun 13th, 2008
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?


  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <sstream>
  6.  
  7. using namespace std;
  8.  
  9. class Node{
  10. public:
  11. int coef;
  12. int exp;
  13. Node* next;
  14.  
  15. Node(){
  16. coef = 0;
  17. exp = 0;
  18. next = NULL;
  19. }//default constructor
  20.  
  21. Node(int c, int e){
  22. if(e < 0){
  23. cout << "Exponent is negative" << endl;
  24. exit(1);
  25. }//if check if exponent is < 0
  26. coef = c;
  27. exp = e;
  28. next = NULL;
  29. }//2 param constructor
  30.  
  31. ~Node(){
  32. delete next;
  33. }//destructor
  34.  
  35. };
  36.  
  37. class LinkedList{
  38. public:
  39. Node* start;
  40.  
  41. LinkedList(){
  42. start = NULL;
  43. }//default constructor
  44.  
  45. LinkedList(const LinkedList& l){
  46. Node* temp = l.start;
  47. if(temp == NULL){
  48. cout << "List is empty";
  49. exit(1);
  50. }//if
  51. else{
  52. while(temp != NULL){
  53. this->add(temp);
  54. temp = temp->next;
  55. }//while
  56.  
  57. }//else
  58. }//copy constructor
  59.  
  60. ~LinkedList(){
  61. delete start;
  62. }//destructor
  63.  
  64.  
  65. void add(Node*& n){
  66. if(start == NULL){
  67. start = n;
  68. start->next = NULL;
  69. }//if
  70. else{
  71. Node* temp = start;
  72. if(n->exp >= temp->exp){
  73. n->next = temp;
  74. start = n;
  75. return;
  76. }//if
  77.  
  78. else{
  79. while(temp!= NULL){
  80. if(n->exp >= temp->next->exp){
  81. n->next = temp->next;
  82. temp->next = n;
  83. return;
  84. }//if
  85. temp = temp->next;
  86. }//while
  87.  
  88. }//else
  89. }//else
  90. }//add
  91.  
  92. void display(){
  93. Node* temp = start;
  94. do{
  95. if(temp == NULL){
  96. cout << "List is empty" << endl;
  97. return;
  98. }//if
  99. else{
  100. cout << temp->coef << " ";
  101. cout << temp->exp << " ";
  102. }//else
  103. temp = temp->next;
  104. }//do
  105. while(temp != NULL);//while
  106. cout << endl;
  107. }//display
  108.  
  109.  
  110.  
  111. void operationAdd(){
  112. Node* temp = start;
  113.  
  114. //if empty list or only 1 node
  115. if(temp == NULL || temp->next == NULL)
  116. return;
  117.  
  118. while(temp != NULL){
  119. if(temp->next == NULL)
  120. break;
  121. if(temp->exp == temp->next->exp){
  122. temp->coef += temp->next->coef;//add the coef
  123. Node* temp1 = temp->next;//temp1 holding it
  124. temp->next = temp1->next;//set temp next = to temp->next->next
  125. }//if
  126. else{
  127. if(temp->next == NULL)
  128. break;
  129. temp = temp->next;//if not keep going
  130. }//else
  131. }//while
  132. }//operationAdd
  133.  
  134.  
  135.  
  136.  
  137.  
  138. };
  139.  
  140.  
  141.  
  142. int main()
  143. {
  144. LinkedList* current = new LinkedList;
  145. string line;
  146. int repeat = 0;
  147.  
  148. ifstream myfile("input.txt");
  149. if(myfile.is_open()){
  150. while(!myfile.eof()){
  151. getline(myfile, line);
  152. repeat++;
  153. cout << "\nLine: " << repeat << " \n";
  154. cout << line << endl;
  155. int num=0, c=0, e=0;
  156. stringstream str(line);
  157. while(str >> num){
  158. c = num;
  159. str >> num;
  160. e = num;
  161. Node* temp = new Node(c,e);
  162. current->add(temp);
  163. }//while
  164. }//while
  165. myfile.close();
  166. }//if
  167. else cout << "Unable to open file" << endl;
  168.  
  169. cout << "\nSorted List: " << endl;
  170. current->display();
  171. cout << endl;
  172.  
  173. LinkedList* add = new LinkedList;
  174. add = current;//how can I make add be a separate copy of current?
  175.  
  176.  
  177. add->operationAdd();
  178. add->display();
  179. current->display();
  180.  
  181. return 0;
  182. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,836
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Help with getting multiple copies(copy constructor help)

 
0
  #2
Jun 14th, 2008
So is this the input file?

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


  1. LinkedList(const LinkedList& l){
  2. Node* temp = l.start;
  3. if(temp == NULL){
  4. cout << "List is empty";
  5. exit(1);
  6. }//if
  7. else{
  8. while(temp != NULL){
  9. this->add(temp);
  10. temp = temp->next;
  11. }//while
  12.  
  13. }//else
  14. }//copy constructor

If I understand your problem correctly, you have a linked list:

  1. 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC