I try to use C# to illustrate HashTable algorithm. My problem is: when
I assign "hashtable[0] = p;" in method InsertNode-class LinkList, the
properties of p is assigned to HashTable[0] correctly. But when I
debug, I see the value of HashTable[0] is null in DisplayDictionary(){
PrintList(hashtable[0]); } method - class Dictionary inherit from
LinkList class. and that's reason why I cannot display the value of
HashTable[0] into screen.
Please tell me why the value of HashTable[0] is null when I call DisplayDictionary() method. Thank you in advance.
This is my code:
class Node
{
public string word;
public string mean;
public Node next;
public Node()
{
next = null;
}
public Node GetNode(String word, String mean)
{
Node p;
p = new Node();
p.word = string.Copy(word);
p.mean = string.Copy(mean);
return p;
}
public int HF(string word)
{
char[] firstchar = word.ToUpper().ToCharArray(0, 1);
int value = firstchar[0];
return ((value - 65) % 26);
}
}
class LinkList : Node
{
public Node[] hashtable = new Node[26];
public LinkList() { }
public void InsertNode(Node p)
{
// int i = HF(p.word);
// int i = 0;
p.next = hashtable[0];
hashtable[0] = p;
}
public void PrintList(Node list)
{
Node temp = list;
while (temp != null)
{
Console.WriteLine("word:{0}", temp.word);
Console.WriteLine("mean:{0}", temp.mean);
temp = temp.next;
}
}
}
class Dictionary : LinkList
{
public Dictionary() { }
public void MakeDictionary()
{
string word, mean;
word = "a";
mean = "is a";
//Node p = new Node();
LinkList linklist = new LinkList();
Node p = linklist.GetNode(word, mean);
linklist.InsertNode(p); // null
}
public void DisplayDictionary()
{
// for (int i = 0; i < 26; i++)
PrintList(hashtable[0]);
}
}
class Program
{
static void Main(string[] args)
{
Dictionary dict = new Dictionary();
dict.MakeDictionary();
dict.DisplayDictionary();
}
}
}
Loading
AlanPosted Oct 12, 2008, 1:33 PM
In this particular case, yes, as the Dictionary class doesn't hide the inherited ancestor class methods with its own versions.
kPosted Oct 12, 2008, 10:17 AM
Posted Oct 11, 2008, 5:20 PM
Thank you very much for explanation, Alan
AlanPosted Oct 11, 2008, 5:15 PM
The string.Copy method creates a clone of the string being copied i.e. a new instance containing the same characters as the original string.
The clones of the 'word' and 'mean' strings are then assigned to the new node, p's, fields of those names.
This is probably being done to ensure that all nodes contain independent copies of these strings i.e.no node contains a reference to a string which is a field of another node.
Posted Oct 11, 2008, 1:29 PM
How this can be explained (highlighted in yellow)? What is the function of this?
public Node GetNode(String word, String mean)
{
Node p;
p = new Node();
p.word = string.Copy(word);
p.mean = string.Copy(mean);
return p;
}
AlanPosted Oct 11, 2008, 8:04 AM
Well, the Dictionary class is inheriting the InsertNode() method from LinkList and, since LinkList inherits in turn from Node, Dictionary is also inheriting the GetNode() method from the latter class.
So this is why preceding these methods with 'this' works. You could also just call them without preceding them with 'this.
It's the only way that you're going to be able to change the hashtable field (also inherited from LinkList) from within the Dictionary class.
Whilst there's nothing to stop you creating a separate LinkList object and calling its methods, it will have its own copy of hashtable and won't therefore affect the one which the Dictionary object inherits. This will apply wherever you put it.
kPosted Oct 10, 2008, 10:10 PM
AlanPosted Oct 10, 2008, 3:59 PM
When you create the new Dictionary object it inherits the public field 'hashtable' from LinkList. However, all the elements of the hashtable array are null at this point.
When you then call the MakeDictionary() method, a new LinkList object is created and its InsertMode() method is called which sets hashtable[0] to a Node object. However, this LinkList object and its hashtable array are destroyed when the method ends because linklist is a local variable - it's not the same as the LinkList base class from which Dictionary is inheriting.
Thus, the Dictionary object's hashtable still has all its elements
set to null and so the DisplayDictionary() method displays nothing.
To remedy the matter, change the MakeDictionary method to the following:
public void MakeDictionary()
{
string word, mean;
word = "a";
mean = "is a";
//Node p = new Node();
//LinkList linklist = new LinkList();
Node p = this.GetNode(word, mean);
this.InsertNode(p); // not null now
}