Advertisement
vovanhik_24

Task 1

Apr 8th, 2025 (edited)
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.74 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Task1
  4. {
  5.     class Program
  6.     {
  7.         static void Main()
  8.         {
  9.             Stack stack = new();
  10.  
  11.             stack.Push(10);
  12.             stack.Push(20);
  13.             stack.Push(30);
  14.  
  15.             stack.ShowStack();
  16.  
  17.             Console.WriteLine($"Верхний элемент: {stack.Top()}");
  18.  
  19.             stack.Pop();
  20.             stack.ShowStack();
  21.  
  22.             while (!stack.IsEmpty())
  23.             {
  24.                 stack.Pop();
  25.             }
  26.  
  27.             stack.Pop();
  28.         }
  29.  
  30.         public class Stack
  31.         {
  32.             private readonly int[] _stackArray;
  33.             private readonly int _capacity;
  34.             private int _topIndex;
  35.  
  36.             public Stack(int size = 10)
  37.             {
  38.                 _capacity = size;
  39.                 _stackArray = new int[_capacity];
  40.                 _topIndex = -1;
  41.             }
  42.  
  43.             public bool IsEmpty()
  44.             {
  45.                 return _topIndex == -1;
  46.             }
  47.  
  48.             public void Push(int value)
  49.             {
  50.                 if (_topIndex >= _capacity - 1)
  51.                 {
  52.                     Console.WriteLine("Ошибка: стек переполнен.");
  53.                     return;
  54.                 }
  55.  
  56.                 _topIndex++;
  57.                 _stackArray[_topIndex] = value;
  58.  
  59.                 Console.WriteLine($"Элемент {value} добавлен в стек.");
  60.             }
  61.  
  62.             public int Pop()
  63.             {
  64.                 if (IsEmpty())
  65.                 {
  66.                     Console.WriteLine("Ошибка: стек пуст. Невозможно извлечь элемент.");
  67.                     return 0;
  68.                 }
  69.  
  70.                 int value = _stackArray[_topIndex];
  71.                 _topIndex--;
  72.  
  73.                 Console.WriteLine($"Элемент {value} удалён из стека.");
  74.                 return value;
  75.             }
  76.  
  77.             public int Top()
  78.             {
  79.                 if (IsEmpty())
  80.                 {
  81.                     Console.WriteLine("Ошибка: стек пуст. Нет верхнего элемента.");
  82.                     return 0;
  83.                 }
  84.  
  85.                 return _stackArray[_topIndex];
  86.             }
  87.  
  88.             public void ShowStack()
  89.             {
  90.                 if (IsEmpty())
  91.                 {
  92.                     Console.WriteLine("Стек пуст.");
  93.                     return;
  94.                 }
  95.  
  96.                 Console.Write("Содержимое стека: ");
  97.                 for (int i = _topIndex; i >= 0; i--)
  98.                 {
  99.                     Console.Write($"{_stackArray[i]} ");
  100.                 }
  101.  
  102.                 Console.WriteLine();
  103.             }
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement