在模板中创建复制链表的函数时遇到问题

Having problems creating function that copies linked list while in template

本文关键字:函数 遇到 问题 链表 复制 创建      更新时间:2023-10-16

赋值就是创建一个由整数和函数组成的列表,这些整数和函数可以做不同的事情。除了一个我都完成了。

在我弄清楚如何编写一个函数来搜索列表中特定的值和节点之后,我必须编写一个函数来复制该列表。但是,我现在用现有的材料做这件事遇到了严重的困难。

这是我正在使用的类(T是模板)

    template <class T>
    class IntegerList{
    private:
    struct ListNode {
        T value;
        struct ListNode *next;
    };
    ListNode *head;
    public:
    //This is the constructor.
    IntegerList()
    {head = NULL;}
    //Destructor
    ~IntegerList();
    void appendNode(T);
    void insertNode(T);
    void deleteNode(T);
    void searchNode(T);
    void Duplicatenode(T);
    void displayList() const;
};

目前的功能:

//==appendNode definition==
template<class T>
void IntegerList<T>::appendNode(T newValue) {
    ListNode *newNode;
    ListNode *nodePtr;
    newNode = new ListNode;
    newNode->value = newValue;
    newNode->next = NULL;
    if (!head) head = newNode;
    else {
        nodePtr = head;
        while (nodePtr->next)nodePtr = nodePtr->next;
        nodePtr->next = newNode;
    }
}
//==displayList Definition==
template<class T>
void IntegerList<T>::displayList() const {
    ListNode *nodePtr;
    nodePtr = head;
    while (nodePtr) {
        cout << nodePtr->value << "";
        nodePtr = nodePtr->next;
    }
    cout << endl;
}
//==insertNode Definiton==
template<class T>
void IntegerList<T>::insertNode(T newValue) {
    ListNode *newNode;
    ListNode *nodePtr;
    ListNode *previousNode = NULL;
    newNode = new ListNode;
    newNode->value = newValue;
    if (!head) {
        head = newNode;
        newNode->next = NULL;
    }
    else {
        nodePtr = head;
        previousNode = NULL;
        while (nodePtr != NULL && nodePtr->value < newValue) {
            previousNode = nodePtr;
            nodePtr = nodePtr->next;
        }
        if (previousNode == NULL) {
            head = newNode;
            newNode->next = nodePtr;
        }
        else {
            previousNode->next = newNode;
            newNode->next = nodePtr;
        }
    }
}
//==deleteNode Definition==
template<class T>
void IntegerList<T>::deleteNode(T searchValue) {
    ListNode *nodePtr;
    ListNode *previousNode = NULL;
    if (!head) return;
    if (head->value == searchValue) {
        nodePtr = head->next;
        delete head;
        head = nodePtr;
    }
    else {
        nodePtr = head;
    while (nodePtr != NULL && nodePtr->value != searchValue) {
        previousNode = nodePtr;
        nodePtr = nodePtr->next;
        }
        if (nodePtr) {
            previousNode->next = nodePtr->next;
            delete nodePtr;
        }
    }
}
//searchNode Definiton
template <class T>
void IntegerList<T>::searchNode(T searchValue)
{
    ListNode *nodePtr=0;
    nodePtr = head;
    int i = searchValue;
                //This variable is initiated to remember the number to search for. For use in if statement.
    int j = 0;          
                //This variable is dedicated to the position number, starting with 0. Increments by 1 when the while loop loops.
        while (nodePtr)
        {
            if (i == nodePtr->value) {
                //This if statemtent will return a success message with the position number if the number is found.
                cout << "nThe value "<< nodePtr->value <<" was found in the list, in position " << j <<" of this list.n";
                return;
            }
            else
            {
                nodePtr = nodePtr->next;
                j++;
            }
        }
        //This message only plays when it goes through the list without finding the value.
        cout << "nThe value " << i << " was not found in this list.n";
}
//==Duplicatenode Definition==
template<class T>
void IntegerList<T>::Duplicatenode(T)
{
    if (list == NULL) return NULL;
    ListNode* result = new ListNode;
    result->value = list->value;
    result->next = Clone(list->next);
    return result;
}

//==Destructor Definition==
template<class T>
IntegerList<T>::~IntegerList() {
    ListNode *nodePtr;
    ListNode *nextNode;
    nodePtr = head;
    while (nodePtr != NULL) {
        nextNode = nodePtr->next;
        delete nodePtr;
        nodePtr = nextNode;
    }
}

和测试发生的主要函数。

int main() {
    IntegerList<int> list1;

    list1.appendNode(1);
    list1.appendNode(2);
    list1.appendNode(5);
    list1.displayList();
    list1.insertNode(4);
    list1.displayList();
    list1.deleteNode(2);
    list1.displayList();
    cout << "nThis line breaks to denote searchNode function running.n";
    list1.searchNode(5);
    list1.searchNode(3);
    cout << "nLine break to denote copyNode function running.n";
    IntegerList<int> list2(list1);
    list2.displayList();
    cin.ignore();
    cin.get();
    return 0;
}

四处寻找并没有给我有用或可用的答案。有一种方法,我可以这样做,同时保持与模板?

在查看所提供的源代码时,将list1复制为list2的方法尚未完成。

Analysis—对于现有的class IntegerList,使用IntegerList<int> list2(list1);进行复制将仅在数据级别执行。该类只包含一个指针数据ListNode *head;,默认的复制构造函数会将list1->head复制到list2->head中。结果是:

  1. 使用appendNode()insertNode()list1将添加相同的节点到list2
  2. 使用deleteNode()list1将删除相同的节点到list2
  3. 使用displayList()list2将与list1相同。

解决方案 -执行list1list2的复制,并允许独立管理两个列表,有必要添加自己的copy- constructor。

class IntegerList声明中,附加复制构造函数。源列表通过引用传递。

IntegerList(IntegerList& src);

然后添加复制构造函数实现。

  1. 目标ListNode *head;在开始复制之前初始化为NULL
  2. 源列表从src.headNULL,如displayList()函数,
  3. 每个源节点都使用appendNode()添加到目标列表中,

建议复制构造函数:

template<class T>
IntegerList<T>::IntegerList(IntegerList& src) : head(NULL) {
    ListNode *nodePtr;
    nodePtr = src.head;
    while (nodePtr) {
        appendNode(nodePtr->value);
        nodePtr = nodePtr->next;
    }
}

不需要更改main()函数。的使用IntegerList<int> list2(list1);将自动调用特定的复制构造函数,而不是默认的。

相关文章: