OK, so I started implementing whiteflags's idea.
here's my List.h:
Code:
typedef unsigned long list_index_type;
class DLListNode{
public:
class DLListNode *list_next_link,*list_prev_link;
list_index_type list_index;
};
template <typename list_member_type>
list_member_type* find_edge(list_member_type* &head, list_index_type x)
{
if(!head) return 0;//list is nonexistant
list_member_type* aux=head;
if(aux->list_index==x) return aux;//head is the result
while((aux->list_next_link)&&(aux->list_next_link->list_index < x)) aux=aux->list_next_link;//Horizontal scan. Find latest node with less x.
if(!aux->list_next_link) return 0;//Didn't find it after a horizontal scan, all were less.
if(aux->list_next_link->list_index > x) return 0;//Didn't find it either, next was greater.
return aux->list_next_link;//Found it normally
}
template <typename list_member_type>
bool DLListInsert(list_member_type* &head, list_member_type* temp)
{
if(find_edge<list_member_type>(head,temp->list_index)) return 1;//Duplicate entry
if((!head)||(temp->list_index < head->list_index)) //new head
{
temp->list_next_link=head;
temp->list_prev_link=0;
if(head) head->list_prev_link=temp;
head=temp;
}
else
{
list_member_type* aux=head;
while((aux->list_next_link)&&(aux->list_next_link->list_index < temp->list_index))
aux=aux->list_next_link;//Find previous
temp->list_next_link=aux->list_next_link;
temp->list_prev_link=aux;
if(aux->list_next_link) aux->list_next_link->list_prev_link=temp;
aux->list_next_link=temp;
}
return 0;
}
and my main.cpp:
Code:
#include <iostream>
#include "List.h"
using namespace std;
typedef class node* np;
class node: public DLListNode
{
public:
int key;
};
class node* head=0;
//np create();
//void insert(np &head, np temp);
//void _delete(np &head, int key);
//void print(np x);
/*Debug recommendation sequence, use print after each step. Has already been tested and correct.
Insert 7 to test first element
Insert 1 to test new head
Insert 4 to test middle element
Insert 9 to test last element. It must be now 1-4-7-9.
Delete in the reverse sequence to test everything: 9,4,1,7.*/
int main()
{
int option;
do
{
system("cls");
cout << "==--Double Linked List--==\n\n\n1.Insert\n2.Delete\n3.Print\n\n0.Exit\n\n\nEnter an option: ";
cin >> option;
system("cls");
switch(option)
{
case 1:
//insert(head,create());
np temp=new class node;
cout << "Enter key of new node: ";
cin >> temp->list_index;
DLListInsert<node>(head,temp);
break;
}
}while(option);
}
I read that "a pointer to a derived class is type-compatible with a pointer to its base class"[SIC]. Yet, when i complie, I get a single type of error multiple times: invalid conversion from DLListNode* to node*. What is going wrong?