Create a Linked link or Generic List in Asp.Net
A Simple way to create a Linked List in ASP.Net C# by using
List<T> = new List<T>(); Class.
<T> Can be any data type or user defined class.
Like, if you want to create a List of Sting or int datatype.
List<String> LS= new List<String>();
or
List<int>LI= new List<int>();
to add nodes in List:
LS.Add("U");
LS.Add("M");
LS.Add("R");
or
LI.Add(7);
LI.Add(8);
LI.Add(6);
List will be shown like this :
U->M->R
or
7->8->6
If you want to add more than one variables in one Node then you need to create a class for that.
For exp u want to store Name and Age of student in a node. Then you first create a user defined class for that.
class Node
{
public String Name;
public int Age = 0;
public Node()
{ }
public Node(String data, ag)
{
this.Name = data;
this.Age=ag;
}
}
Here we have defined a class name called Node and in this we declared two members named Name and Age. We have defined to constructors, default and overloaded constructor.
now you can use this class to create your custom Linked List.
Like,
List<Node> LStudents= List<Node>();
To add new Node:
Node a = new Node();
a.Name="UMR";
a.Age=25;
LStudents.Add(a);
Node a2 = new Node();
a2.Name="UMR2";
a2.Age=26;
LStudents.Add(a2);
you may use constructor:
Node a = new Node("UMR",25);
LStrudents.Add(a);
You can traverse the Linked List like:
for(int i=0;i<List.Count;i++)
{
.
.
.
//your code
.
.
TextBox1.Text=List[i].Name;
TextBox2.Text=List[i].Age;
}

Comments
Post a Comment