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()
- {
- long?[] arr = new long?[4];
- string[] command = Console.ReadLine().Split();
- while (command[0] != "end")
- {
- switch (command[0])
- {
- case "push":
- for (long i = 0; i < arr.Length; i++)
- {
- if (arr[i]==null)
- {
- arr[i] = long.Parse(command[1]);
- break;
- }
- if (arr[arr.Length-1] != null)
- {
- long?[] arrBuffer = new long?[arr.Length];
- Array.Copy(arr, arrBuffer, arrBuffer.Length);
- arr = new long?[arr.Length * 2];
- Array.Copy(arrBuffer, arr, arrBuffer.Length);
- }
- }
- break;
- case "pop":
- for (long i = arr.Length-1; i >= 0; i--)
- {
- if (arr[i] != null)
- {
- arr[i] = null;
- break;
- }
- }
- break;
- case "removeAt":
- long index = long.Parse(command[1]);
- arr[index] = null;
- for (long i = index; i < arr.Length-1; i++)
- {
- if (arr[i+1] != null)
- {
- arr[i] = arr[i + 1];
- }
- else
- {
- arr[i] = null;
- break;
- }
- }
- break;
- case "clear":
- for (long i = 0; i < arr.Length; i++)
- {
- arr[i] = null;
- }
- break;
- default:
- break;
- }
- command = Console.ReadLine().Split();
- }
- if (arr.All(x => x == null))
- {
- Console.WriteLine("empty array");
- return;
- }
- else
- {
- for (long i = 0; i < arr.Length; i++)
- {
- if (arr[i] != null)
- {
- Console.Write("{0} ",arr[i]);
- }
- else
- {
- break;
- }
- }
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement