Не рекурсивный метод добавления нового элемента n-ым в список - C#

Узнай цену своей работы

Формулировка задачи:

Ничего не работает. И вообще не уверен в правильности. Не понимаю, как вставлять, вместо чего.
Листинг программы
  1. using System;
  2. namespace Лаба_2
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. RList list = new RList(1, null);
  9. list = new RList(2, list);
  10. list = new RList(3, list);
  11. list.AddElByIndex(4, 4);
  12. do
  13. {
  14. Console.WriteLine(list.info);
  15. list = list.next;
  16. }
  17. while (list != null);
  18. }
  19. }
  20. class RList
  21. {
  22. public int info;
  23. public RList next;
  24. public RList(int i, RList n)
  25. {
  26. info = i;
  27. next = n;
  28. }
  29. public void AddElByIndex(int a, int index)
  30. {
  31. int count = 0;
  32. RList el = this;
  33. while (el.next != null)
  34. {
  35. count++;
  36. if (count == index)
  37. {
  38. el = new RList(a, null);
  39. break;
  40. }
  41. el = el.next;
  42. }
  43. }
  44. }
  45. }

Решение задачи: «Не рекурсивный метод добавления нового элемента n-ым в список»

textual
Листинг программы
  1. class Program
  2. {
  3.     static void Main()
  4.     {
  5.         var list = new RList();
  6.         list.Add(11);
  7.         list.Add(13);
  8.         list.Add(15);
  9.         Console.Write("после add:    ");
  10.         Console.WriteLine(list);
  11.         list.Insert(0, 10);
  12.         list.Insert(2, 12);
  13.         list.Insert(4, 14);
  14.         Console.Write("после insert: ");
  15.         Console.WriteLine(list);
  16.         Console.ReadLine();
  17.     }
  18. }
  19.  
  20. class RListItem
  21. {
  22.     public int Value;
  23.     public RListItem Next;
  24.  
  25.     public RListItem(int Value = 0, RListItem Next = null)
  26.     {
  27.         this.Value = Value;
  28.         this.Next = Next;
  29.     }
  30. }
  31.  
  32. class RList
  33. {
  34.     private RListItem Root = new RListItem();
  35.  
  36.     // Добавляет в конец
  37.     public void Add(int Value)
  38.     {
  39.         Insert(int.MaxValue, Value);
  40.     }
  41.  
  42.     // Вставляет перед элементом под номером Index
  43.     public void Insert(int Index, int Value)
  44.     {
  45.         var node = Root;
  46.         while (Index-- > 0 && node.Next != null)
  47.             node = node.Next;
  48.         node.Next = new RListItem(Value, node.Next);
  49.     }
  50.  
  51.     public override string ToString()
  52.     {
  53.         var sb = new StringBuilder();
  54.         var node = Root.Next;
  55.         while (node != null)
  56.         {
  57.             sb.Append(node.Value);
  58.             sb.Append(" ");
  59.             node = node.Next;
  60.         }
  61.  
  62.         return sb.ToString();
  63.     }
  64. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 3.333 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы