Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# Data Strcutures

Posted on 2010-08-20
3
Medium Priority
?
341 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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 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.

604 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