Definición de Clase DobNode
#ifndef TnDOBNODE
#define TnDOBNODE
#include <iostream>
template <class Tn>
class DobNode{
public:
Tn element;
DobNode<Tn>* next;
DobNode<Tn>* prev;
DobNode(Tn pElement, DobNode<Tn>* pNext = NULL, DobNode<Tn>* pPrev = NULL){
element = pElement;
next = pNext;
prev = pPrev;
}
DobNode(DobNode<Tn>* pNext = NULL, DobNode<Tn>* pPrev = NULL){
next = pNext;
prev = pPrev;
}
};
#endif
Definición de Clase Cola
#ifndef ECOLA
#define ECOLA
#include <iostream>
#include "DobNode.h"
using namespace std;
template <class E>
class Cola{
public:
DobNode<E> *front;
DobNode<E> *rear;
int tamano;
Cola(){
front = rear = NULL;
}
~Cola() { clear(); }
void clear(){
while (front!=NULL){
DobNode<E> *temp = front;
front = front->prev;
delete temp;
delete front;
}
}
void encolar(E elem){
DobNode<E> *temp = new DobNode<E>(elem,rear);
rear = temp;
}
E desencolar(){
E elem = front->element;
DobNode<E> *temp = front;
front = front->prev;
delete temp;
tamano--;
return elem;
}
E frente(){
return front->element;
}
bool vacia(){
return (rear==NULL);
}
};
#endif
Main
#include "Cola.h"
#include <iostream>
using namespace std;
int main(){
Cola<int> ColaInt;
cout << "Cola Mundo\n";
ColaInt.encolar(4);
ColaInt.encolar(6);
ColaInt.encolar(7);
ColaInt.encolar(9);
ColaInt.encolar(1);
ColaInt.desencolar();
return 0;
}
El código es diseñado en c++ Y compilado con g++
Me genera Violación de segmento al desencolar el elemento