H******e 发帖数: 4682 | 1 希望static node fromArray(int[] a) {...}中的head或者tail只用到一个, 应该如何
update codes? Expect working codes in visual studio with only head or tail,
which mean eliminate the head or tail. Just 1 variable, no constructor. :)
hint:
You do not need double-linked list. Just think, why do we have head and tail
variables. We do not do anything with head except initial assignment.
class node
{
public int value;
public node next = null;
static node fromArray(int[] a)
{
if (a.Count() == 0) return null;
else
{
node head = new node();
node tail = head;
for (int i = 0; i < a.Count(); i++)
{
tail.value = a[i];
if (i != a.Count() - 1)
{
tail.next = new node();
tail = tail.next;
}
}
return head;
}
}
static void Main(string[] args)
{
int[] c = new int[] { 11, 2, 3 };
node my_list = node.fromArray(c);
while (my_list != null)
{
Console.WriteLine(my_list.value);
my_list = my_list.next;
}
Console.ReadLine();
}
}
|
|