Сделать сортировку стека методом простого выбора - C#
Формулировка задачи:
Привет всем))
Помогите пожалуйста с сортировкой стека методом простого выбора.
Стек есть а вот сам метод сортировки простым выбором не могу понять как надо его соединить со стеком =((
Вот стек
using System;
using System.Text;
namespace ConsoleApplication10
{
class Program
{
class Node
{
public int val;
public Node next;
public Node prev;
}
class Stack
{
Node root;
Node top;
public void Push(int _val)
{
if (root == null)
{
root = new Node();
root.val = _val;
top = root;
}
else
{
top.next = new Node();
top.next.val = _val;
top.next.prev = top;
top = top.next;
}
}
public int Pop(int _val)
{
int result;
if (root == null)
{
Console.WriteLine("Error: stack is empty");
}
else if (root == top)
{
result = root.val;
root = null;
return result;
}
result = top.val;
top = top.prev;
top.next = null;
return result;
}
Node ElementAt(int num)
{
if (root != null && num > -1)
{
int c = 0;
Node current = root;
while (c < num)
{
c++;
current = current.next;
}
return current;
}
return null;
}
int Count()
{
int c = 0;
if (root != null)
{
Node current = root;
while (current != top)
{
c++;
current = current.next;
}
c++;
}
return c;
}
public void Sort()
{
}
public void Print()
{
if (root == null)
{
Console.WriteLine("Stack is empty");
return;
}
Node i = root;
while (i != top)
{
Console.Write(i.val.ToString() + " ");
i = i.next;
}
Console.WriteLine(top.val.ToString());
}
}
public static void Main(string[] args)
{
Stack stack = new Stack();
stack.Push(5);
stack.Push(3);
stack.Push(7);
stack.Push(2);
stack.Push(1);
stack.Print();
stack.Sort();
stack.Print();
Console.ReadKey();
Console.ReadKey();
}
}
}Решение задачи: «Сделать сортировку стека методом простого выбора»
textual
Листинг программы
st.ToArray().ToList().Sort();