Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在模板中创建复制链表的函数时遇到问题_C++_Linked List_Copy - Fatal编程技术网

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

C++ 在模板中创建复制链表的函数时遇到问题,c++,linked-list,copy,C++,Linked List,Copy,赋值就是把做各种事情的整数和函数列成一个列表。我只完成了一个 在我弄明白如何创建一个函数来搜索列表中的特定值和节点后,我必须创建一个复制所述列表的函数。然而,我现在用我手头的材料做这件事有很大的困难 这是我正在使用的类(T是模板) 模板 类整数列表{ 私人: 结构列表节点{ T值; 结构ListNode*next; }; ListNode*头; 公众: //这是构造函数。 整数列表() {head=NULL;} //析构函数 ~IntegerList(); 空节点(T); void-insert

赋值就是把做各种事情的整数和函数列成一个列表。我只完成了一个

在我弄明白如何创建一个函数来搜索列表中的特定值和节点后,我必须创建一个复制所述列表的函数。然而,我现在用我手头的材料做这件事有很大的困难

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

模板
类整数列表{
私人:
结构列表节点{
T值;
结构ListNode*next;
};
ListNode*头;
公众:
//这是构造函数。
整数列表()
{head=NULL;}
//析构函数
~IntegerList();
空节点(T);
void-insertNode(T);
无效节点(T);
无效搜索节点(T);
无效节点(T);
void displayList()常量;
};
迄今为止的职能:

//==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;
    }
}
/==追加节点定义==
模板
void IntegerList::appendNode(T newValue){
ListNode*newNode;
ListNode*nodePtr;
newNode=新的ListNode;
newNode->value=newValue;
newNode->next=NULL;
如果(!head)head=newNode;
否则{
nodePtr=头部;
而(nodePtr->next)nodePtr=nodePtr->next;
nodePtr->next=newNode;
}
}
//==显示列表定义==
模板
void IntegerList::displayList()常量{
ListNode*nodePtr;
nodePtr=头部;
while(nodePtr){
其次是价值观;
}
cout值=newValue;
如果(!头){
头=新节点;
newNode->next=NULL;
}
否则{
nodePtr=头部;
previousNode=NULL;
while(nodePtr!=NULL&&nodePtr->valuenext;
}
if(previousNode==NULL){
头=新节点;
newNode->next=nodePtr;
}
否则{
上一个节点->下一个=新节点;
newNode->next=nodePtr;
}
}
}
//==删除节点定义==
模板
void IntegerList::deleteNode(T searchValue){
ListNode*nodePtr;
ListNode*previousNode=NULL;
如果(!head)返回;
如果(头->值==搜索值){
nodePtr=head->next;
删除标题;
head=nodePtr;
}
否则{
nodePtr=头部;
while(nodePtr!=NULL&&nodePtr->value!=searchValue){
previousNode=nodePtr;
nodePtr=nodePtr->next;
}
if(nodePtr){
previousNode->next=nodePtr->next;
删除nodePtr;
}
}
}
//搜索节点定义
模板
void IntegerList::searchNode(T searchValue)
{
ListNode*nodePtr=0;
nodePtr=头部;
int i=搜索值;
//启动此变量是为了记住要搜索的数字。用于if语句。
int j=0;
//此变量专用于位置编号,从0开始。当while循环循环时,增量为1。
while(nodePtr)
{
如果(i==nodePtr->value){
//如果找到位置号,则此if语句将返回一条带有位置号的成功消息。

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

分析-使用现有的
类整数列表
,使用
整数列表列表2(列表1)要复制的
将仅在数据级别执行。该类仅包含一个指针数据
ListNode*head;
,默认的复制构造函数将
list1->head
复制到
list2->head
中。 结果是:

  • appendNode()
    insertNode()
    list1
    一起使用,会将相同的节点添加到
    list2
  • deleteNode()
    list1
    一起使用将把同一节点删除到
    list2
  • displayList()
    list2
    一起使用与
    list1
    相同
  • 解决方案-要将
    列表1
    复制到
    列表2
    中,并允许独立管理这两个列表,需要添加自己的复制构造函数

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

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

  • 开始复制之前,目标
    ListNode*head;
    被初始化为
    NULL
  • 源代码列表从
    src.head
    一直探索到
    NULL
    ,就像在
    displayList()函数中一样
  • 使用
    appendNode()
    ,将每个源节点添加到目标列表中
  • 建议的复制构造函数:

    template<class T>
    IntegerList<T>::IntegerList(IntegerList& src) : head(NULL) {
        ListNode *nodePtr;
        nodePtr = src.head;
    
        while (nodePtr) {
            appendNode(nodePtr->value);
            nodePtr = nodePtr->next;
        }
    }
    
    模板
    IntegerList::IntegerList(IntegerList&src):head(NULL){
    ListNode*nodePtr;
    nodePtr=src.head;
    while(nodePtr){
    appendNode(nodePtr->value);
    nodePtr=nodePtr->next;
    }
    }
    
    main()
    函数中不需要更改
    整数列表列表2(列表1);
    将自动调用特定的 复制构造函数而不是默认构造函数


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

    分析-使用现有的
    类整数列表
    ,使用
    整数列表列表2(列表1)要复制的
    将仅在数据级别执行。该类仅包含一个指针数据
    ListNode*head;
    ,默认的复制构造函数将
    list1->head
    复制到
    list2->head
    中。 结果是:

  • 使用
    appendNode()
    insertNode()
    list1
    将相同的节点添加到
    lis中
    
    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;
    }
    
    IntegerList(IntegerList& src);
    
    template<class T>
    IntegerList<T>::IntegerList(IntegerList& src) : head(NULL) {
        ListNode *nodePtr;
        nodePtr = src.head;
    
        while (nodePtr) {
            appendNode(nodePtr->value);
            nodePtr = nodePtr->next;
        }
    }