Пример #1
0
        public bool MoveNext()
        {
            _currentMember = _nextMember;
            if (_currentMember != null)
            {
                _nextMember = _nextMember.GetNext();
            }

            return(_currentMember != null);
        }
Пример #2
0
        public void Print()
        {
            Console.Write("{ ");

            LinkedListMember currentMember = _first;

            while (currentMember != null)
            {
                Console.Write(currentMember.GetData() + " ");
                currentMember = currentMember.GetNext();
            }

            Console.WriteLine("}");
        }
Пример #3
0
        private LinkedListMember GetMember(int index)
        {
            if (index < 0 || index >= _size)
            {
                throw new IndexOutOfRangeException();
            }

            LinkedListMember targetMember = _first;

            for (int i = 0; i < index; i++)
            {
                targetMember = targetMember.GetNext();
            }

            return(targetMember);
        }
Пример #4
0
        public void Remove(int index)
        {
            if (index < 0 || index >= _size)
            {
                throw new IndexOutOfRangeException();
            }

            LinkedListMember targetMember = GetMember(index);

            LinkedListMember prev = targetMember.GetPrev();
            LinkedListMember next = targetMember.GetNext();

            prev?.SetNext(next);
            next?.SetPrev(prev);

            if (index == 0)
            {
                _first = next;
            }

            _size--;
        }
Пример #5
0
        public void Reverse()
        {
            if (_size < 2)
            {
                return;
            }

            LinkedListMember currentMember = _first;
            LinkedListMember newFirst      = null;

            while (currentMember != null)
            {
                LinkedListMember next = currentMember.GetNext();
                currentMember.SetNext(currentMember.GetPrev());
                currentMember.SetPrev(next);

                newFirst      = currentMember;
                currentMember = next;
            }

            _first = newFirst;
        }
Пример #6
0
        public void AddAs(int index, int data)
        {
            LinkedListMember newMember = new LinkedListMember(data);

            if (index < 0 || index > _size)
            {
                throw new IndexOutOfRangeException();
            }

            if (_size == 0)
            {
                _first = newMember;
                _first.SetPrev(null);
                _first.SetNext(null);
            }
            else if (index == 0)
            {
                newMember.SetNext(_first);
                _first.SetPrev(newMember);
                _first = newMember;
            }
            else
            {
                LinkedListMember addAfterMember = GetMember(index - 1);

                LinkedListMember next = addAfterMember.GetNext();

                next?.SetPrev(newMember);
                addAfterMember.SetNext(newMember);

                newMember.SetPrev(addAfterMember);
                newMember.SetNext(next);
            }

            _size++;
        }