StevanovicMilan

Zadatak 1

Oct 31st, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Vezba_03_2017
  8. {
  9.      class StackA
  10.     {
  11.         private int[] _items;
  12.         private int _count;
  13.  
  14.         public StackA()
  15.         {
  16.             _items = new int[5];
  17.             _count = 0;
  18.         }
  19.  
  20.         public bool IsEmpty
  21.         {
  22.             get { return _count == 0; }
  23.         }
  24.  
  25.         public void Push(int x)
  26.         {
  27.             if (_count == _items.Length)
  28.             {
  29.                 int[] newItems = new int[_items.Length * 2];
  30.                 for (int i = 0; i < _items.Length; i++)
  31.                     newItems[i] = _items[i];
  32.                 _items = newItems;
  33.             }
  34.                
  35.             _items[_count] = x;
  36.             _count++;
  37.         }
  38.  
  39.         public int Pop()
  40.         {
  41.             if (_count == 0)
  42.                 throw new InvalidOperationException("The stack is empty.");
  43.  
  44.             if (_count <= _items.Length / 4)
  45.             {
  46.                 int[] newItems = new int[_items.Length / 2];
  47.                 for (int i = 0; i < newItems.Length; i++)
  48.                     newItems[i] = _items[i];
  49.                 _items = newItems;
  50.             }
  51.  
  52.             _count--;
  53.             return _items[_count];
  54.            
  55.         }
  56.  
  57.         public override string ToString()
  58.         {
  59.             StringBuilder sb = new StringBuilder();
  60.  
  61.             for (int i = _count - 1; i >= 0; i--)
  62.             {
  63.                 sb.AppendFormat("{0} ", _items[i]);
  64.             }
  65.  
  66.             return sb.ToString();
  67.         }
  68.  
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment