问题严重!
#include<iostream>
#include<string>
#include<vector>
#include<memory>
using namespace std;
class list_creat
{
public:
list_creat(istream &in) { in >> address >> data >> next; }
string& get_address() { return address; }
long long& get_data() { return data; }
string& get_next() { return next; }
private:
long long data = 0;
string address;
string next;
};
class list_result
{
public:
list_result(vector<list_creat> &l) :list_info(l) {};
void sort(string address, unsigned n, unsigned k) { list_default(address); sort_by_format(n, k); };
friend ostream &operator<<(ostream &os,const list_result &rhs);
private:
vector<list_creat> list_info;
void sort_by_format(unsigned n,unsigned k);
void list_default(string cur_add);
};
void list_result::list_default(string cur_add)
{
vector<list_creat> tmp;
while (true)
{
for (auto &r : list_info)
{
if (cur_add == r.get_address())
{
tmp.push_back(r);
cur_add = r.get_next();
break;
}
}
if (cur_add == "-1")
break;
}
*this = tmp;
}
void list_result::sort_by_format(unsigned n,unsigned k)
{
unsigned count = n / k;
for (unsigned i = 0; i < count; ++i)
{
for (unsigned j = 0; j < k/2; ++j)
{
auto tmp = list_info[j + i*k];
list_info[j + i*k] = list_info[i*k + k - j - 1];
list_info[i*k + k - j - 1] = tmp;
}
}
for (unsigned i=0;i<n-1;i++)
{
list_info[i].get_next() = list_info[i + 1].get_address();
}
}
ostream &operator<<(ostream &os,const list_result &rhs)
{
unsigned n = rhs.list_info.size();
for (unsigned i = 0; i < n; ++i)
{
list_creat tmp = rhs.list_info.at(i);
if (i != n - 1)
{
os << tmp.get_address() << " " << tmp.get_data() << " " << tmp.get_next() << endl;
}
else
{
os << tmp.get_address() << " " << tmp.get_data() << " " << tmp.get_next();
}
}
return os;
}
int main()
{
string first_add;
unsigned n = 0;
unsigned k = 0;
cin >> first_add >> n >> k;
vector<list_creat> in_list;
for (unsigned i = 0; i < n; ++i)
{
in_list.push_back(list_creat(cin));
}
list_result result(in_list);
result.sort(first_add, n, k);
cout << result;
cout << endl;
system("pause");
return 0;
}