I am sending you UNSAFE Code in which I am trying to implement Linked List like we used to do in C. What I believe is that the following line of code should allocate new memory address on each iteration ¡°test1 = new LinkListPtr();¡± but in fact it is not. See if you can figure it out what wrong with it.
class Program
{
unsafe public struct LinkListPtr
{
public int num;
public LinkListPtr* linkListPtr;
}
unsafe static void Main(string[] args)
{
LinkListPtr* cur=null;
LinkListPtr* first=null;
LinkListPtr* obj=null;
LinkListPtr test1;
for (int i = 0; i < 3; i++)
{
if (first == null)
{
LinkListPtr test = new LinkListPtr();
first =&test;
first->num = i;
first->linkListPtr = null;
}
else
{
cur = first;
while (cur->linkListPtr != null)
{
//I am stuck into a continuous loop
cur = cur->linkListPtr;
}
test1 = new LinkListPtr();
obj = &test1;
obj->num=i;
obj->linkListPtr = null;
cur->linkListPtr = obj;
}
}
while (first != null)
{
Console.WriteLine(first->num);
first = first->linkListPtr;
}
Console.ReadLine();
}
}
}