Очередь queue на двух стеках. C C++ в C#

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

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

Может кто перевести на С#?
Листинг программы
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4. #include <stack>
  5. using std::stack;
  6. class queue
  7. {
  8. stack<int> s1;
  9. stack<int> s2;
  10. public:
  11. void push(int n)
  12. {
  13. s1.push(n);
  14. }
  15. void pop()
  16. {
  17. while(!s1.empty())
  18. {
  19. s2.push(s1.top());
  20. s1.pop();
  21. }
  22. s2.pop();
  23. //добавляем элементы обратно
  24. while(!s2.empty())
  25. {
  26. s1.push(s2.top());
  27. s2.pop();
  28. }
  29. }
  30. void print()
  31. {
  32. while(!s1.empty())
  33. {
  34. s2.push(s1.top());
  35. s1.pop();
  36. }
  37. //вывод элементов
  38. while(!s2.empty())
  39. {
  40. cout <<s2.top()<<' ';
  41. s1.push(s2.top());
  42. s2.pop();
  43. }
  44. }
  45. };
  46. int main()
  47. {
  48. queue q;
  49. q.push(1);
  50. q.push(2);
  51. q.push(3);
  52. q.print();
  53. cout <<endl;
  54. //удаляем элемент очереди
  55. q.pop();
  56. q.print();
  57. cout <<endl;
  58. //удаляем элемент очереди
  59. q.pop();
  60. q.print();
  61. cout <<endl;
  62. return 0;
  63. }
Что не кто не может помочь с переводом на с#? Так сложно помочь?Я просто не знаю с++.

Решение задачи: «Очередь queue на двух стеках. C C++ в C#»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. class MyQueue
  6. {
  7.     private Stack<int> stack = new Stack<int>();
  8.     private Stack<int> temp = new Stack<int>();
  9.  
  10.     public void Push(int Value)
  11.     {
  12.         stack.Push(Value);
  13.     }
  14.  
  15.     public int Pop()
  16.     {
  17.         while (stack.Count != 0)
  18.             temp.Push(stack.Pop());
  19.  
  20.         int Value = temp.Pop();
  21.  
  22.         while (temp.Count != 0)
  23.             stack.Push(temp.Pop());
  24.  
  25.         return Value;
  26.     }
  27.  
  28.     public override string ToString()
  29.     {
  30.         StringBuilder sb = new StringBuilder();
  31.         while (stack.Count != 0)
  32.             temp.Push(stack.Pop());
  33.  
  34.         while (temp.Count != 0)
  35.         {
  36.             sb.Append(temp.Peek());
  37.             sb.Append(" ");
  38.             stack.Push(temp.Pop());
  39.         }
  40.  
  41.         return sb.ToString();
  42.     }
  43. }
  44.  
  45. class Program
  46. {
  47.     static void Main()
  48.     {
  49.         MyQueue q = new MyQueue();
  50.         q.Push(1);
  51.         q.Push(2);
  52.         q.Push(3);
  53.  
  54.         Console.WriteLine("{0,-10} - исходная очередь", q);
  55.         Console.WriteLine("{1,-10} - После удаления {0}", q.Pop(), q);
  56.         Console.WriteLine("{1,-10} - После удаления {0}", q.Pop(), q);
  57.  
  58.         Console.ReadLine();
  59.     }
  60. }

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


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

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

13   голосов , оценка 4.231 из 5

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

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

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