Advertisement
BeloMaximka

Belov_HW_CS5

Feb 7th, 2022
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.31 KB | None | 0 0
  1. using System;
  2.  
  3. namespace classwork
  4. {
  5.     class MyStack
  6.     {
  7.         private int[] array;
  8.         private int index;
  9.         public MyStack(int size = 64)
  10.         {
  11.             array = new int[size];
  12.             index = 0;
  13.         }
  14.         public bool isOverflow()
  15.         {
  16.             return array.Length == index;
  17.         }
  18.         public bool isEmpty()
  19.         {
  20.             return index == 0;
  21.         }
  22.         public int Size
  23.         {
  24.             get
  25.             {
  26.                 return index;
  27.             }
  28.         }
  29.         public int MaxSize
  30.         {
  31.             get
  32.             {
  33.                 return array.Length;
  34.             }
  35.         }
  36.         public void MaxCount(out int count)
  37.         {
  38.             count = 0;
  39.             int max = array[0];
  40.             for (int i = 0; i < index; i++)
  41.             {
  42.                 if (max < array[i])
  43.                 {
  44.                     max = array[i];
  45.                     count = 1;
  46.                 }
  47.                 else if (max == array[i])
  48.                     count++;
  49.             }
  50.         }
  51.         public void MinCount(out int count)
  52.         {
  53.             count = 0;
  54.             int min = array[0];
  55.             for (int i = 0; i < index; i++)
  56.             {
  57.                 if (min > array[i])
  58.                 {
  59.                     min = array[i];
  60.                     count = 1;
  61.                 }
  62.                 else if (min == array[i])
  63.                     count++;
  64.             }
  65.         }
  66.         public void PushBack(params int[] values)
  67.         {
  68.             for (int i = 0; i < values.Length && index != array.Length; i++)
  69.                 array[index++] = values[i];
  70.         }
  71.         public int? PopBack()
  72.         {
  73.             if (index != 0)
  74.                 return array[--index];
  75.             return null;
  76.         }
  77.         public int[] PopBack(int count) // извлечение элементов по номерам противоречит идеологии стека
  78.         {
  79.             int trueSize = 0;
  80.             int[] result = new int[count];
  81.             for (int i = 0; i < count && index != 0; i++)
  82.             {
  83.                 result[i] = array[--index];
  84.                 trueSize++;
  85.             }
  86.             Array.Resize(ref result, trueSize);
  87.             return result;
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement