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;                     //把当前节点里包含的地址赋给nodenode指向下一个节点

                     }

              }

             

 

             

       }

       #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

}

posted on 2008-11-13 00:28  〆o殺殸纨o〆  阅读(144)  评论(0编辑  收藏  举报