简体   繁体   English

使用矢量 C++ 实现队列

[英]queue implementation with vector c++

We were instructed to implement a class that uses a vector to store a queue.我们被指示实现一个使用向量来存储队列的类。 I came up with the following but it's not really working.我想出了以下内容,但它并没有真正起作用。 Can anyone tell me what went wrong?谁能告诉我出了什么问题?

The values of the numbers are correctly pushed into the vec, and the first pop() works.数字的值被正确地推入 vec,第一个 pop() 起作用。 But if I check head->getElement(), it gives a strange number.但是如果我检查 head->getElement(),它会给出一个奇怪的数字。 Subsequent calls to pop() also fail.对 pop() 的后续调用也会失败。

#include <iostream>
#include <vector>
using namespace std;

template<class T>
class node{
    T element;
    node* next;
public:
    node(): next(nullptr){};
    T getElement() {return element;}
    void setElement(T newElement) {element=newElement;}
    node* getNext() {return next;}
    void setNext(node* newNext) {next = newNext;}
};

template<class T>
class queue{
    vector<node<T>> vec;
    node<T>* head;
    int size;
public:
    queue(): head(nullptr){}
    void push(node<T> newNode);
    node<T> pop();
    int getSize() {return unsigned(vec.size());}
    vector<T> getVec()const {return vec;}
    node<T>* getHead() {return head;}
    void setHead(node<T>* newHead) {head = newHead;}
    vector<node<T>> getVec() {return vec;}
};

int main() {
    queue<int> v;

    for (int i=0;i<5;i++){
        node<int>* newNode = new node<int>;
        newNode->setElement(i);
        v.push(*newNode);
    }
    cout<<"The elements in the vector are initially:\n";
    for (int i=0; i<v.getSize();i++)
        cout<<v.getVec()[i].getElement()<<" ";
    cout<<"\nAfter popping, the popped element is "<<v.pop().getElement()<<endl;
}

template<class T>
node<T> queue<T>:: pop(){
    node<T>* tmp = new node<T>;
    tmp->setNext(head->getNext());
    head=head->getNext();
    return *tmp;
}

template<class T>
void queue<T>:: push(node<T> newNode){
    if (head==nullptr){
        node<T>* newPtr = new node<T>;
        newPtr = &newNode;
        newPtr->setNext(head);
        head=newPtr;
    }
    else{
        node<T>* newPtr = new node<T>;
        newPtr->setElement(newNode.getElement());
        node<T>* end = head;
        while (end->getNext() != nullptr)
            end->setNext(end->getNext());
        end->setNext(newPtr);
    }
    vec.push_back(newNode);
}

There are many issues with your code.你的代码有很多问题。 First, lets fix pop.首先,让我们修复流行音乐。 You are creating a new node tmp, setting the next and returning the same without setting the element.您正在创建一个新节点 tmp,设置下一个并在不设置元素的情况下返回相同的节点。 To fix this, you just need it to set it to head and move the head to its next.要解决此问题,您只需要将其设置为头部并将头部移动到下一个。

template<class T>
node<T> queue<T>:: pop(){
node<T>* tmp = head;// = new node<T>;
//tmp->setNext(head->getNext());
head=head->getNext();
return *tmp;
}

After this you will get your element with pop.在此之后,您将获得带有 pop 的元素。 But you will get wrong element.但是你会得到错误的元素。 As queue is FIFO based, you should get '0' at first pop but in your case you will not get 0. Because, your push function is also incorrect.由于队列是基于 FIFO 的,你应该在第一次弹出时得到 '0',但在你的情况下你不会得到 0。因为,你的推送功能也不正确。 In push, when you are pushing the first element, then you are taking the address of the node object passed which is passed by value, this leads to undefined behavior, since the node object passed would be destroyed once the function finishes.在推送中,当您推送第一个元素时,您将获取通过值传递的节点对象的地址,这会导致未定义的行为,因为一旦函数完成,传递的节点对象将被销毁。 Also, in the else part of your push you are setting the next of end to its own next and hence it will go into infinite loop.此外,在您推送的其他部分中,您将下一个结束设置为自己的下一个,因此它将进入无限循环。 Below is the corrected implementation.下面是更正后的实现。

template<class T>
void queue<T>:: push(node<T> newNode){
if (head==nullptr){
    node<T>* newPtr = new node<T>;
    newPtr->setElement(newNode.getElement());
    //newPtr = &newNode;
    //newPtr->setNext(head);
    head=newPtr;
}
else{
    node<T>* newPtr = new node<T>;
    newPtr->setElement(newNode.getElement());
    node<T>* end = head;
    while (end->getNext() != nullptr)
        end = end->getNext();
    end->setNext(newPtr);
}
vec.push_back(newNode);
}

Lastly, there are many memory leaks in your code.最后,您的代码中存在许多内存泄漏。 You are creating a new node far more number of times than needed and also not deleting them.您正在创建一个新节点的次数远远超过需要的次数,并且也没有删除它们。 As, you can see now that in case of push, you just need to pass the element T and it would suffice.因为,您现在可以看到,在推送的情况下,您只需要传递元素 T 就足够了。 You don't need to pass a new node every time.您不需要每次都传递一个新节点。 Also, try using smart pointers as it will manage a lot of things on its own.此外,尝试使用智能指针,因为它会自行管理很多事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM