public void Add(double value)
        {
            var newStart = new NodeDouble(value);

            newStart.Next = Head;
            Head          = newStart;
        }
        NodeDouble GetEnd()
        {
            if (IsEmpty)
            {
                throw new Exception("Список пуст");
            }
            NodeDouble currentNode = Head;

            while (currentNode.Next != null)
            {
                currentNode = currentNode.Next;
            }
            return(currentNode);
        }
        public override string ToString()
        {
            if (IsEmpty)
            {
                return("Список пуст");
            }
            NodeDouble    currentNode = Head;
            StringBuilder listString  = new StringBuilder(Head.ToString());

            while (currentNode.Next != null)
            {
                currentNode = currentNode.Next;
                listString.AppendFormat($" {currentNode.ToString()}");
            }
            return(listString.ToString());
        }
        public void Add(double value, int indexForInsert)
        {
            if (indexForInsert < 1)
            {
                throw new Exception(
                          $"Позиция для вставки не может быть меньше 1.");
            }
            if (indexForInsert == 1)
            {
                NodeDouble tmp = Head;
                Head      = new NodeDouble(value);
                Head.Next = tmp;
                return;
            }
            if (IsEmpty)
            {
                throw new Exception(
                          $"Добавление элемента в позицию {indexForInsert} невозможно. Список пуст.");
            }
            if (indexForInsert > Length + 1)
            {
                throw new Exception(
                          $"Добавление элемента в позицию {indexForInsert} невозможно." +
                          $" Длина списка {Length}.");
            }
            NodeDouble currentNode  = Head;
            int        currentIndex = 1;

            while (currentNode.Next != null && currentIndex < indexForInsert - 1)
            {
                currentNode = currentNode.Next;
                currentIndex++;
            }
            if (currentIndex == indexForInsert - 1)
            {
                NodeDouble tail = currentNode.Next;
                currentNode.Next      = new NodeDouble(value);
                currentNode.Next.Next = tail;
            }
            else
            {
                throw new Exception(
                          $"Добавление элемента в позицию {indexForInsert} невозможно.");
            }
        }
Example #5
0
        private static void InsertZeros(LinkedListDouble list)
        {
            if (list.Head == null)
            {
                return;
            }
            NodeDouble currentNode = list.Head;

            do
            {
                if (currentNode.Value < 0)
                {
                    NodeDouble tmp = currentNode.Next;
                    currentNode.Next      = new NodeDouble(0);
                    currentNode.Next.Next = tmp;
                    currentNode           = currentNode.Next;
                }
                else
                {
                    currentNode = currentNode.Next;
                }
            } while (currentNode != null);
        }
        public double Extract(int position)
        {
            if (position < 1)
            {
                throw new Exception(
                          $"Позиция для извлечения не может быть меньше 1.");
            }
            if (IsEmpty)
            {
                throw new Exception(
                          $"Извлечение элемента в невозможно. Список пуст.");
            }
            if (position == 1)
            {
                return(this.Top());
            }
            NodeDouble currentNode  = Head;
            int        currentIndex = 1;

            while (currentNode.Next != null && currentIndex < position - 1)
            {
                currentNode = currentNode.Next;
                currentIndex++;
            }
            if (currentIndex == position - 1 && currentNode.Next != null)
            {
                double value = currentNode.Next.Value;
                currentNode.Next = currentNode.Next.Next;
                return(value);
            }
            else
            {
                throw new Exception(
                          $"Извлечение элемента в невозможно.");
            }
        }
        public double Bottom()
        {
            if (IsEmpty)
            {
                throw new Exception(
                          $"Извлечение последнего элемента невозможно. Список пуст.");
            }
            if (IsEmpty)
            {
                double value = Head.Value;
                Head = null;
                return(value);
            }
            NodeDouble currentNode  = Head;
            NodeDouble previousNode = null;

            while (currentNode.Next != null)
            {
                previousNode = currentNode;
                currentNode  = currentNode.Next;
            }
            previousNode.Next = null;
            return(currentNode.Value);
        }
Example #8
0
 public NodeDouble(double value)
 {
     Value = value;
     Next  = null;
 }
Example #9
0
 public NodeDouble()
 {
     Value = 0;
     Next  = null;
 }
        public void AppEnd(double value)
        {
            NodeDouble endOfList = GetEnd();

            endOfList.Next = new NodeDouble(value);
        }
 public LinkedListDouble(double value)
 {
     Head = new NodeDouble(value);
 }