函数将列表元素移动到列表末尾多次不起作用
Function moving list elements to the end of the list not working multiple times
我需要创建一个函数,将单向链表中的第 n 个元素移动到链表的末尾。我创建了一些代码来执行此操作,但如果我尝试再次执行它,它只会工作一次,它将选定的元素移动到末尾,但之前移动的元素得到 deleted/dissapears。我的理论是它实际上并没有改变尾部参考。所以我现在卡住了!
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;
}
我的输入:
1 2 3 4 5
第一次移动第三个元素后:
1 2 4 5 3
第二次移动第三个元素(4)后:
1 2 5 4
但应该是
1 2 5 3 4
我用自己的实现检查了你的代码。您的函数 move() 工作正常。但是,您不应该在第 8 行代码中使用 'new',正如@molbdnilo 和@PaulMakenzie 所强调的那样。但它不对这个问题负责。您的代码的其他部分存在问题。
#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();
}
我需要创建一个函数,将单向链表中的第 n 个元素移动到链表的末尾。我创建了一些代码来执行此操作,但如果我尝试再次执行它,它只会工作一次,它将选定的元素移动到末尾,但之前移动的元素得到 deleted/dissapears。我的理论是它实际上并没有改变尾部参考。所以我现在卡住了!
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;
}
我的输入: 1 2 3 4 5
第一次移动第三个元素后:
1 2 4 5 3
第二次移动第三个元素(4)后:
1 2 5 4
但应该是
1 2 5 3 4
我用自己的实现检查了你的代码。您的函数 move() 工作正常。但是,您不应该在第 8 行代码中使用 'new',正如@molbdnilo 和@PaulMakenzie 所强调的那样。但它不对这个问题负责。您的代码的其他部分存在问题。
#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();
}