1. 程式人生 > >C# 單鏈表的實現

C# 單鏈表的實現

    /// <summary>
    /// 節點類
    /// </summary>
    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() { this._data = default(T); this._next = null; } public Node(T data) { this._data = data; this._next = null; } } /// <summary>
/// 單鏈表 /// </summary> public class SingleLinked<T> { private Node<T> _head; private int _count; public Node<T> Head { get { return _head; } } public int Count { get { return _count; } } public
SingleLinked() { _head = null; } /// <summary> /// 清空 /// </summary> public void Clear() { _head = null; _count=0; } /// <summary> /// 判斷連結串列是否為空 /// </summary> /// <returns></returns> public bool IsEmpty() { if (_head == null) { return true; } return false; } /// <summary> /// 新增元素 /// </summary> /// <param name="data"></param> public void Add(T data) { Node<T> newNode = new Node<T>(data);//將資料放入節點 if (_head == null) { _head = newNode; _count++; return; } Node<T> p = new Node<T>(); p = _head; while (p.Next != null) { p = p.Next; } p.Next = newNode; _count++; } /// <summary> /// 移除指定的節點 /// </summary> /// <param name="index"></param> /// <returns></returns> public T RemoveAt(int index) { if (IsEmpty() || index > _count - 1 || index < 0) { Console.WriteLine("SingleLinked is empty or index is error!"); throw new Exception("SingleLinked is empty or index is error!"); } Node<T> p = new Node<T>(); p = _head; if (index == 0) //刪除頭節點 { _head = p.Next; _count--; return p.Data; } int i = 0; Node<T> frontNode = new Node<T>();//要刪除節點的前一個節點 while (p.Next != null) { frontNode = p; p = p.Next; if (index == ++i) { break; } } frontNode.Next = p.Next; _count--; return p.Data; } /// <summary> /// 在指定位置插入節點(前插) /// </summary> /// <param name="data"></param> /// <param name="index"></param> /// <returns></returns> public bool Insert(int index, T data) { if (IsEmpty() || index > _count - 1 || index < 0) { Console.WriteLine("SingleLinked is empty or index is error!"); throw new Exception("SingleLinked is empty or index is error!"); } Node<T> newNode = new Node<T>(data); Node<T> p = _head; if (index == 0) { _head = newNode; _head.Next = p; _count++; return true; } Node<T> frontNode = new Node<T>(); int i = 0; while (p.Next != null) { frontNode = p; p = p.Next; if (index == ++i) { break; } } frontNode.Next = newNode; newNode.Next = p; _count++; return true; } /// <summary> /// 獲取指定節點的元素 /// </summary> /// <param name="index"></param> /// <returns></returns> public T GetData(int index) { if (IsEmpty() || index > _count - 1 || index < 0) { Console.WriteLine("SingleLinked is empty or index is error!"); throw new Exception("SingleLinked is empty or index is error!"); } Node<T> p = new Node<T>(); p = _head; if (index == 0) { return p.Data; } int i = 0; while (p.Next != null) { p = p.Next; if (index == ++i) { break; } } return p.Data; } /// <summary> /// 判斷節點時候包含指定元素 /// </summary> /// <param name="data"></param> /// <returns></returns> public bool Contains(T data) { if (IsEmpty()) return false; if (_head.Data.Equals(data)) return true; Node<T> p = _head; while (p.Next != null) { p = p.Next; if (p.Data.Equals(data)) { return true; } } return false; } /// <summary> /// 獲取與元指定素匹配的最後一個元素的索引 /// </summary> /// <param name="data"></param> /// <returns></returns> public int LastIndexOf(T data) { if (IsEmpty()) return -1; Node<T> p = _head; int index = -1; int lastIndex = -1; while (p != null) { index++; if (p.Data.Equals(data)) { lastIndex = index; } p = p.Next; } return lastIndex; } /// <summary> /// 獲取與元指定素匹配的第一個元素的索引 /// </summary> /// <param name="data"></param> /// <returns></returns> public int FirstIndexOf(T data) { if (IsEmpty()) return -1; Node<T> p = _head; int index = -1; while (p!=null) { index++; if (p.Data.Equals(data)) { return index; } p = p.Next; } return index; } /// <summary> /// 顯示連結串列節點 /// </summary> public void Display() { Node<T> p = new Node<T>(); p = _head; while (p != null) { Console.WriteLine(p.Data); p = p.Next; } } /// <summary> /// 逆置 /// </summary> public void Reverse() { //頭插法:將原連結串列的頭節點的下一個節點插入到當前頭接點的前面 Node<T> oriHead = _head;//記錄原始頭節點 Node<T> tempCurHead = _head;//當前臨時頭節點 Node<T> nextNode=new Node<T>();//原始節點的下一個節點 //將原始頭節點的下一個節點設定為當前的頭節點 while (oriHead.Next != null) { nextNode = oriHead.Next;//記錄原始節點的下一個節點 oriHead.Next = nextNode.Next;//原始節點的下一個節點指向下下個節點 nextNode.Next = tempCurHead;//原始節點的下一個節點指向當前頭節點 tempCurHead = nextNode;//原始節點的下一個節點設定為臨時頭節點 } _head = tempCurHead; } }