C#实现单链表
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SingleList 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 MyLinkList<string> myLinkList = new MyLinkList<string>();//实例化一个链表 14 Console.WriteLine(myLinkList.GetLength());//获取长度 15 16 //添加元素 17 myLinkList.Append("hello"); 18 myLinkList.Append("C#"); 19 myLinkList.Append("I"); 20 myLinkList.Append("love"); 21 myLinkList.Append("U"); 22 23 //myLinkList.Insert("!", 6);//在i结点前插入元素,位置错误测试 24 //myLinkList.InsertPost("!", 5);//在i结点后插入元素,位置错误测试 25 26 //myLinkList.InsertPost(",", 1); //后插元素 27 //myLinkList.Insert(",", 4); //前插元素 28 29 //Console.WriteLine(myLinkList.GetElement(4));//获取结点,并显示 30 31 myLinkList.Delete(1); //删除结点 32 33 myLinkList.Display(); 34 35 Console.WriteLine(myLinkList.GetLength()); //显示链表长度 36 Console.Read(); 37 38 } 39 } 40 41 //单链表节点类,采用泛型 42 public class Node<T> 43 { 44 private T data;//数据域,当前节点的数据 45 private Node<T> next;//引用域,即下一节点 46 47 //构造器,数据域+引用域,普通节点 48 public Node(T item, Node<T> p) 49 { 50 data = item; 51 next = p; 52 } 53 54 //构造器:引用域,头节点 55 public Node(Node<T> p) 56 { 57 next = p; 58 } 59 60 //构造器,数据域,尾节点 61 public Node(T val) 62 { 63 data = val; 64 next = null; 65 } 66 67 //构造器:无参数 68 public Node() 69 { 70 data = default(T); 71 next = null; 72 } 73 74 //数据域属性 75 public T Data 76 { 77 get { return data; } 78 set { data = value; } 79 } 80 81 //引用域属性 82 public Node<T> Next 83 { 84 get { return next; } 85 set { next = value; } 86 } 87 } 88 89 //链表类,包含链表定义以及基本的操作方法 90 public class MyLinkList<T> 91 { 92 private Node<T> head;//单链表的头节点 93 94 //头节点属性 95 public Node<T> Head 96 { 97 get { return head; } 98 set { head = value; } 99 } 100 101 //构造器 102 public MyLinkList() 103 { 104 head = null; 105 } 106 107 //求单链表的长度 108 public int GetLength() 109 { 110 Node<T> p = head; 111 int len = 0; 112 while (p != null) 113 { 114 ++len; 115 p = p.Next; 116 } 117 return len; 118 } 119 120 //清空单链表 121 public void Clear() 122 { 123 head = null; 124 } 125 126 //判断单链表是否为空 127 public bool IsEmpty() 128 { 129 if (head == null) 130 return true; 131 else 132 return false; 133 } 134 135 //在单链表的末尾添加新的元素 136 public void Append(T item) 137 { 138 Node<T> q = new Node<T>(item); 139 Node<T> p = new Node<T>(); 140 if (head == null) 141 { 142 head = q; 143 return; 144 } 145 p = head; 146 while (p.Next != null) 147 { 148 p = p.Next; 149 } 150 p.Next = q; 151 } 152 153 //在单链表的第i个节点的位置前插入一个值为item的结点 154 public void Insert(T item, int i) 155 { 156 if (IsEmpty() || i < 1 || i > GetLength()) 157 { 158 Console.WriteLine("LinkList is empty or Position is error!"); 159 return; 160 } 161 if (i == 1) 162 { 163 Node<T> q = new Node<T>(item); 164 q.Next = head; 165 head = q; 166 return; 167 } 168 Node<T> p = head; 169 Node<T> r = new Node<T>(); 170 int j = 1; 171 while (p.Next != null && j < i) 172 { 173 r = p; 174 p = p.Next; 175 ++j; 176 } 177 if (j == i) 178 { 179 Node<T> q = new Node<T>(item); 180 q.Next = p; 181 r.Next = q; 182 } 183 else 184 { 185 Console.WriteLine("The" + i + "th node is not exist!"); 186 } 187 } 188 189 //在单链表的第i个节点的位置后插入一个值为item的结点 190 public void InsertPost(T item, int i) 191 { 192 if (IsEmpty() || i < 1 || i > GetLength()) 193 { 194 Console.WriteLine("LinkList is empty or Position is error!"); 195 return; 196 } 197 if (i == 1) 198 { 199 Node<T> q = new Node<T>(item); 200 q.Next = head.Next; 201 head.Next = q; 202 return; 203 } 204 Node<T> p = head; 205 int j = 1; 206 while (p != null && j < i) 207 { 208 p = p.Next; 209 ++j; 210 } 211 if (j == i) 212 { 213 Node<T> q = new Node<T>(item); 214 q.Next = p.Next; 215 p.Next = q; 216 } 217 else 218 { 219 Console.WriteLine("The" + i + "th node is not exist!"); 220 } 221 } 222 223 //删除单链表的第i个节点 224 public T Delete(int i) 225 { 226 if (IsEmpty() || i < 0 || i > GetLength()) 227 { 228 Console.WriteLine("Linklist is empty or Position is error!"); 229 return default(T); 230 } 231 Node<T> q = new Node<T>(); 232 if (i == 1) 233 { 234 q = head; 235 head = head.Next; 236 return q.Data; 237 } 238 Node<T> p = head; 239 int j = 1; 240 while (p.Next != null && j < i) 241 { 242 ++j; 243 q = p; 244 p = p.Next; 245 } 246 if (j == i) 247 { 248 q.Next = p.Next; 249 return p.Data; 250 } 251 else 252 { 253 Console.WriteLine("The" + i + "th node is not exist!"); 254 return default(T); 255 } 256 } 257 258 //获得单链表的第i个数据元素 259 public T GetElement(int i) 260 { 261 if (IsEmpty() || i < 0 || i > GetLength()) 262 { 263 Console.WriteLine("Linklist is empty or Position is error!"); 264 return default(T); 265 } 266 Node<T> p = new Node<T>(); 267 p = head; 268 int j = 1; 269 while (p.Next != null && j < i) 270 { 271 ++j; 272 p = p.Next; 273 } 274 if (j == i) 275 { 276 return p.Data; 277 } 278 else 279 { 280 Console.WriteLine("The" + i + "th node is not exist!"); 281 return default(T); 282 } 283 } 284 285 //在链表中查找值为value的结点 286 public int Locate(int value) 287 { 288 if (IsEmpty()) 289 { 290 Console.WriteLine("Linklist is empty!"); 291 return -1; 292 } 293 Node<T> p = new Node<T>(); 294 p = head; 295 int i = 1; 296 while (!p.Data.Equals(value) && p.Next != null) 297 { 298 p = p.Next; 299 ++i; 300 } 301 return i; 302 } 303 304 //显示链表 305 public void Display() 306 { 307 Node<T> p = new Node<T>(); 308 p = head; 309 while (p != null) 310 { 311 Console.WriteLine(p.Data + " "); 312 p = p.Next; 313 } 314 } 315 } 316 }