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:
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?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; }
Here is my Dlist.h:
and here is Dnode as well just in case: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"
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.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; };
thanks,
-ac



LinkBack URL
About LinkBacks


