Advertisement
Danielos168

Odwracanie stosu

Jan 22nd, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp42
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Stack<int> s = new Stack<int>();
  14.             Random r = new Random();
  15.             for (int i = 0; i < 10; i++)
  16.             {
  17.                 s.Push(r.Next(0,100));
  18.             }
  19.  
  20.             Stack<int> s1 = OdwrocStos(s);
  21.             Console.ReadKey();
  22.         }
  23.         static Stack<int> OdwrocStos(Stack<int> s)
  24.         {
  25.             Queue<int> temp = new Queue<int>();
  26.             Console.WriteLine("Stos przed odwróceniem");
  27.             while (s.Count != 0)
  28.             {
  29.                 int tmp = s.Pop();
  30.                 temp.Enqueue(tmp);
  31.                 Console.Write(tmp + ",");
  32.             }
  33.  
  34.             Console.WriteLine();
  35.  
  36.             while (temp.Count != 0)
  37.             {
  38.                 int tmp = temp.Dequeue();
  39.                 s.Push(tmp);
  40.             }
  41.             Console.WriteLine("Po odwróceniu: ");
  42.             while (s.Count != 0)
  43.             {
  44.                 int tmp = s.Pop();
  45.                 temp.Enqueue(tmp);
  46.                 Console.Write(tmp + ",");
  47.             }
  48.             return s;
  49.  
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement