Advertisement
Guest User

Untitled

a guest
Mar 16th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApp1
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Iterate(CreateQueue(), "queue");
  11.             Iterate(CreateStack(), "stack");
  12.  
  13.             Console.ReadKey();
  14.         }
  15.  
  16.         private static IEnumerable<int> CreateQueue()
  17.         {
  18.             var collection = new Queue<int>();
  19.             for (int i = 0; i < 4; i++)
  20.                 collection.Enqueue(i);
  21.             return collection;
  22.         }
  23.  
  24.         private static IEnumerable<int> CreateStack()
  25.         {
  26.             var collection = new Stack<int>();
  27.             for (int i = 0; i < 4; i++)
  28.                 collection.Push(i);
  29.             return collection;
  30.         }
  31.  
  32.         private static void Iterate(IEnumerable<int> collection, string name)
  33.         {
  34.             Console.Write($"{name} foreach: ");
  35.             foreach (var entry in collection)
  36.                 Console.Write($"{entry} ");
  37.             Console.WriteLine();
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement