Advertisement
jtentor

DemoStack2 - CS - Stack.cs

May 9th, 2020
1,306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 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 DemoStack2
  8. {
  9.     class Stack <Elemento>
  10.     {
  11.         private int dimension;
  12.         private Elemento[] datos;
  13.         private int cuenta;
  14.  
  15.         public Stack(int dimension = 10)
  16.         {
  17.             this.dimension = dimension;
  18.             this.datos = new Elemento[dimension];
  19.             this.cuenta = 0;
  20.         }
  21.  
  22.         public void Push(Elemento elemento)
  23.         {
  24.             if (this.cuenta >= this.dimension)
  25.             {
  26.                 throw new Exception("Error la Pila está llena ...");
  27.             }
  28.             this.datos[this.cuenta] = elemento;
  29.             ++this.cuenta;
  30.         }
  31.  
  32.         public Elemento Pop()
  33.         {
  34.             if (this.isEmpty)
  35.             {
  36.                 throw new Exception("Error la Pila está vacía ...");
  37.             }
  38.             --this.cuenta;
  39.             return this.datos[this.cuenta];
  40.         }
  41.  
  42.         public Elemento Peek()
  43.         {
  44.             if (this.isEmpty)
  45.             {
  46.                 throw new Exception("La pila está vacía ...");
  47.             }
  48.             return this.datos[this.cuenta - 1];
  49.         }
  50.  
  51.         public bool isEmpty {
  52.             get { return this.cuenta <= 0; }
  53.         }
  54.         public int Count {
  55.             get { return this.cuenta; }
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement