segmentation fault: 11 Error

im trying to implement a Dequeue with dummy nodes (centinels). When i'm trying to compile, i get a segmentation fault 11 Error. This is my code:(Thank you so much!!)

Node.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 */

and finally, main.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();

}

Maybe I should don't implement the destructor of Node and go virtual for it? Or there's some error with the pointer declarations?

1 answer

  • answered 2018-04-14 14:32 pasha

    I guess your LinkedDeque constructor template is accessing invalid _front, _rear pointers

    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;
    
    }