![]() |
| | #1 |
| Registered User Join Date: Apr 2005
Posts: 29
| Cant head insert on doubly linked list 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;
}
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"
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 |
| aciarlillo is offline | |
| | #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 | |
| | #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 | |
| | #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; thanks |
| aciarlillo is offline | |
| | #5 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| Code: temp->next = head; head->prev = temp; Code: public:
// ...
Dnode * next(){return link_next;}
Dnode * prev(){return link_prev;}
// ...
private:
Dnode * link_next;
Dnode * link_prev;
Code: temp->setnext(head); head->setprev(temp); Code: void setnext(Dnode *n) {link_next = n;}
__________________ 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |