emilia98

07.ResizableArray

Jun 21st, 2017
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.80 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _07.ResizableArray
  4. {
  5.     class ResizableArray
  6.     {
  7.         static void Main()
  8.         {
  9.             //Here, we have an array with given length of 4 elements
  10.             long?[] array = new long?[4];
  11.             string input = Console.ReadLine();
  12.             string[] command = new string[4];
  13.             //The index = 0, because the index of the first element of array
  14.             //is always 0. We will change the value, depending on elements which array holds
  15.             int index = 0;
  16.             //This is a variable, in which we will store the value of the index
  17.             // we will need to resize the array
  18.             // overflow = index = 3
  19.             // overflow = index = 7
  20.             // overflow = index = 11 etc.
  21.             int overflow = 3;
  22.  
  23.             while (input != "end")
  24.             {
  25.                 command = input.Split(' ');
  26.  
  27.                 if (command[0] == "push")
  28.                 {
  29.                     long number = long.Parse(command[1]);
  30.                     //The index at first is 0, so that we should increase it
  31.                     //after we've given a value of the array on the
  32.                     //current position
  33.                     //=> 1) we give a value to the array element
  34.                     //   2) we increase the index, so that we could be
  35.                     //      ready to push the next element  
  36.                     array[index] = number;
  37.                     index++;
  38.                 }
  39.                 else if (command[0] == "pop")
  40.                 {
  41.                     //If we want to pop the last element, we should decrease the index
  42.                     //by 1, because we've already increased it by one when we pushed the
  43.                     //last element, but we don't have a value on postion "index"
  44.                     // Example:
  45.                     //index = 0
  46.                     // push 1 -> index = 1
  47.                     // push 2 -> index = 2
  48.                     // On position index = 2, we don't have a value;
  49.                     // If we have a command "pop",
  50.                     // if we don't decrease the index by 1, we will pop an array element
  51.                     // which doesn't have a value
  52.                     // So that, I did this -> Search for the last element, which has a value
  53.                     // the element on position INDEX - 1;
  54.  
  55.                     array[index - 1] = null;
  56.                     index--;
  57.                 }
  58.                 else if (command[0] == "removeAt")
  59.                 {
  60.                     int toRemoveIndex = int.Parse(command[1]);
  61.                     //Here, we give the element on this position a "null" value.
  62.                     array[toRemoveIndex] = null;
  63.                     //Here, we have a loop which iterates all elements from given position  (the number
  64.                     //after command "removeAt") to the "index" (excluded)
  65.                     for (int j = toRemoveIndex; j < index; j++)
  66.                     {
  67.                         //Here, we shift the elements from the removed value from right to left
  68.                         array[j] = array[j + 1];
  69.                         array[j + 1] = null;
  70.                     }
  71.                     index--;
  72.                 }
  73.                 else if (command[0] == "clear")
  74.                 {
  75.                     for (int i = 0; i <= index; i++)
  76.                     {
  77.                         array[i] = null;
  78.                     }
  79.                     index = 0;
  80.                 }
  81.  
  82.                 if (index == overflow)
  83.                 {
  84.                     //Here:
  85.                     //1) we create a new array, which has the same size as the original array
  86.                     //and it's temporary -> we don't do any operations with its elements
  87.                     //2) we copy the elements from the original array to the new array
  88.                     //3) We change the "overflow" value with adding 4.
  89.                     //4) We change the size of the original array with the "overflow" value;
  90.                     //5) We copy the whole new array, we've previously saved, in the original array
  91.                     long?[] newArray = new long?[overflow + 1];
  92.                     Array.Copy(array, newArray, overflow + 1);
  93.                     overflow += 4;
  94.                     array = new long?[overflow + 1];
  95.                     Array.Copy(newArray, array, overflow - 3);
  96.                 }
  97.  
  98.                 input = Console.ReadLine();
  99.             }
  100.  
  101.             if (index <= 0)
  102.             {
  103.                 Console.Write("empty array");
  104.             }
  105.             else
  106.             {
  107.                 for (int i = 0; i <= index; i++)
  108.                 {
  109.                     Console.Write("{0} ", array[i]);
  110.                 }
  111.             }
  112.             Console.WriteLine();
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment