Advertisement
grubcho

Resizable array

Jun 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.20 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(string[] args)
  12.         {
  13.             int?[] array = new int?[4];
  14.             int index = 0;
  15.             int overflow = 3;
  16.             string[] command = Console.ReadLine().Split(' ').ToArray();
  17.             while (command [0] != "end")
  18.             {
  19.                 if (command[0] == "push")
  20.                 {
  21.                     array[index] = int.Parse(command[1]);
  22.                     index++;
  23.                 }
  24.                 else if (command [0] == "pop")
  25.                 {
  26.                     array[index-1] = null;
  27.                     index--;
  28.                 }
  29.                 else if (command[0] == "removeAt")
  30.                 {
  31.                     int removeIndex = int.Parse(command[1]);
  32.                     for (int i = removeIndex; i < index; i++)
  33.                     {
  34.                         array[i] = array[i + 1];
  35.                         array[i + 1] = null;
  36.                     }
  37.                     index--;
  38.                 }
  39.                 else if (command[0] == "clear")
  40.                 {
  41.                     for (int i = 0; i < index; i++)
  42.                     {
  43.                         array[i] = null;                        
  44.                     }
  45.                     index = 0;
  46.                 }
  47.                 if (index == overflow)
  48.                 {
  49.                     int?[] tempArray = new int?[overflow + 1];
  50.                     Array.Copy(array, tempArray, overflow + 1);
  51.                     overflow += 4;
  52.                     array = new int?[overflow + 1];
  53.                     Array.Copy(tempArray, array, overflow - 3);
  54.                 }
  55.                 command = Console.ReadLine().Split(' ').ToArray();
  56.             }
  57.             if (index <= 0)
  58.             {
  59.                 Console.Write("empty array");
  60.             }
  61.             else
  62.             {
  63.                 for (int i = 0; i <= index; i++)
  64.                 {
  65.                     Console.Write("{0} ", array[i]);
  66.                 }
  67.             }
  68.             Console.WriteLine();
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement