纯纯的模拟题目的描述,没有别人写的简练。
#include<iostream>
#include<queue>
#include<vector>
#include<map>
using namespace std;
struct Node {
string address;
int data;
string next;
};
vector<Node> totalList;
void Merge(queue<Node> list) {
while (!list.empty()) {
totalList.push_back(list.front());
list.pop();
}
}
int main() {
string address;
int n, k;
map<string, Node> list;
queue<Node> llist, mlist, rlist;//左中右
cin >> address >> n >> k;
for (int i = 0; i < n; i++) {
Node node;
string tempaddress;
int data;
string tempnext;
cin >> tempaddress >> data >> tempnext;
node.address = tempaddress;
node.data = data;
node.next = tempnext;
list[tempaddress] = node;
}
while (address!="-1") {
Node current = list[address];
address = list[address].next;
if (current.data < 0) {
llist.push(current);
}
else if (current.data >= 0 && current.data <= k) {
mlist.push(current);
}
else if (current.data > k) {
rlist.push(current);
}
}
Merge(llist);
Merge(mlist);
Merge(rlist);
for (int i = 0; i < totalList.size(); i++) {
string next;
if (i < totalList.size() - 1) {
next = totalList[i + 1].address;
}
else {
next = "-1";
}
cout << totalList[i].address << " " << totalList[i].data << " " << next << endl;
}
return 0;
}