示例#1
0
        public object[] ToArray()
        {
            object[] array = new object[Count];

            MyLinkedListNode node = _head;

            int i = 0;

            while (node != null)
            {
                array[i] = node.Value;
                i++;
                node = node.Next;
            }

            return(array);
        }
示例#2
0
        public void AddLast(object value)
        {
            MyLinkedListNode node = new MyLinkedListNode(value);

            if (_head == null)
            {
                _head = node;
                _tail = node;
            }
            else
            {
                _tail.Next = node;
                _tail      = node;
            }

            Count++;
        }
示例#3
0
        public bool Remove(object value)
        {
            MyLinkedListNode node = _head;

            while (node != null)
            {
                if (node.Value.Equals(value))
                {
                    RemoveNode(node);
                    return(true);
                }

                node = node.Next;
            }

            return(false);
        }
示例#4
0
        public bool RemoveByIndex(int index)
        {
            if (index >= Count || index < 0)
            {
                return(false);
            }

            MyLinkedListNode node = _head;

            for (int i = 0; i < index; i++)
            {
                node = node.Next;
            }

            RemoveNode(node);

            return(true);
        }
示例#5
0
        public int IndexOf(object value)
        {
            MyLinkedListNode current = _head;
            int i = 0;

            while (current != null)
            {
                if (current.Value.Equals(value))
                {
                    return(i);
                }

                current = current.Next;
                i++;
            }

            return(-1);
        }
示例#6
0
        public MyLinkedList CloneList()
        {
            MyLinkedList newList = new MyLinkedList();

            if (_head == null)
            {
                return(newList);
            }

            MyLinkedListNode head = _head;

            while (head != null)
            {
                newList.AddLast(head.Value);
                head = head.Next;
            }

            return(newList);
        }
示例#7
0
        private protected override void RemoveNode(MyLinkedListNode node)
        {
            node = node as MyDoublyLinkedListNode;
            MyDoublyLinkedListNode previous = null;
            MyDoublyLinkedListNode current  = _head as MyDoublyLinkedListNode;

            while (current != null)
            {
                if (current.Equals(node))
                {
                    if (previous != null)
                    {
                        previous.Next = current.Next;

                        if (current.Next == null)
                        {
                            _tail = previous;
                        }
                        else
                        {
                            MyDoublyLinkedListNode temp = current.Next as MyDoublyLinkedListNode;
                            temp.Previous = previous;
                        }

                        Count--;
                    }
                    else
                    {
                        RemoveFirst();
                    }
                }

                previous = current;
                current  = current.Next as MyDoublyLinkedListNode;
            }
        }
 internal override void Invalidate()
 {
     base.Invalidate();
     Previous = null;
 }