Solved

C# Data Strcutures

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTTPS jquery doesn't work 9 58
Graphics 2 26
What can cause the styling on a .NET site to not be found? 17 24
SQL Help 27 40
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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 and create keystrokes in Netbeans IDE 8.0 for Windows.

815 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

12 Experts available now in Live!

Get 1:1 Help Now