I have an array of linked list , each index of array holds linked list.
Beside that, I have another linked list , that hold nodes in exact order I inputted them.
So for example. My array has 500 indexes. I input one node on index 1 and one node on index 2. When I print linked list on index 1 it will print only one node. However when I print the linked list that holds the exact order that node were inputted in , it will print both of them in order I inputted them.
I have implemented this using this code
class CHash{
public:
CHash(){
for( int i = 0; i < 500 ;i++){
holder[i] = NULL;
}
first = NULL;
last = NULL;
};
void insert( string key , string value ){
size_t num = index(key);
Node *tmp;
if( holder[num] == NULL ){
tmp = new Node(key , value , NULL);
holder[num] = tmp;
}else{
tmp = new Node(key, value , holder[num]);
holder[num] = tmp;
}
if( first == NULL){
tmp -> nextNode = NULL;
tmp -> prevNode = NULL;
first = tmp;
last = tmp;
}else{
tmp -> prevNode = last;
last -> nextNode = tmp;
last = tmp;
}
}
void Print( size_t number ){
Node *tmp = holder[number];
while( tmp!= NULL){
cout << tmp -> value << endl;
tmp = tmp -> next;
}
}
void PrintAll(){
Node *tmp = first;
while( tmp != NULL){
cout << tmp -> value << endl;
tmp = tmp -> nextNode;
}
}
size_t index( string name ){
return name.length();
}
void de(string val){
size_t num = index(val);
if( holder[num] == NULL){
return;
}
Node *tmp = holder[num];
Node *help;
Node *help1;
while( tmp != NULL){
if( tmp -> key == val){
Node *temp = tmp;
if( tmp -> prevNode != NULL)
help = tmp -> prevNode;
else
help = NULL;
if( tmp -> nextNode != NULL)
help1 = tmp -> nextNode;
else
help1 = NULL;
if( tmp -> next != NULL){
tmp = tmp -> next;
tmp -> nextNode = help1;
tmp -> prevNode = help;
}
else
tmp = NULL;
delete temp;
return ;
}
tmp = tmp -> next;
}
}
It works , what troubles me is the de
method. It should find the node with key same as argument and delete it. This delete should be reflected in both linked list. I have been trying to figure this out for a while but it always throw seg fault.
Example of usage.
CHash one;
one.insert("two","cauko");
one.insert("tuk","hello");
one.insert("three","hrello");
one.Print(3) // should print "hello" , "cauko"
one.PrintAll() // should print "cauko" , "hello" , "hrello"
one.de("tuk");
one.Print(3); // should print only "cauko"
one.PrintAll(); // should print "cauko" , "hrello"
Where did I make a mistake?