list - C++ | Queue: Dequeue function crashes program -


i attempting make queue in c++ using double linked list. have not tested since stuck @ step dequeue. attempted create temp node, , move around stuff when call delete on head node in queue (called queue), , set head temp node next element, (you can see in code) when call delete, crashes, according ms visual studios 2013. add how weird is, following stack called, after delete called, setprev called , set prev node , crashes there. never call function during of destructors deletes do. try best understand answers still new c++ terminology. below code. oh 1 last thing, in main, did call enqueue once, dequeue once, delete

node class 

...

#ifndef tsdnode_h #define tsdnode_h  template <class t> class dnode {     private:         dnode<t>* next;         dnode<t>* prev;         t data;      public:         dnode(t);         void setnext(dnode<t>* next);         void setprev(dnode<t>* prev);         dnode<t>* getnext() const;         dnode<t>* getprev() const;         t getdata() const;         void setdata(t data);         ~dnode(); };  template <class t> dnode<t>::dnode(t data) {     this->next = nullptr;     this->data = data;     this->prev = nullptr; }  template <class t> void dnode<t>::setnext(dnode<t>* next) {     this->next = next; }  template <class t> void dnode<t>::setprev(dnode<t>* prev) {     this->prev = prev; }  template <class t> dnode<t>* dnode<t>::getnext() const {     return this->next; }  template <class t> dnode<t>* dnode<t>::getprev() const {     return this->prev; }  template <class t> t dnode<t>::getdata() const {     return this->data; }  template <class t> void dnode<t>::setdata(t data) {     this->data = data; }  template <class t> dnode<t>::~dnode() {     delete this->next;     delete this->prev;     this->next = nullptr;     this->prev = nullptr; }  #endif  /* tsdnode_h */ 

....

queue class 

....

#ifndef tsqueue_h #define tsqueue_h  #include "tsdnode.h" #include <string>  template <class t> class queue {     private:         dnode<t>* queue;         dnode<t>* tail;         int size;      public:         queue();         void enqueue(t data);         t dequeue();         ~queue(); };  template <class t> queue<t>::queue() {     this->queue = nullptr;     this->tail = this->queue;     size = 0; }   template <class t> void queue<t>::enqueue(t data) {     if (this->tail != null)     {         this->tail->setnext(new dnode<t>(data));         this->tail->getnext()->setprev(this->tail);         this->tail = this->tail->getnext();     }     else     {         this->queue = new dnode<t>(data);         this->tail = this->queue;     }     size++; }  template <class t> t queue<t>::dequeue() {     t data;      if (this->queue == nullptr)     {         delete this->tail;         delete this->queue;         this->tail = nullptr;         std::string ex = "exception: empty queue\n";         throw ex;     }     else if (this->queue != nullptr)     {         data = this->queue->getdata();         dnode<t>* node = this->queue->getnext();         this->queue->setnext(nullptr);         this->queue->setprev(nullptr);         node->setprev(nullptr);         //--------------------------------------------------- crashes here          delete this->queue;         this->queue = node;     }      size--;     return data; }  template <class t> queue<t>::~queue() {     delete this->queue;     this->queue = nullptr;     this->tail = nullptr; }     #endif  /* tsqueue_h */ 

in dnode destructor, don't want delete next , prev nodes. want delete node, not links to.

remove these lines

delete this->next; delete this->prev; 

edit: isn't problem, because clearing out next , prev values before delete node. still think better not automatically delete whole chain, long consistent how handle node deletion should still work.

you problem when dequeue last node, still try set next pointer of next node in line:

node->setprev(nullptr); //--------------------------------------------------- crashes here  

at point node nullptr, trying access node->next causes crash. simple if test need

if (node != nullptr)     node->setprev(nullptr); //--------------------------------------------------- no longer crashes here  

edit 2: note in case next node in queue nullptr, want set tail nullptr.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -