用C#实现单链表(初始化数据,返回链表元素个数)

初学C#记录历程,记录心情。

在学习的过程中严重参考了前辈们在网上的分享,当然也不忘有自己的细微思想在里面,写在这里主要是对自己由不懂到能独立写下来的纪念。如有冒犯,还请原谅。

定义接口:

View Code
1    //定义接口IList,来规范后面类的功能实现
2     public interface IList<T>
3     {
4            int GetLength();    //得到链表的数据个数
5            bool IsEmpty();     //判断链表是否为空
6            void Clear();       //清空链表
7            void Add(T item);   //增加数据到链表(初始化)
8       
9     }

 

定义节点类:

View Code
 1  //节点类
 2     public class LinkNode<T>
 3     {
 4        private LinkNode<T> next;  //指向下一个结点对象 字段
 5        private T data;            //结点的数据  字段
 6  
 7         /// <summary>
 8        ///  结点的数据 属性
 9         /// </summary>
10         public T Data
11         {
12             get { return this.data; }
13             set { this.data = value; }
14         }
15  
16         /// <summary>
17         /// 指向下一个结点对象 属性
18         /// </summary>
19         public LinkNode<T> Next           
20         {
21             get { return this.next; }
22             set { this.next = value; }
23         }          
24 
25         public LinkNode()
26         {
27             this.data = default(T);
28             this.next = null;
29         }
30 
31         public LinkNode(T t)
32         {
33             this.data = t;
34             this.next = null;
35         }
36 
37         public LinkNode(T t, LinkNode<T> node)
38         {
39             this.data = t;
40             this.next = node;
41         }
42     }

定义链表类,继承接口IList:

View Code
  1 //定义链表类,继承接口IList
  2             public class LinkList<T>:IList<T>
  3             {
  4         
  5                 private LinkNode<T> head;     //单链表的表头
  6                 public LinkNode<T> Head
  7                 {
  8                     get { return this.head; }
  9                     set { this.head = value; }
 10                 }
 11 
 12                 public LinkList()
 13                 {
 14                     this.head = null;
 15                 }
 16 
 17                 public LinkList(LinkNode<T> node)
 18                 {
 19                     this.head = node;
 20                 }
 21 
 22                public enum AddPosition    //添加在什么位置的枚举
 23                 { Head, 
 24                   Tail
 25                 };
 26 
 27                 /// <summary>
 28                 /// 增加节点
 29                 /// </summary>
 30                 /// <param name="item">要增加的节点的数据</param>
 31                 /// <param name="p">在头部增加还是尾部增加</param>
 32                 public void Add(T item, AddPosition p)
 33                 {
 34                     if (p == AddPosition.Tail)
 35                     {
 36                         this.Add(item);            //调用Add(item), 添加在尾部
 37                     }
 38                     else
 39                     {
 40                         LinkNode<T> node = this.head;
 41                       //  LinkNode<T> nodeTemp;
 42                         LinkNode<T> AddedNode = new LinkNode<T>(item);
 43                         if (this.head == null)
 44                         {
 45                             this.head = AddedNode;
 46                         }
 47                         else
 48                         {
 49                             AddedNode.Next= node;
 50                             this.head = AddedNode;
 51                         }
 52 
 53                     }
 54  
 55                 }
 56 
 57                 /// <summary>
 58                 /// 添加数据到链表尾部
 59                 /// </summary>
 60                 /// <param name="item">要添加的结点的数据</param>
 61                 public void Add(T item)
 62                 {
 63                     LinkNode<T> AddedNode = new LinkNode<T>(item, null);  //创建一个结点
 64                     if (this.head == null)
 65                     {
 66                         this.head = AddedNode;
 67          
 68                     }
 69                     else
 70                     {
 71                        LinkNode<T> node=this.head;
 72                        while (node.Next != null)
 73                        {
 74                            node = node.Next;
 75                        }
 76                        node.Next =AddedNode;
 77                     }
 78                 }
 79 
 80                 /// <summary>
 81                 /// 返回链表元素个数
 82                 /// </summary>
 83                 /// <returns></returns>
 84                 public int  GetLength()
 85                 {
 86                     LinkNode<T> node = new LinkNode<T>();
 87                     int count = 0;
 88                    node = this.head;
 89                     while (node!= null)
 90                     {
 91                         count++;
 92                         node = node.Next;
 93                     }
 94                     return count;
 95 
 96                }
 97       
 98                 /// <summary>
 99                 /// 判断链表是否为空
100                 /// </summary>
101                 /// <returns></returns>
102                 public bool IsEmpty()
103                 {         
104                     return this.head == null;
105                 }
106 
107                 /// <summary>
108                 /// 清空链表
109                 /// </summary>
110                 public void Clear()
111                 {
112                     this.head =null;
113                 }
114 }

测试是否通过:

View Code
 1  static void Main(string[] args)
 2         {
 3                LinkList<int> MyList = new LinkList<int>();
 4                LinkNode<int> node = new LinkNode<int>();
 5                            
 6                  //验证返回链表个数
 7                  MyList.Add(3);
 8                  MyList.Add(4);
 9                  Console.WriteLine("The total data of list is:{0}", MyList.GetLength());
10              
11                 //验证是否已清空
12                  MyList.Clear();   //清空链表
13                  node = MyList.Head;
14                   if (node == null)
15                   {
16                       Console.WriteLine("List has been cleared");
17                   }
18                   else
19                   {
20                       node = PrintData(node);
21                   }
22             
23             //验证是否为空
24               Console.WriteLine("Empty?:{0}",MyList.IsEmpty());
25               Console.ReadLine();          
26         }
27 
28         private static LinkNode<int> PrintData(LinkNode<int> node)
29         {
30             while (node != null)
31             {
32                 Console.WriteLine("The data of List are:{0}", node.Data);
33                 node = node.Next;
34             }
35             return node;
36         }

 

 

 

 

 

 

posted on 2013-01-15 14:49  bloomalone  阅读(900)  评论(0编辑  收藏  举报