Esempio n. 1
0
        /// <summary>
        /// 添加新的元素
        /// </summary>
        /// <param name="t">待添加的元素</param>
        public void AddToLast(T t)
        {
            var newNode = new MyLinkedListNode <T>(t);
            var temp    = head;

            if (this.head == null)
            {
                head = newNode;
            }
            else
            {
                while (temp.next != null)
                {
                    temp = temp.next;
                }
                temp.next = newNode;
            }
            _count++;
        }
Esempio n. 2
0
        /// <summary>
        /// 移除第一个匹配的索引
        /// </summary>
        /// <param name="t">待匹配的元素</param>
        public void Remove(T t)
        {
            var temp = head;
            var last = new MyLinkedListNode <T>();

            if (temp == null)
            {
                throw new Exception();
            }
            if (temp.value.Equals(t))
            {
                _count--;
                return;
            }
            while (!temp.value.Equals(t) && temp.next != null)
            {
                last = temp;
                temp = temp.next;
            }
            last.next = temp.next;
            temp      = null;
            _count--;
        }
Esempio n. 3
0
 public MyLinkedList()
 {
     this.head = null;
     _count    = 0;
 }