rear_remove from double linked lsit causing segfault

This is a discussion on rear_remove from double linked lsit causing segfault within the C++ Programming forums, part of the General Programming Boards category; Okay I am creating a doubly linked list with simple functions for head insert, tail insert, head remove, tail remove ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    29

    rear_remove from double linked list causing segfault

    Okay I am creating a doubly linked list with simple functions for head insert, tail insert, head remove, tail remove and a few others. head insert and remove seem to be working fine and the tail insert does as well but for some reason my tail remove causes a segfault whenever it is trying to remove the last file in a list. So if I have a list of 3 integer 5 6 7 it removes 7 fine then 6 but seg faults as soon as it tail points to 5. The debugger reports a seg fault but when I try tpo print the list it spits out random numbers. I have included almost all my code, starting with the offending.

    rear_remove(problem area):
    Code:
    //tail remove
    template <class T>
    void Dlist<T>::rear_remove()
    {
         if(tail != NULL){
         Dnode<T> * removeptr;
         
         removeptr = tail;
         tail = tail->prev();
         if(tail != NULL) //can't use set_next if tail is not pointing to a node (thought this would fix the problem
                 tail->set_next(NULL);
         delete removeptr;
         }
    }
    front_insert(being used to populate the list):
    Code:
    //head insert
    template <class T>
    void Dlist<T>::front_insert(const T& entry)
    {
         if(head == NULL)
                 head = tail = new Dnode<T>(entry);
         else {   
         Dnode <T> * temp;
         temp = new Dnode<T>(entry);
         temp->set_next(head);
         head->set_prev(temp);
         head = temp;
         }
    }
    header file for Dlist - dlist.h:
    Code:
    #include "Dnode.h"
    
    #include <iostream>
    #include <string>
    
    template <class T>
    class Dlist{
    
      public:
             Dlist();
             Dlist(const Dlist& other);
             ~Dlist();
      /*      
       void list_clear(Dnode<T>*& head_ptr);
       void list_copy(const Dnode<T>* source_ptr, Dnode<T>*& head_ptr, Dnode<T>*& tail_ptr);
       std::size_t list_length(const Dnode<T>* head_ptr);
       */
       void rear_insert(const T& entry);
       void rear_remove();
       void front_remove();
       void front_insert(const T& entry);
       void print();
     
      
     private:
              Dnode<T>* head;
              Dnode<T>* tail;
    };
    
    #include "Dlist.template"
    and lastly Dnode.h if it helps:
    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <iterator>
    
    template <class T>
    class Dnode{
    
      public:      
      Dnode(T d =  T(), Dnode *n = NULL, Dnode *p = NULL){datafield = d; link_next = n; link_prev = p;}
      
      T& data(){return datafield;}
      Dnode * next(){return link_next;}
      Dnode * prev(){return link_prev;}
    
      void set_data(T d){datafield = d;}
      void set_link(Dnode * n = NULL, Dnode * p = NULL){link_next = n; link_prev = p;}
      void set_next(Dnode * n = NULL){link_next = n;}
      void set_prev(Dnode * p = NULL){link_prev = p;}
       
     private: 
                T datafield;
                Dnode * link_next;
                Dnode * link_prev;
    };
    sorry if this is a lot to wade through I just wanted to make sure I included everything to make my question understandable. thanks for taking the time to look through this
    -ac
    Last edited by aciarlillo; 09-19-2005 at 06:57 AM.

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    29
    anyone?

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Not this list again . . . .
    rear_remove from double linked list causing segfault
    Make sure you set the pointer that used to point to the old line to NULL. Looks like you do that . . . .
    Code:
    template <class T>
    void Dlist<T>::rear_remove()
    {
         if(tail != NULL){
         Dnode<T> * removeptr;
         
         removeptr = tail;
         tail = tail->prev();
         if(tail != NULL) //can't use . . . .
                 tail->set_next(NULL);
         delete removeptr;
         }
    }
    What happens if that's false?

    This function's also not good:
    Code:
    else {   
         Dnode <T> * temp;
         temp = new Dnode<T>(entry);
         temp->set_next(head);
         head->set_prev(temp);
         head = temp;
         }
    The red line is all wrong.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quoting myself . . .
    Quote Originally Posted by dwks
    What happens if that's false?
    tail will be NULL, and then when you try to access it, SIGSERV.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. double linked list
    By TNA$H in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 07:45 AM
  3. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  4. error message, what does it mean?
    By Shy_girl_311 in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2001, 08:54 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21