分段错误:11 错误



im 试图实现带有虚拟节点(centinels(的取消排队。当我尝试编译时,我收到分段错误 11 错误。这是我的代码:(非常感谢!!

节点.h:

template <class Element>
class Node {
public:
    Node(const Node& orig);
    Node(Element e);
    const Element& getElement();
    Node<Element>* getNext() const; 
    void setNext(Node<Element> *next);
    Node<Element>* getPrevious() const;
    void setPrevious(Node<Element> *prev);
    ~Node();
private:
    Node <Element>* next;
    Node <Element>* prev;
    Element element; 

};
template <class Element>
Node<Element>::Node(Element e){
    this->element = e;
    this->next = nullptr; 
    this->prev= nullptr;
}
template <class Element>
Node<Element>::~Node(){
}
template <class Element>
const Element& Node<Element>::getElement(){
    return this->element; 
}
template <class Element>
Node<Element>* Node<Element>::getNext() const{
    return this->next;
}
template <class Element>
Node<Element>* Node<Element>::getPrevious() const{
    return this->prev;
}
template <class Element>
void Node<Element>::setNext(Node<Element>* next){
    this->next = next; 
}
template <class Element>
void Node<Element>::setPrevious(Node<Element>* prev){
    this->prev = prev; 
}

LinkedDeque.h:

#ifndef LINKEDDEQUE_H
#define LINKEDDEQUE_H
#include "Node.h"
#include <iostream>
using namespace std;
template <class Element>
class LinkedDeque {
public:
     LinkedDeque();
    ~LinkedDeque();
    LinkedDeque(const LinkedDeque<Element>& deque);
    bool isEmpty()const;
    void insertFront(const Element & element);
    void insertRear(const Element & element);
    void deleteFront();
    void deleteRear();
    void print();
    const Element& getFront()const;
    const Element& getRear()const;
private:
    int num_elements; 
    Node<Element> *_front; 
    Node<Element> *_rear; 
    Node<Element> *frontSentinel;
    Node<Element> *rearSentinel;
};
template <class Element>
LinkedDeque<Element>::LinkedDeque(){
    //creamos nodos vacios
    _front->setNext(frontSentinel);
    _rear->setPrevious(rearSentinel); //**************
    int num_elements = 0; 
    cout << "Holi" << endl;
}
//cal implementar el constructor copia
template <class Element>
LinkedDeque<Element>::LinkedDeque(const LinkedDeque<Element>& deque){
}
template <class Element>
LinkedDeque<Element>::~LinkedDeque() {
}
template <class Element>
bool LinkedDeque<Element>::isEmpty()const{
    return (this->frontSentinel->getNext()==nullptr);   
}
template <class Element>
void LinkedDeque<Element>::insertFront(const Element & element){
    Node<Element> *new_node = new Node<Element> (element); 
    if(isEmpty()){
        rearSentinel->setPrevious(new_node);
        frontSentinel->setNext(new_node); 
    }else{
        Node<Element> *aux;
        aux = frontSentinel->getNext();
        frontSentinel->setNext(new_node);
        aux->setPrevious(frontSentinel->getNext());
        delete aux;
    }
    num_elements++;
}
template <class Element>
void LinkedDeque<Element>::insertRear(const Element & element){
    Node<Element> *new_node = new Node<Element> (element); 
    if(isEmpty()){
        rearSentinel->setPrevious(new_node);
        frontSentinel->setNext(new_node);
    }else{
        Node<Element> *aux;
        aux = rearSentinel->getPrevious();
        rearSentinel->setPrevious(new_node);
        aux->setNext(rearSentinel->getPrevious());
        delete aux;
    }
    num_elements++;
}
template <class Element>
void LinkedDeque<Element>::deleteFront(){
    if(!isEmpty()){
        if(num_elements == 1){
            delete frontSentinel->getNext();
            frontSentinel->setNext(rearSentinel);
            rearSentinel->setPrevious(frontSentinel);
        }else {
            Node<Element> *aux;
            aux = frontSentinel->getNext();
            aux->getNext()->setPrevious(frontSentinel);
            frontSentinel->setNext(aux->getNext());
            delete aux;
        }
        num_elements--;
    }
}
template <class Element>
void LinkedDeque<Element>::deleteRear(){
    if(!isEmpty()){
        if(num_elements == 1){
            delete rearSentinel->getPrevious();
            rearSentinel->setPrevious(nullptr);
            frontSentinel->getPrevious(nullptr);
        }else{
            Node<Element> *aux;
            aux = rearSentinel->getPrevious();
            aux->getPrevious()->setNext(rearSentinel);
            rearSentinel->setPrevious(aux->getPrevious());
            delete aux;
        }
        num_elements--;
    }
}
template <class Element>
void LinkedDeque<Element>::print(){
    if(!isEmpty()){
        Node<Element> *printController = frontSentinel->getNext();
        while( printController->getNext()!= nullptr){
            cout<< printController->getElement() << endl;
            printController = printController->getNext();
        }
    }
}
template <class Element>
const Element& LinkedDeque<Element>::getFront()const{
    return frontSentinel->getNext()->getElement();
}
template <class Element>
const Element& LinkedDeque<Element>::getRear()const{
    return rearSentinel->getPrevious()->getElement();
}
#endif /* LINKEDDEQUE_H */

最后,主要.cpp:

#include <cstdlib>
#include"Node.h"
#include"LinkedDeque.h"
using namespace std;
/*
 * 
 */
int main(int argc, char** argv) {
    LinkedDeque<int> *deque= new LinkedDeque<int>;
    deque->insertFront(4);
    deque->insertFront(5);
    deque->insertRear(10);
    deque->deleteFront();
    deque->print();
}

也许我不应该实现 Node 的析构函数并为其虚拟化?或者指针声明存在一些错误?

我猜你的 LinkedDeque 构造函数模板正在访问无效的_front,_rear指针

template <class Element>
LinkedDeque<Element>::LinkedDeque(){
    //creamos nodos vacios
    _front->setNext(frontSentinel); //*_front has not been initialized and contains undefined values*
    _rear->setPrevious(rearSentinel); //**************
    int num_elements = 0; 
    cout << "Holi" << endl;
}
您可以使用

new Node<Element>(NULL);构造哨兵,这样您的LinkedDeque就有Node<Element>对象用作哨兵而不是nullptr。从以下方法中的这些更改开始:

template <class Element>
LinkedDeque<Element>::LinkedDeque(){
    //creamos nodos vacios
    frontSentinel = new Node<Element>(NULL);
    rearSentinel  = new Node<Element>(NULL);
    rearSentinel->setPrevious(frontSentinel);
    frontSentinel->setNext(rearSentinel);
    int num_elements = 0;
    cout << "Holi" << endl;
}
template <class Element>
bool LinkedDeque<Element>::isEmpty()const{
    return this->frontSentinel->getNext()->getElement()==NULL;
}
template <class Element>
void LinkedDeque<Element>::insertFront(const Element & element){
    Node<Element> *new_node = new Node<Element>(element); 
    new_node->setNext(frontSentinel->getNext());
    frontSentinel->getNext()->setPrevious(new_node);
    new_node->setPrevious(frontSentinel);
    frontSentinel->setNext(new_node);
    num_elements++;
}
template <class Element>
void LinkedDeque<Element>::insertRear(const Element & element){
    Node<Element> *new_node = new Node<Element> (element); 
    new_node->setPrevious(rearSentinel->getPrevious());
    rearSentinel->getPrevious()->setNext(new_node);
    new_node->setNext(rearSentinel);
    rearSentinel->setPrevious(new_node);
    num_elements++;
}
template <class Element>
void LinkedDeque<Element>::deleteFront(){
    Node<Element> *aux = frontSentinel->getNext();
    aux->getNext()->setPrevious(frontSentinel);
    frontSentinel->setNext(aux->getNext());
    delete aux;
    num_elements--;
}
template <class Element>
void LinkedDeque<Element>::deleteRear(){
    Node<Element> *aux = rearSentinel->getPrevious();
    aux->getPrevious()->setNext(rearSentinel);
    rearSentinel->setPrevious(aux->getPrevious());
    delete aux;
    num_elements--;
}
template <class Element>
void LinkedDeque<Element>::print(){
    Node<Element> *printController = frontSentinel->getNext();
    while (printController->getElement() != NULL) {
        cout << printController->getElement() << endl;
        printController = printController->getNext();
    }
}

相关内容

  • 没有找到相关文章

最新更新