?
Solved

C# Data Strcutures

Posted on 2010-08-20
3
Medium Priority
?
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 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 500 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses

771 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