【发布时间】:2015-10-27 04:37:54
【问题描述】:
我正在编写一些使用链表的代码,我很难确定为什么我的 + 运算符不起作用,我不断收到上述错误。
我错过了什么?
#include <iostream>
#include <string>
using namespace std;
struct sortedListNode
{
char letter;
int occurrences = 1;
sortedListNode *next;
};
sortedListNode *operator+(sortedListNode *lhs, sortedListNode *rhs)
{
int i = 0;
sortedListNode *head, *tail, *curr, *prev, *tempLoc;
sortedListNode *list1, *list1curr, *list2, *list2curr;
list1 = lhs;
list2 = rhs;
// Copy list1 into output list
head = new sortedListNode;
head->letter = list1->letter;
head->occurrences = list1->occurrences;
head->next = NULL;
tail = head;
list1curr = list1;
list1curr = list1curr->next;
while (list1curr != NULL)
{
tempLoc = new sortedListNode;
tempLoc->letter = list1curr->letter;
tempLoc->occurrences = list1curr->occurrences;
tempLoc->next = NULL;
tail->next = tempLoc;
tail = tempLoc;
list1curr = list1curr->next;
}
curr = head;
while (list2curr != NULL)
{
while (curr != NULL)
{
if (curr->letter == list2curr->letter)
{
curr->occurrences++;
break;
}
else if ((curr->letter > list2curr->letter) && (curr == head))
{
tempLoc = new sortedListNode;
tempLoc->next = curr;
tempLoc->letter = list2curr->letter;
head = tempLoc;
break;
}
else if ((curr->letter > list2curr->letter) && (curr != head))
{
tempLoc = new sortedListNode;
tempLoc->next = curr;
tempLoc->letter = list2curr->letter;
prev->next = tempLoc;
break;
}
else if ((curr == tail) && (curr->letter < list2curr->letter))
{
tempLoc = new sortedListNode;
tempLoc->next = NULL;
tempLoc->letter = list2curr->letter;
tail->next = tempLoc;
tail = tempLoc;
break;
}
prev = curr;
curr = curr->next;
}
curr = head;
list2curr = list2curr->next;
}
return head;
}
sortedListNode *fromString(string inWord)
{
int i = 0;
sortedListNode *head, *tail, *curr, *prev, *tempLoc;
// Put the first letter in as the first element, set head and
// tail to this element.
head = new sortedListNode;
tail = head;
head->letter = inWord[0];
head->next = NULL;
curr = head;
for (int i = 1; inWord[i] != '\0'; i++)
{
while (curr != NULL)
{
if (curr->letter == inWord[i])
{
curr->occurrences++;
break;
}
else if ((curr->letter > inWord[i]) && (curr == head))
{
tempLoc = new sortedListNode;
tempLoc->next = curr;
tempLoc->letter = inWord[i];
head = tempLoc;
break;
}
else if ((curr->letter > inWord[i]) && (curr != head))
{
tempLoc = new sortedListNode;
tempLoc->next = curr;
tempLoc->letter = inWord[i];
prev->next = tempLoc;
break;
}
else if ((curr == tail) && (curr->letter < inWord[i]))
{
tempLoc = new sortedListNode;
tempLoc->next = NULL;
tempLoc->letter = inWord[i];
tail->next = tempLoc;
tail = tempLoc;
break;
}
prev = curr;
curr = curr->next;
}
curr = head;
}
return head;
}
void printList(sortedListNode *inSortedListNode)
{
sortedListNode *curr;
curr = inSortedListNode;
int nodeCounter = 0;
while (curr != NULL)
{
nodeCounter++;
cout << "Node " << nodeCounter << " at " << curr << " - Letter = " << curr->letter << ", Occurrences = " << curr->occurrences << ", Next Node = "
<< curr->next << endl;
curr = curr->next;
}
}
int main()
{
string word1, word2;
sortedListNode *list1;
sortedListNode *list2;
sortedListNode *list3;
cout << "Enter first word: ";
cin >> word1;
cout << "Enter second word: ";
cin >> word2;
list1 = fromString(word1);
cout << "Letter list from word one: " << endl;
printList(list1);
list2 = fromString(word2);
cout << "Letter list from word two: " << endl;
printList(list2);
list3 = list1 + list2;
cout << "Letter list from both words: " << endl;
printList(list3);
cin.ignore(32767, '\n');
char dummy[1]{};
cin.getline(dummy, 1);
}
请注意,我知道运算符定义中有错误,因为我还没有写完。我试图克服将两个链表 sortedListNode 对象作为参数的障碍。
这是一个任务。 main 是由讲师提供的 stub main。运算符 + 应该取两个列表并将它们合并在一起,并输出合并后的列表。
【问题讨论】:
-
我很难确定您要编译的代码。请发帖MCVE。
-
澄清了运算符调用在 main 中,这就是我遇到语法错误的地方。
-
应该
+运算符只是连接两个列表(即将它们链接在一起)还是返回一个等于输入列表连接的新列表?您的代码似乎是后者,但参数(lhs,rhs)可能是const sortedListNode*。 -
对不起,它应该返回一个新列表,这是正确的。
-
你可以使用
std命名空间中的函数吗?
标签: c++ linked-list operator-overloading