Solved

C# Data Strcutures

Posted on 2010-08-20
3
324 Views
Last Modified: 2013-12-17
using System;

namespace SingleLinkedList
{
	/// <summary>
	/// this class implement each node in SSL
	/// </summary>
	class SLLNode
	{
		private int _data;
		private SLLNode _next;
		public SLLNode()
			{
				_next = null;
			}
		public int data
		{
			get
			{
				return _data;
			}
			set
			{
				_data=value;
			}
		}
		public SLLNode next
		{
			get
			{
				return _next;
			}
			set
			{
				_next=value;
			}
		}
	}
	/// <summary>
	/// this class implement Single Linked List ( SLL )
	/// </summary>
	class SLL
	{
		private SLLNode head;
		private int totalNode;
		public SLL()
		{
			head=null;
			totalNode=0;
		}
		public void insert(int data)
		{
			if(head==null)
			{
				head=new SLLNode();
				head.data = data;
			}
			else
			{
				SLLNode temp;
				temp=new SLLNode();
				temp.data=data;
				temp.next=head;
				head=temp;
			}
			totalNode++;
		}
		public int removeAll(int data)
		{
			//remove any data in linked list and return number
			//of items that have been deleted
			SLLNode before,current;
			int total=0;
			before=null;
			current=head;
			while(current!=null)
			{
				if(current.data==data)
				{
					total++;
					totalNode--;
					if(before==null)
					{
						head=current.next;
						System.GC.Collect();  
					}
					else
					{
						before.next=current.next;
						System.GC.Collect(); 
					}
				}
				before=current;
				current=current.next; 
			}
			return total;
		}
		public int count
		{
			get
			{
				return totalNode;
			}
		}
		public int this [int index]
		{
			get
			{	
				SLLNode temp=head;
				int i;
				if(index>=0 && index<count)
				{
					for(i=0;i<index;i++)
					{
						temp=temp.next; 
					}
					return temp.data; 
				}
				else
				{
					throw(new IndexOutOfRangeException()); 
				}
			}
			set
			{
				SLLNode temp=head;
				int i;
				if(index>=0 && index<count)
				{
					for(i=0;i<index;i++)
					{
						temp=temp.next; 
					}
				    temp.data=value; 
				}
				else
				{
					throw(new IndexOutOfRangeException()); 
				}
			}
		}
		public void traverse()
		{
			//print out all data in linked list
			SLLNode temp=head;
			while(temp!=null)
			{
				Console.Write(temp.data + " "); 
				temp=temp.next; 
			}
		}
	}
	/// <summary>
	/// CMain contain the necessary code to test class SLL
	/// </summary>
	class CMain
	{
		[STAThread]
		static void Main(string[] args)
		{
			SLL mySLL;
			mySLL=new SLL();
			mySLL.insert(14); 
			mySLL.insert(21); 
			mySLL.insert(7); 
			mySLL.insert(28); 
			mySLL.insert(21);
			Console.Write("Data in this SLL are : ");
			mySLL.traverse();
			Console.Write("\n");
			Console.WriteLine("Total item in SLL : " + mySLL.count);
			Console.WriteLine("Delete all data 21");
			mySLL.removeAll(21);
			Console.WriteLine("Total item in SLL after deletion : " + mySLL.count);
			Console.Write("They are : ");
			for(int i=0;i<mySLL.count;i++)
			{
				Console.Write(mySLL[i]+" ");
			}
			Console.WriteLine("\nFinish !"); 
		}
	}
}

Open in new window

______________________________________________________

So when the first time node is created by adding 14   , in this situation  the head will point to
 new node " 14 "  .
My doubt here is  even Head also a Node ( of type SLLNode)  so at this what does the  data part contains ??
0
Comment
Question by:N_Sri
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 125 total points
ID: 33489579
Not quite right. When you first add "14" to the empty list then "14" becomes "head", you don't have "head" plus another node. The list will simply contain a single node which, by definition, is also the head.
0
 
LVL 2

Assisted Solution

by:omraviprakash
omraviprakash earned 125 total points
ID: 33499869
Initially in the constructor, the head is set to null, that means it does not point of anything (no pointer to any memory location). When you add a new node (of type SLLNode), with data 14, the head now points to first node containing data 14. therefore, remember that head as such is simply a pointer to the linked list and it does not contain any data at any point. The data is only contained in each of the SLLNode (nodes of linked list).
0
 

Author Closing Comment

by:N_Sri
ID: 33554301
thanku
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now