Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. class UnidirList
  2. {
  3. public UnidirPoint head;
  4. public UnidirList()
  5. {
  6. head = null;
  7. }
  8. private void makeRandomList()
  9. {
  10. Console.Clear();
  11. Console.WriteLine("Введите размер однонаправленного списка:");
  12. int size = Program.scanNatNumber();
  13. Random rand = new Random();
  14. head = new UnidirPoint(rand.Next(-100, 101));
  15. UnidirPoint curPoint = head;
  16. for (int i = 1; i < size; i++)
  17. {
  18. System.Threading.Thread.Sleep(10);
  19. UnidirPoint newPoint = new UnidirPoint(rand.Next(-100, 101));
  20. curPoint.next = newPoint;
  21. curPoint = newPoint;
  22. }
  23. }
  24. private void makeOwnList()
  25. {
  26. Console.Clear();
  27. Console.WriteLine("Введите размер однонаправленного списка:");
  28. int size = Program.scanNatNumber();
  29. Console.WriteLine("Введите голову списка:");
  30. head = new UnidirPoint(Program.scanNumber());
  31. UnidirPoint curPoint = head;
  32. if (size == 1)
  33. {
  34. return;
  35. }
  36. Console.WriteLine("Вводите элементы списка по очереди через Enter:");
  37. for (int i = 1; i < size; i++)
  38. {
  39. UnidirPoint newPoint = new UnidirPoint(Program.scanNumber());
  40. curPoint.next = newPoint;
  41. curPoint = newPoint;
  42. }
  43. }
  44. public void makeList()
  45. {
  46. Console.WriteLine(@"Как вы хотите создать однонаправленный список:
  47. 1. Вручную.
  48. 2. С помощью датчика случайных чисел.");
  49. while (true)
  50. {
  51. switch (Program.scanNumber())
  52. {
  53. case 1:
  54. makeOwnList();
  55. return;
  56. case 2:
  57. makeRandomList();
  58. return;
  59. default:
  60. Console.WriteLine("Введена некорректная команда, повторите ввод!");
  61. break;
  62. }
  63. }
  64. }
  65. private UnidirPoint findFirstEven(UnidirPoint cur)
  66. {
  67. if (cur.next == null && Math.Abs(cur.data % 2) == 1)
  68. {
  69. return null;
  70. }
  71. if (cur.next.data % 2 == 0)
  72. {
  73. return cur;
  74. }
  75. return findFirstEven(cur.next);
  76. }
  77. public void deleteEvenElements()
  78. {
  79. if (head == null)
  80. {
  81. return;
  82. }
  83. if (head.data % 2 == 0)
  84. {
  85. head = head.next;
  86. deleteEvenElements();
  87. return;
  88. }
  89. UnidirPoint found = findFirstEven(head);
  90. if (found == null)
  91. {
  92. return;
  93. }
  94. found.next = found.next.next;
  95. deleteEvenElements();
  96. }
  97. public void print(UnidirPoint cur)
  98. {
  99. Console.Write(cur);
  100. if (cur.next == null)
  101. {
  102. Console.Write("\n");
  103. return;
  104. }
  105. print(cur.next);
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement