StevanovicMilan

Zadatak 1 - Generic

Oct 31st, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 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_04_2017
  8. {
  9.     class GenericStack<T>
  10.     {
  11.         private T[] a;
  12.         private int count;
  13.  
  14.         public GenericStack()
  15.         {
  16.             a = new T[10];
  17.             count = 0;
  18.         }
  19.  
  20.         public bool IsEmpty
  21.         {
  22.             get { return count == 0; }
  23.         }
  24.  
  25.         public void Push(T x)
  26.         {
  27.             if (count > a.Length)
  28.                 throw new InvalidOperationException("Ju kenot du dat, d stek iz ful");
  29.             a[count] = x;
  30.             count++;
  31.         }
  32.  
  33.         public T Pop ()
  34.         {
  35.             if (IsEmpty)
  36.                 throw new InvalidOperationException("Stek iz empti");
  37.             count--;
  38.             return a[count];
  39.         }
  40.  
  41.         public override string ToString()
  42.         {
  43.             StringBuilder sb = new StringBuilder();
  44.            
  45.             for (int i = count - 1; i >= 0; i--)
  46.             {
  47.                 sb.AppendFormat("{0} ", a[i]);
  48.             }
  49.  
  50.             return sb.ToString();
  51.         }
  52.     }
  53. }
Add Comment
Please, Sign In to add comment