嗨我的链表程序中有错误,我想在链表上进行合并排序。
我确实声明了它,但它仍然给我错误。
P.S。我没有完成合并排序功能,但合并假设已经编译。
请帮助!!
代码:
#ifndef _NODE
#define _NODE
template<typename ItemType>
class Node {
private:
ItemType item; // A data item
Node<ItemType>* next; // Pointer to next node
public:
Node();
Node(const ItemType& anItem);
Node(const ItemType& anItem, Node<ItemType>* nextNodePtr);
void setItem(const ItemType& anItem);
void setNext(Node<ItemType>* nextNodePtr);
void mergeSort(Node<ItemType>*& head);
Node<ItemType>* merge(Node<ItemType>*& head1, Node<ItemType>*& head2);
ItemType getItem() const;
Node<ItemType>* getNext() const;
}; // end Node
#endif
template<typename ItemType>
Node<ItemType>::Node() {
next(nullptr)
} // end default constructor
template<typename ItemType>
Node<ItemType>::Node(const ItemType& anItem) {
item(anItem);
next(nullptr);
} // end constructor
template<typename ItemType>
Node<ItemType>::Node(const ItemType& anItem, Node<ItemType>* nextNodePtr) {
item(anItem);
next(nextNodePtr);
} // end constructor
template<typename ItemType>
void Node<ItemType>::setItem(const ItemType& anItem) {
item = anItem;
} // end setItem
template<typename ItemType>
void Node<ItemType>::setNext(Node<ItemType>* nextNodePtr) {
next = nextNodePtr;
} // end setNext
template<typename ItemType>
ItemType Node<ItemType>::getItem() const {
return item;
} // end getItem
template<typename ItemType>
Node<ItemType>* Node<ItemType>::getNext() const {
return next;
} // end getNext
template<typename ItemType>
void Node<ItemType>::mergeSort(Node<ItemType>*& head) {
Node<ItemType>*countptr = head;
int length = 0;
while (countptr->getNext() != nullptr) {
length++;
countptr = countptr->getNext();
}
//if only one or non in list then return list
if (length <= 1)
return head;
//int half = length / 2;
}
template<typename ItemType>
ItemType Node<ItemType>::merge(Node<ItemType>*& head1, Node<ItemType>*& head2) {
Node<ItemType>*head = nullptr;
Node<ItemType>*cur = nullptr;
//set the first element
if (head1->getItem() >= head2->getItem()) {
head = head2;
head2 = head2->getNext();
}
else {
head = head1;
head1 = head1->getNext();
}
cur = head;
while (true) {
if (head1->getItem() == NULL && head2->getItem() != NULL) {
cur->setNext(head2);
cur = cur->getNext();
head2 = head2->getNext();
}
else if (head2->getItem() == NULL && head1->getItem() != NULL) {
cur->setNext(head1);
cur = cur->getNext();
head1 = head1->getNext();
}
else if (head1->getItem() >= head2->getItem()) {
cur->setNext(head2);
cur = cur->getNext();
head2 = head2->getNext();
}
else if (head1->getItem() <= head2->getItem()) {
cur->setNext(head1);
cur = cur->getNext();
head1 = head1->getNext();
}
else {
break;
}
}
return head;
}
这是我的错误:
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE
\xlocale(341): war
ning C4530: C++ exception handler used, but unwind semantics are not enabled. Sp
ecify /EHsc
c:\users\mykev\desktop\Node.h(145): error C2244: 'Node<ItemType>::merge': unable
to match function definition to an existing declaration
c:\users\mykev\desktop\Node.h(100): note: see declaration of 'Node<ItemType>::me
rge'
c:\users\mykev\desktop\Node.h(145): note: definition
c:\users\mykev\desktop\Node.h(145): note: 'ItemType Node<ItemType>::merge(Node<I
temType> *&,Node<ItemType> *&)'
c:\users\mykev\desktop\Node.h(145): note: existing declarations
c:\users\mykev\desktop\Node.h(145): note: 'Node<ItemType> *Node<ItemType>::merge
(Node<ItemType> *&,Node<ItemType> *&)'
答案 0 :(得分:1)
您(至少)代码中存在两个问题
1)您的默认构造函数在第29行附近缺少分号
2)你的合并原型(第17行)看起来不像你的实现(第100行)。您可以将第100行更改为更像:
template<typename ItemType>
Node<ItemType>* Node<ItemType>::merge(Node<ItemType>*& head1, Node<ItemType>*& head2) {