k

k

  • NA
  • 25
  • 0

why the value is null in this case?

Oct 10 2008 3:15 PM
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();
        }
    }
}

Answers (8)