C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-15-2005, 06:27 PM   #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
aciarlillo is offline   Reply With Quote
Old 09-15-2005, 06:58 PM   #2
Registered User
 
Join Date: Jan 2005
Posts: 7,252
>> 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.
Daved is offline   Reply With Quote
Old 09-16-2005, 12:14 AM   #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 `&' ?)
aciarlillo is offline   Reply With Quote
Old 09-18-2005, 10:34 AM   #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
aciarlillo is offline   Reply With Quote
Old 09-18-2005, 11:31 AM   #5
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
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.
dwks is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete Function in Doubly Linked List Dampecram C Programming 5 11-15-2008 04:30 PM
Linked List Help CJ7Mudrover C Programming 9 03-10-2004 10:33 PM
Making a doubly linked list mlupo C Programming 1 10-16-2002 09:05 PM
doubly linked lists qwertiop C++ Programming 3 10-03-2001 06:25 PM
singly linked list clarinetster C Programming 2 08-26-2001 10:21 PM


All times are GMT -6. The time now is 05:38 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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