using System;
namespace 单向链表
{
#region 节点类
public class Node
{
private int x;
public Node next; //指向下一个节点
public Node(int n)
{
x = n;
}
public int X
{
get
{
return x;
}
}
}
#endregion
#region 链表类
public class CreateListTable
{
public Node head; //指向根节点的变量
public Node end; //指向链表尾的变量
public void GetListTable(Node node) //建立链表的方法
{
if(head == null)
{
this.head = node; //head指向第一个节点
this.end = node; //end指向第一个节点
return;
}
#region 把最新加入的节点放在链表头
node.next = this.head; //新生成的节点指向前一个节点
head = node; //head指向新生成的节点
#endregion
//
// #region 把最新加入的节点放在链表尾
// end.next = node; //链表尾的节点指向新加入的节点
// end = end.next; //新加入的节点成为链表尾的节点
// #endregion
}
public void SortListTable()
{
}
public void DisplayListTable()
{
Node node = head; //把head里的地址赋给临时变量node里
while(node != null)
{
Console.WriteLine(node.X);
node = node.next; //把当前节点里包含的地址赋给node,node指向下一个节点
}
}
}
#endregion
#region 测试链表类
class Test
{
static void Main(string[] args)
{
Console.Write("请输入生成节点的个数:");
int number = Convert.ToInt32(Console.ReadLine());
int Value;
CreateListTable clt = new CreateListTable();
for(int i = 0; i < number; i++)
{
Console.Write("请输入第{0}个节点的数据:",i+1);
Value = Convert.ToInt32(Console.ReadLine());
Node node = new Node(Value);
clt.GetListTable(node);
}
clt.DisplayListTable();
}
}
#endregion
}