Thread: Cant head insert on doubly linked list

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

    Cant head insert on doubly linked list

    Okay, this is my first time working with doubly linked lists and I can't get past the first thing... doing a head insert.

    here is my code for the insert:
    Code:
    template <class T>
    void Dlist<T>::front_insert(const T& entry)
    {
         if(head == NULL)
                 head = new Dnode<T>(entry);
                 
         Dnode <T> * temp;
         temp = new Dnode<T>(entry);
         temp->next = head;
         head->prev = temp;
         head = temp;
    }
    the biggest problem im having is linking the new node so it has a previous pointer at NULL and a 'next' pointer at the node that was originally the head. Then I need to set the previous node of what was the head to the new head. consufing?

    Here is my 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(Dnode<T>* tail_ptr, const T& entry);
       void rear_remove(Dnode<T>*& tail_ptr);
       void front_remove(Dnode<T>*& head_ptr);
      */
       void front_insert(const T& entry);
     
      
     private:
              Dnode<T>* head;
              Dnode<T>* tail;
    };
    
    #include "Dlist.template"
    and here is Dnode as well just in case:
    Code:
    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;}
       
     private: 
                T datafield;
                Dnode * link_next;
                Dnode * link_prev;
    };
    i would really appreciate any instruction on this, I have the idea of what it is supposed to do but I think I am missing some fundamental knowledge in trying to implement this.
    thanks,
    -ac

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> the biggest problem im having is linking the new node so it has a previous pointer at NULL and a 'next' pointer at the node that was originally the head. Then I need to set the previous node of what was the head to the new head. consufing?

    The code you post does all that except the part about setting the new node's previous pointer to null.

    Also, don't forget to leave the function if the original head is null after you create the new head.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    29
    i keep getting this error in my code:

    67 C:\Dev-Cpp\240c1\Dlist.template invalid use of member (did you forget the `&' ?)
    68 C:\Dev-Cpp\240c1\Dlist.template invalid use of member (did you forget the `&' ?)

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    29
    okay still looking for some help on this... i am getting the above two errors on these two lines of code:

    Code:
    temp->next = head;
    head->prev = temp;
    those two lines are from a function which can be seen in its entirety in the first post.

    thanks

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    temp->next = head;
    head->prev = temp;
    Well, first of all, next and prev are functions:

    Code:
    public:
    // ...
      Dnode * next(){return link_next;}
      Dnode * prev(){return link_prev;}
    // ... 
     private: 
                Dnode * link_next;
                Dnode * link_prev;
    You want something like

    Code:
    temp->setnext(head);
    head->setprev(temp);
    Where setnext() is public and declared as
    Code:
    void setnext(Dnode *n) {link_next = n;}
    setprev() would be similar.
    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. Delete Function in Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 04:30 PM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Making a doubly linked list
    By mlupo in forum C Programming
    Replies: 1
    Last Post: 10-16-2002, 09:05 PM
  4. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM