Advertisement
Vladimir76

Resizable Array

Mar 7th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 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 Resizable_Array
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             long?[] arr = new long?[4];
  14.             string[] command = Console.ReadLine().Split();
  15.  
  16.             while (command[0] != "end")
  17.             {
  18.                 switch (command[0])
  19.                 {
  20.                     case "push":
  21.                         for (long i = 0; i < arr.Length; i++)
  22.                         {
  23.                             if (arr[i]==null)
  24.                             {
  25.                                 arr[i] = long.Parse(command[1]);
  26.                                 break;
  27.                             }
  28.                             if (arr[arr.Length-1] != null)
  29.                             {
  30.                                 long?[] arrBuffer = new long?[arr.Length];
  31.                                 Array.Copy(arr, arrBuffer, arrBuffer.Length);
  32.                                 arr = new long?[arr.Length * 2];
  33.                                 Array.Copy(arrBuffer, arr, arrBuffer.Length);                                
  34.                             }
  35.                         }
  36.                         break;
  37.                     case "pop":
  38.                         for (long i = arr.Length-1; i >= 0; i--)
  39.                         {
  40.                             if (arr[i] != null)
  41.                             {
  42.                                 arr[i] = null;
  43.                                 break;
  44.                             }
  45.                         }
  46.                         break;
  47.                     case "removeAt":
  48.                         long index = long.Parse(command[1]);
  49.                         arr[index] = null;
  50.                         for (long i = index; i < arr.Length-1; i++)
  51.                         {
  52.                             if (arr[i+1] != null)
  53.                             {
  54.                                 arr[i] = arr[i + 1];
  55.                             }
  56.                             else
  57.                             {
  58.                                 arr[i] = null;
  59.                                 break;
  60.                             }
  61.                         }
  62.                         break;
  63.                     case "clear":
  64.                         for (long i = 0; i < arr.Length; i++)
  65.                         {
  66.                             arr[i] = null;
  67.                         }
  68.                         break;
  69.                     default:
  70.                         break;
  71.                 }
  72.                 command = Console.ReadLine().Split();
  73.             }
  74.             if (arr.All(x => x == null))
  75.             {
  76.                 Console.WriteLine("empty array");
  77.                 return;
  78.             }
  79.             else
  80.             {
  81.                 for (long i = 0; i < arr.Length; i++)
  82.                 {
  83.                     if (arr[i] != null)
  84.                     {
  85.                         Console.Write("{0} ",arr[i]);
  86.                     }
  87.                     else
  88.                     {
  89.                         break;
  90.                     }
  91.                 }
  92.             }
  93.             Console.WriteLine();
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement