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

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

};

public:
Node* start;

start = NULL;
}//default constructor

Node* temp = l.start;
if(temp == NULL){
cout << "List is empty";
exit(1);
}//if
else{
while(temp != NULL){
temp = temp->next;
}//while

}//else
}//copy constructor

delete start;
}//destructor

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

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

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

};

int main()
{
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);
}//while
}//while
myfile.close();
}//if
else cout << "Unable to open file" << endl;

cout << "\nSorted List: " << endl;
current->display();
cout << endl;

add = current;//how can I make add be a separate copy of current?

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

So is this the input file?

``````-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``````
``````LinkedList(const LinkedList& l){
Node* temp = l.start;
if(temp == NULL){
cout << "List is empty";
exit(1);
}//if
else{
while(temp != NULL){
temp = temp->next;
}//while

}//else
}//copy constructor``````

``2,2 -> 4,2 -> 6,2 -> 8,8``