我有两个带有未知长度的数字节点的双向链表。 我需要添加和乘以两个列表。什么是解决问题的好方法 ?我尝试使用运算符重载+,*但不清楚算法
主要在这里
//get line input from file
while (getline(ifs, line)){
string operation=" ";
string num1=" ";
string num2=" ";
int operationIndex=0;
//get string from stringstream
//delimiter here + - * / to split string to two part
for (int i=0; i<line.length(); i++) {
if (ispunct(line[i])) {
operationIndex = i;
}
}
num1 = line.substr(0,operationIndex);
num2 = line.substr(operationIndex+1);
operation = line[operationIndex];
//Dynamic lists
DoublyLinkedList* l1 = new DoublyLinkedList(num1,digitsPerNode);
DoublyLinkedList* l2 = new DoublyLinkedList(num2,digitsPerNode);
DoublyLinkedList* result = new DoublyLinkedList;
//Do math here
if (operation == "+") {
//*result = *l1 + *l2;
}
else if(operation == "*"){
//*result = *l1 + *l2;
}
//print answer here
cout << num1 << operation << num2 << "=";
result->printList();
//l1->printList();
//l2->printList();
//free lists memory
delete l1;
delete l2;
delete result;
}
在这里重载
//addition
DoublyLinkedList DoublyLinkedList::operator+(const DoublyLinkedList &l2) const
{
DoublyLinkedList result;
return result;
}
//multiplication
DoublyLinkedList DoublyLinkedList::operator*(const DoublyLinkedList &l2) const
{
DoublyLinkedList result;
return result;
}