Сделать сортировку стека методом простого выбора - C#

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

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

Привет всем)) Помогите пожалуйста с сортировкой стека методом простого выбора. Стек есть а вот сам метод сортировки простым выбором не могу понять как надо его соединить со стеком =(( Вот стек
Листинг программы
  1. using System;
  2. using System.Text;
  3. namespace ConsoleApplication10
  4. {
  5. class Program
  6. {
  7. class Node
  8. {
  9. public int val;
  10. public Node next;
  11. public Node prev;
  12. }
  13. class Stack
  14. {
  15. Node root;
  16. Node top;
  17. public void Push(int _val)
  18. {
  19. if (root == null)
  20. {
  21. root = new Node();
  22. root.val = _val;
  23. top = root;
  24. }
  25. else
  26. {
  27. top.next = new Node();
  28. top.next.val = _val;
  29. top.next.prev = top;
  30. top = top.next;
  31. }
  32. }
  33. public int Pop(int _val)
  34. {
  35. int result;
  36. if (root == null)
  37. {
  38. Console.WriteLine("Error: stack is empty");
  39. }
  40. else if (root == top)
  41. {
  42. result = root.val;
  43. root = null;
  44. return result;
  45. }
  46. result = top.val;
  47. top = top.prev;
  48. top.next = null;
  49. return result;
  50. }
  51. Node ElementAt(int num)
  52. {
  53. if (root != null && num > -1)
  54. {
  55. int c = 0;
  56. Node current = root;
  57. while (c < num)
  58. {
  59. c++;
  60. current = current.next;
  61. }
  62. return current;
  63. }
  64. return null;
  65. }
  66. int Count()
  67. {
  68. int c = 0;
  69. if (root != null)
  70. {
  71. Node current = root;
  72. while (current != top)
  73. {
  74. c++;
  75. current = current.next;
  76. }
  77. c++;
  78. }
  79. return c;
  80. }
  81. public void Sort()
  82. {
  83. }
  84.  
  85. public void Print()
  86. {
  87. if (root == null)
  88. {
  89. Console.WriteLine("Stack is empty");
  90. return;
  91. }
  92. Node i = root;
  93. while (i != top)
  94. {
  95. Console.Write(i.val.ToString() + " ");
  96. i = i.next;
  97. }
  98. Console.WriteLine(top.val.ToString());
  99. }
  100. }
  101. public static void Main(string[] args)
  102. {
  103. Stack stack = new Stack();
  104. stack.Push(5);
  105. stack.Push(3);
  106. stack.Push(7);
  107. stack.Push(2);
  108. stack.Push(1);
  109. stack.Print();
  110. stack.Sort();
  111. stack.Print();
  112. Console.ReadKey();
  113. Console.ReadKey();
  114. }
  115. }
  116. }

Решение задачи: «Сделать сортировку стека методом простого выбора»

textual
Листинг программы
  1. st.ToArray().ToList().Sort();

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


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

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

8   голосов , оценка 3.5 из 5

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

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

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