本人在用C#实现单链表时,碰到问题如下,请高手指教. 在求做表长时,定义一个该类型变量Indexor,在循环过程中, Indexor = Indexor.Next; 会不会随着Indexor的变化导致之前Indexor引用的值也跟着变了,进而导致链表里的值都变了....毕竟都是引用阿...本人很纠结...求解释...//节点定义
public class Node<T>
{
private T data;
private Node<T> next; public T Data
{
get
{
return data;
}
set
{
data = value;
}
} public Node<T> Next
{
get
{
return next;
}
set
{
next = value;
}
} public Node(T val,Node<T> p)
{
data = val;
next = p;
} public Node(Node<T> p)
{
next = p;
} public Node(T val)
{
data = val;
next = null;
}
public Node()
{
data = default(T);
next = null;
}
}//表定义
public class LinkList<T>
{
private Node<T> head; public Node<T> Head
{
get
{
return head;
}
set
{
head = value;
}
} public LinkList()
{
head = null;
}
//求表长
public int Getlength()
{
int i=0;
Node<T> Indexor = head;
while(Indexor.Next!=null)
{
Indexor = Indexor.Next;
i++;
}
return i;
} }
public class Node<T>
{
private T data;
private Node<T> next; public T Data
{
get
{
return data;
}
set
{
data = value;
}
} public Node<T> Next
{
get
{
return next;
}
set
{
next = value;
}
} public Node(T val,Node<T> p)
{
data = val;
next = p;
} public Node(Node<T> p)
{
next = p;
} public Node(T val)
{
data = val;
next = null;
}
public Node()
{
data = default(T);
next = null;
}
}//表定义
public class LinkList<T>
{
private Node<T> head; public Node<T> Head
{
get
{
return head;
}
set
{
head = value;
}
} public LinkList()
{
head = null;
}
//求表长
public int Getlength()
{
int i=0;
Node<T> Indexor = head;
while(Indexor.Next!=null)
{
Indexor = Indexor.Next;
i++;
}
return i;
} }
{
Indexor = Indexor.Next;
i++;
}
又没有改动节点,你怕什么?
class A
{
private int i;
private string j; public int I
{
get { return i; }
set { i = value; }
} public string J
{
get { return j; }
set { j = value; }
} public A()
{
i = 5;
j = "j is default";
} } static void Main(string[] args)
{
A a = new A();
Console.WriteLine(a.I);
Console.WriteLine(a.J); A b = a;
Console.WriteLine(b.I);
Console.WriteLine(b.J); //c.I = 10;
//c.J = "J is changed"; A c = new A();
c.I = 10;
c.J = "J is changed"; b = c; Console.WriteLine(a.I);
Console.WriteLine(a.J); }