【问题标题】:Data insertion and deletion speed of queue in STL of c++c++ STL中队列的数据插入和删除速度
【发布时间】:2021-01-06 06:04:40
【问题描述】:

我对 C++ STL 中队列的插入和删除速度有疑问。

当我尝试使用我提出的 Dequeue 解决算法问题时,我面临着超时问题。

所以我认为我的 Dequeue 太慢了,我想知道我的 Dequeue 和 c++ STL 中的队列有什么区别。

这是我的队列代码。

请给我一些建议。

在这段代码中,我想Node 类中的value 不能有负数。

class Node
{
private:
    int value;

    Node* prev;
    Node* next;

public:
    Node();
    Node(int value);
    Node(int value, Node* next);
    ~Node();

    void setValue(int value);
    void setPrev(Node* prev);
    void setNext(Node* next);

    int getValue();
    Node* getPrev();
    Node* getNext();
};

Node::Node()
    : value(-1), prev(nullptr), next(nullptr)
{
}

Node::Node(int value)
    : value(value), prev(nullptr), next(nullptr)
{
}

Node::Node(int value, Node* next)
    : value(value), prev(nullptr), next(next)
{
}

Node::~Node()
{
}

void Node::setValue(int value)
{
    this->value = value;
}

void Node::setPrev(Node* prev)
{
    this->prev = prev;
}

void Node::setNext(Node* next)
{
    this->next = next;
}

int Node::getValue()
{
    return this->value;
}

Node* Node::getPrev()
{
    return this->prev;
}

Node* Node::getNext()
{
    return this->next;
}

class Dequeue
{
private:
    Node* front;
    Node* back;

public:
    Dequeue();
    ~Dequeue();

    void pushFront(int value);
    void pushBack(int value);

    void popFront();
    void popBack();

    int getFront();
    int getBack();
    int getSum();
};

Dequeue::Dequeue()
{
    this->back = new Node(-1, nullptr);
    this->front = new Node(-1, this->back);

    this->back->setPrev(front);
}

Dequeue::~Dequeue()
{
}

void Dequeue::pushFront(int value)
{
    Node* node = new Node(value, this->front->getNext());
    node->setPrev(this->front);
    
    this->front->setNext(node);
    node->getNext()->setPrev(node);
}

void Dequeue::pushBack(int value)
{
    Node* node = new Node(value, this->back);
    node->setPrev(this->back->getPrev());

    this->back->setPrev(node);
    node->getPrev()->setNext(node);
}

void Dequeue::popFront()
{
    if (this->front->getNext() == this->back)
        return;

    Node* node = this->front->getNext();
    
    this->front->setNext(node->getNext());
    node->getNext()->setPrev(this->front);

    node->setNext(nullptr);
    node->setPrev(nullptr);
    delete node;
}

void Dequeue::popBack()
{
    if (this->back->getPrev() == this->front)
        return;

    Node* node = this->back->getPrev();

    this->back->setPrev(node->getPrev());
    node->getPrev()->setNext(this->back);

    node->setNext(nullptr);
    node->setPrev(nullptr);
    delete node;
}

int Dequeue::getFront()
{
    if (this->front->getNext() == this->back)
        return -1;

    return this->front->getNext()->getValue();
}

int Dequeue::getBack()
{
    if (this->back->getPrev() == this->front)
        return -1;

    return this->back->getPrev()->getValue();
}

【问题讨论】:

    标签: c++ stl queue


    【解决方案1】:

    You Dequeue 是通过链表实现的,其中节点在每次推送/弹出操作中分配/解除分配。 std::queue 是通过 std::deque 实现的,效率更高(它只分配一次)。

    如果您需要在中间插入,链表很好,但这不是您的情况。 std::deque 基本上是一个固定大小数组的动态序列。

    相关问题:

    【讨论】:

    • 感谢您的友好解释!你帮了大忙。
    猜你喜欢
    • 2013-08-27
    • 2010-12-15
    • 1970-01-01
    • 2017-07-17
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    相关资源
    最近更新 更多