Solved

C# Data Strcutures

Posted on 2010-08-20
3
325 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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…

867 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

15 Experts available now in Live!

Get 1:1 Help Now