PAT B1075 链表元素分类

纯纯的模拟题目的描述,没有别人写的简练。

#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;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容