3 Replies - 640 Views - Last Post: 10 October 2008 - 07:21 PM Rate Topic: -----

#1 lordelf2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 10-October 08

why the value is null in this case?

Post icon  Posted 10 October 2008 - 11:29 AM

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();
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: why the value is null in this case?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,224
  • Joined: 23-August 08

Re: why the value is null in this case?

Posted 10 October 2008 - 11:53 AM

Try
public void MakeDictionary()
{
    string word, mean;
    word = "a";
    mean = "is a";
    Node p = GetNode(word, mean);
    base.InsertNode(p);
}

Was This Post Helpful? 0
  • +
  • -

#3 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1376
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: why the value is null in this case?

Posted 10 October 2008 - 01:59 PM

Please don't double post. I'm deleting your other thread.
Was This Post Helpful? 0
  • +
  • -

#4 lordelf2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 10-October 08

Re: why the value is null in this case?

Posted 10 October 2008 - 07:21 PM

Thanks JackOfAllTrades very much. but if I want to create object LinkList type to use its methods correctly (not using "base" anymore). Where can I declare it?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1