Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Resizable_array
- {
- class Program
- {
- static void Main(string[] args)
- {
- int?[] array = new int?[4];
- int index = 0;
- int overflow = 3;
- string[] command = Console.ReadLine().Split(' ').ToArray();
- while (command [0] != "end")
- {
- if (command[0] == "push")
- {
- array[index] = int.Parse(command[1]);
- index++;
- }
- else if (command [0] == "pop")
- {
- array[index-1] = null;
- index--;
- }
- else if (command[0] == "removeAt")
- {
- int removeIndex = int.Parse(command[1]);
- for (int i = removeIndex; i < index; i++)
- {
- array[i] = array[i + 1];
- array[i + 1] = null;
- }
- index--;
- }
- else if (command[0] == "clear")
- {
- for (int i = 0; i < index; i++)
- {
- array[i] = null;
- }
- index = 0;
- }
- if (index == overflow)
- {
- int?[] tempArray = new int?[overflow + 1];
- Array.Copy(array, tempArray, overflow + 1);
- overflow += 4;
- array = new int?[overflow + 1];
- Array.Copy(tempArray, array, overflow - 3);
- }
- command = Console.ReadLine().Split(' ').ToArray();
- }
- if (index <= 0)
- {
- Console.Write("empty array");
- }
- else
- {
- for (int i = 0; i <= index; i++)
- {
- Console.Write("{0} ", array[i]);
- }
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement