C++ 将列表元素移动到列表末尾的函数多次无效

C++ 将列表元素移动到列表末尾的函数多次无效,c++,oop,linked-list,nodes,singly-linked-list,C++,Oop,Linked List,Nodes,Singly Linked List,我需要做一个函数,将单链表中的第n个元素移动到列表的末尾。我创建了一些这样做的代码,但是如果我再次尝试这样做,它只工作一次。它将选定的元素移动到末尾,但先前移动的元素将被删除/消失。我的理论是,它实际上并没有改变尾部引用。所以我现在被卡住了 void move(int n) { if (head == NULL || head->next == NULL) { return; } node *f

我需要做一个函数,将单链表中的第n个元素移动到列表的末尾。我创建了一些这样做的代码,但是如果我再次尝试这样做,它只工作一次。它将选定的元素移动到末尾,但先前移动的元素将被删除/消失。我的理论是,它实际上并没有改变尾部引用。所以我现在被卡住了

void move(int n)
    {
        if (head == NULL || head->next == NULL)
        {
            return;
        }
        node *first = head;
        node *temp =new node;

        for (int i = 1; i < n-1; i++)
        {
            first=first->next;
        }
        temp = first->next;
        first->next=first->next->next;
        temp->next = NULL;

        tail->next = temp;
        tail=temp;
    }
void移动(int n)
{
if(head==NULL | | head->next==NULL)
{
返回;
}
节点*第一个=头部;
node*temp=新节点;
对于(int i=1;i下一个;
}
温度=第一个->下一个;
第一个->下一个=第一个->下一个->下一个;
temp->next=NULL;
尾部->下一步=温度;
尾=温度;
}
我的意见: 123445

第一次移动第三个元件后:

1 2 4 5 3

第二次移动第三个元件(4)后:

1 2 5 4

但应该是这样


1 2 5 3 4

我用自己的实现检查了您的代码。函数move()工作正常。但是,您不应该在第8行代码中使用@molbdnilo和@PaulMakenzie所强调的“new”。但它并不对这个问题负责。代码的其他部分有问题

#include<iostream>
using namespace std;

class List
{
    struct Node
    {
        int number;
        Node* next;
    };  

    Node* head;
    Node* tail;

    public:
    List()
    {
        head = NULL;
        tail = NULL;
    }
        void insert(int num)
    {
        Node* temp = new Node();
        temp->number = num;
        temp->next = NULL;

        if (head == NULL)
        {
            head = temp;
            tail = temp;
        }
        else
        {
            Node* point = head;
            while (point->next != NULL)
                point = point->next;

            point->next = temp;
            tail = point->next;
        }
    }
    void display()
    {
        Node* point = head;
        while (point != NULL)
        {
            cout << point->number << " ";
            point = point->next;
        }
    }

    void move(int n)
    {
        if (head == NULL || head->next == NULL)
        {
            return;
        }
        Node *first = head;
        Node *temp;

        for (int i = 1; i < n-1; i++)
        {
            first=first->next;
        }
        temp = first->next;
        first->next=first->next->next;
        temp->next = NULL;

        tail->next = temp;
        tail=temp;
    }
};

int main()
{
    List a;
    a.insert(1);
    a.insert(2);
    a.insert(3);
    a.insert(4);
    a.insert(5);

    a.move(3);
    a.move(3);

    a.display();

}


#包括
使用名称空间std;
班级名单
{
结构体类型
{
整数;
节点*下一步;
};  
节点*头;
节点*尾部;
公众:
列表()
{
head=NULL;
tail=NULL;
}
无效插入(整数)
{
Node*temp=新节点();
temp->number=num;
temp->next=NULL;
if(head==NULL)
{
压头=温度;
尾=温度;
}
其他的
{
节点*点=头部;
while(点->下一步!=NULL)
点=点->下一步;
点->下一步=温度;
尾=点->下一步;
}
}
无效显示()
{
节点*点=头部;
while(点!=NULL)
{
下一步是数数;
}
}
无效移动(整数n)
{
if(head==NULL | | head->next==NULL)
{
返回;
}
节点*第一个=头部;
节点*温度;
对于(int i=1;i下一个;
}
温度=第一个->下一个;
第一个->下一个=第一个->下一个->下一个;
temp->next=NULL;
尾部->下一步=温度;
尾=温度;
}
};
int main()
{
清单a;
a、 插入(1);
a、 插入(2);
a、 插入(3);
a、 插入(4);
a、 插入(5);
a、 移动(3);
a、 移动(3);
a、 显示();
}

是时候使用调试器并逐步检查代码了。“3”的消失应该会给你一个提示。通过查看您的代码当前的编写方式,思考一下为什么前一个尾部项会消失。如果要移动节点,则不应使用
new
。是的,内存泄漏,但这不是观察到的问题的原因…调试指针操作代码的最佳方法是在纸上绘制结构,并在脑海中跟踪代码时更新图形。只需对insert()进行注释,为什么要在else块中循环?为什么不“tail->next=temp;尾=温度`我解决了我遇到的问题。我将列表传递给一个使用move函数的函数,不知怎么搞砸了,但是当我将其更改为只执行a.move(n)而不将对象传递给其他函数时,它就工作了。