Очередь queue на двух стеках. C C++ в C#
Формулировка задачи:
Может кто перевести на С#?
#include <iostream>
using std::cout;
using std::endl;
#include <stack>
using std::stack;
class queue
{
stack<int> s1;
stack<int> s2;
public:
void push(int n)
{
s1.push(n);
}
void pop()
{
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
s2.pop();
//добавляем элементы обратно
while(!s2.empty())
{
s1.push(s2.top());
s2.pop();
}
}
void print()
{
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
//вывод элементов
while(!s2.empty())
{
cout <<s2.top()<<' ';
s1.push(s2.top());
s2.pop();
}
}
};
int main()
{
queue q;
q.push(1);
q.push(2);
q.push(3);
q.print();
cout <<endl;
//удаляем элемент очереди
q.pop();
q.print();
cout <<endl;
//удаляем элемент очереди
q.pop();
q.print();
cout <<endl;
return 0;
}
Что не кто не может помочь с переводом на с#? Так сложно помочь?Я просто не знаю с++.
Решение задачи: «Очередь queue на двух стеках. C C++ в C#»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Text;
class MyQueue
{
private Stack<int> stack = new Stack<int>();
private Stack<int> temp = new Stack<int>();
public void Push(int Value)
{
stack.Push(Value);
}
public int Pop()
{
while (stack.Count != 0)
temp.Push(stack.Pop());
int Value = temp.Pop();
while (temp.Count != 0)
stack.Push(temp.Pop());
return Value;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
while (stack.Count != 0)
temp.Push(stack.Pop());
while (temp.Count != 0)
{
sb.Append(temp.Peek());
sb.Append(" ");
stack.Push(temp.Pop());
}
return sb.ToString();
}
}
class Program
{
static void Main()
{
MyQueue q = new MyQueue();
q.Push(1);
q.Push(2);
q.Push(3);
Console.WriteLine("{0,-10} - исходная очередь", q);
Console.WriteLine("{1,-10} - После удаления {0}", q.Pop(), q);
Console.WriteLine("{1,-10} - После удаления {0}", q.Pop(), q);
Console.ReadLine();
}
}