C++ 实现链表的创建,插入,删除,查找最大的k个值和链表的合并

class ListNode {
    friend class MergeListNode;
public:
    ListNode()=default;
    ListNode(int val) :data(val), next(nullptr) {}
    void insertNode(ListNode * node, int pos);
    void deleteNode(int pos);
    ListNode * initialList();
    int getsize();
    void insertBack(ListNode *node);
    void showList();
    void ListNodeSort();
    void ListNodeTopK();
private:
    ListNode *next;
    int data;
};
int  ListNode::getsize() {
    int count = 0;
    ListNode *tar = this;
    while (tar->next) { tar = tar->next; count++; }
    return count;

}
void ListNode::insertNode(ListNode *node, int pos) {
    if (pos > (this->getsize())) { throw std::out_of_range("out of range"); }
    int count = 0;
    ListNode *tarl = this;
    while (count<pos) {
        tarl = tarl->next;
        count++;
    }
    ListNode * temp = tarl->next->next;;
    tarl->next = node;
    node->next = temp;
}
void ListNode::deleteNode(int pos) {
    if (pos > this->getsize()) { throw std::out_of_range("out of range"); }
    int count = 0;
    ListNode * tarl = this;
    while (count < pos-1) {
        tarl = tarl->next;
        count++;
    }
    ListNode *temp = tarl->next->next;
    ListNode *delp = tarl->next;
    tarl->next = temp;
    delete delp;
}
void ListNode::showList() {
    ListNode *temp = this;
    while (temp->next) {
        std::cout << temp->next->data << "->";
        temp = temp->next;
    }
}
ListNode * ListNode::initialList() {
    bool flage = true;
    ListNode * temp = this;
    while (flage) {
        std::cin >> data;
        if (data == -1) { flage = false; return this; }
        ListNode *newNode = new ListNode(data);
        temp->next = newNode;
        temp = newNode;
    }
    return  this;
}
void ListNode::insertBack(ListNode *node) {
    ListNode *tar = this;
    while (tar->next != nullptr) {
        tar = tar->next;
    }
    tar->next = node;
}
void ListNode::ListNodeSort() {
    std::vector<ListNode *> vec;
    ListNode * tarl = this;
    while (tarl->next) {
        tarl = tarl->next;
        vec.push_back(tarl);
    }
    auto comp = [](ListNode *node1, ListNode *node2) {return (node1->data) > (node2->data); };
    std::sort(vec.begin(), vec.end(), comp);
    ListNode *tar = this;
    while (!vec.empty()) {
        auto temp = vec.back();
        tar->next = temp;
        vec.pop_back();
        tar = temp;
    }
    tar->next = nullptr;
    
}
void ListNode::ListNodeTopK() {
    auto compare = [](ListNode* node1, ListNode* node2) {return node1->data > node2->data; };
    std::priority_queue<ListNode*, std::vector<ListNode*>, decltype(compare)> pro_q(compare);
    ListNode *iterator = this;
    while (iterator->next) {
        iterator = iterator->next;
        if (pro_q.size() < 3)
            pro_q.push(iterator);

        else {
            pro_q.pop();
            pro_q.push(iterator);
        }
    }
    while (!pro_q.empty()) {
        std::cout << pro_q.top()->data << std::endl;
        pro_q.pop();
    }
}
class MergeListNode {
public:
    std::vector<int> static MergListNode(std::vector<ListNode*>);
};
std::vector<int> MergeListNode::MergListNode(std::vector<ListNode*> vec) {
    std::vector<int> res;
    auto compare = [](ListNode *node1, ListNode *node2) { return node1->data > node2->data; };
    std::priority_queue<ListNode *, std::vector<ListNode *>, decltype(compare)> pro_q(compare);
    for (auto ite = vec.begin(); ite != vec.end(); ite++) {
        if ((*ite)->next == nullptr) { std::cout << "合并的链表有一个为空" << std::endl; break; }
        else pro_q.push((*ite)->next);
    }
    while (!pro_q.empty()) {
        if (pro_q.size() < vec.size()){
            if (pro_q.top()->next) { pro_q.push(pro_q.top()->next); res.push_back(pro_q.top()->data); pro_q.pop(); }
            
            else { res.push_back(pro_q.top()->data); pro_q.pop(); }
        }
        else {
            auto temp = pro_q.top();
            res.push_back(temp->data);
            pro_q.pop();
            if (temp->next) { pro_q.push(temp->next);
            }
        }
    }
    return res;

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值