对数组进行排序会使指针 link 错误
Sorting an array makes pointers link wrongly
所以我正在对具有一些整数和指针的对象执行排序算法。每当我进行排序时,指针开始表现奇怪并链接到错误的索引。
这是我的代码
#include<iostream>
#include<stdio.h>
using namespace std;
class person
{
public:
int knots, time, ID;
int untie, status = 1;
person *next = NULL;
};
///DEBUG
void print(person *a, int p)
{
for (int i = 0; i < p; i++)
{
cout<<a[i].ID<<": "<<a[i].knots<<" "<<a[i].time<<" "<<a[i].untie<<" "<<a[i].status<<" Next: "<<a[i].next->ID<<endl;
}
cout<<endl;
}
int main()
{
int p;
scanf("%d\n", &p);
while (p != -1)
{
person *people = new person[p];
for (int i = 0; i < p; i++)
{
people[i].ID = i+1;
scanf("%d ", &people[i].knots);
if (i)
{
people[i-1].next = &people[i];
}
}
people[p-1].next = &people[0];
scanf("/n");
for (int i = 0; i < p; i++)
{
scanf("%d ", &people[i].time);
if (i)
{
people[i-1].untie = people[i-1].time * people[i].knots;
}
}
people[p-1].untie = people[p-1].time * people[0].knots;
print(people, p);
///SORTING
for (int i = 0; i < p-1; i++)
{
for (int j = i; j < p; j++)
{
if (people[j].untie < people[i].untie)
{
person x = people[j];
people[j] = people[i];
people[i] = x;
}
}
}
print(people, p);
for (int i = 0; i < p; i++)
{
if (people[i].status)
{
if (people[i].next->untie == people[i].untie)
{
people[i].next->status = 2;
}
else
people[i].next->status = 0;
if (people[i].status == 2)
people[i].status = 0;
}
}
print(people, p);
}
}
排序工作正常,但问题是当我链接时 "next" 它链接到错误的对象。我使用链接的原因是即使在排序后也能保持顺序。
person x = people[j];
people[j] = people[i];
people[i] = x;
这是因为您交换了上面排序代码中的值,而struct
中记录的地址person *next
没有改变。
因此旧地址还在struct person
中,而那个地址中的内容在交换时已经被修改,导致不一致。
所以我正在对具有一些整数和指针的对象执行排序算法。每当我进行排序时,指针开始表现奇怪并链接到错误的索引。
这是我的代码
#include<iostream>
#include<stdio.h>
using namespace std;
class person
{
public:
int knots, time, ID;
int untie, status = 1;
person *next = NULL;
};
///DEBUG
void print(person *a, int p)
{
for (int i = 0; i < p; i++)
{
cout<<a[i].ID<<": "<<a[i].knots<<" "<<a[i].time<<" "<<a[i].untie<<" "<<a[i].status<<" Next: "<<a[i].next->ID<<endl;
}
cout<<endl;
}
int main()
{
int p;
scanf("%d\n", &p);
while (p != -1)
{
person *people = new person[p];
for (int i = 0; i < p; i++)
{
people[i].ID = i+1;
scanf("%d ", &people[i].knots);
if (i)
{
people[i-1].next = &people[i];
}
}
people[p-1].next = &people[0];
scanf("/n");
for (int i = 0; i < p; i++)
{
scanf("%d ", &people[i].time);
if (i)
{
people[i-1].untie = people[i-1].time * people[i].knots;
}
}
people[p-1].untie = people[p-1].time * people[0].knots;
print(people, p);
///SORTING
for (int i = 0; i < p-1; i++)
{
for (int j = i; j < p; j++)
{
if (people[j].untie < people[i].untie)
{
person x = people[j];
people[j] = people[i];
people[i] = x;
}
}
}
print(people, p);
for (int i = 0; i < p; i++)
{
if (people[i].status)
{
if (people[i].next->untie == people[i].untie)
{
people[i].next->status = 2;
}
else
people[i].next->status = 0;
if (people[i].status == 2)
people[i].status = 0;
}
}
print(people, p);
}
}
排序工作正常,但问题是当我链接时 "next" 它链接到错误的对象。我使用链接的原因是即使在排序后也能保持顺序。
person x = people[j];
people[j] = people[i];
people[i] = x;
这是因为您交换了上面排序代码中的值,而struct
中记录的地址person *next
没有改变。
因此旧地址还在struct person
中,而那个地址中的内容在交换时已经被修改,导致不一致。