using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
namespace
pilas_push_pop
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
Pila
pil = new Pila(10);
public void Mostrar()
{
dataGridView1.Rows.Clear();
if
(pil.estaVacia())
return;
int
r = pil.Max - 1;
for
(int i = 0; i <= pil.Tope; i++)
{
dataGridView1.RowCount =
pil.Max;
//dataGridView1[0,
r].Value = i;
dataGridView1[0, r].Value =
pil[i];
r--;
}
}
private
void button1_Click(object
sender, EventArgs e)
{
int
dato;
if
(sender == button1)
{
try
{
dato = Convert.ToInt32(numericUpDown1.Value);
}
catch
{
MessageBox.Show("Teclee un dato entero por favor...");
return;
}
if
(pil.llena==true)
{
MessageBox.Show("Pila llena
** overflow** ");
return;
}
if
(pil.Push(dato))
{
if (dato >= 0)
{
Mostrar();
}
}
else
MessageBox.Show("No se pudo poner el dato");
}
else
if (sender == button2)
{
if
(pil.vacia==true)
{
MessageBox.Show("La pila esta vacía,
**Underflow** ");
return;
}
pil.Pop();
Mostrar();
}
}
}
}
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
class Pila
{
//ATRIBUTOS
//ARREGLO QUE GUARDA ENTEROS
private int[] vec = null;
//SE DECLARA UNA VARIABLE TOPE
private int tope;
public bool vacia, llena;
//CONSTRUCTORES
public
Pila()
{
vec = new
int[5];
tope = -1;
}
public
Pila(int n)
{
if
(n < 1)
n = 5;
vec = new
int[n];
tope = -1;
}
//Indexador
public int this[int index]
{
get
{ return vec[index]; }
}
//PROPIEDADES
public int Max
{
get
{ return vec.Length; }
}
public int Tope
{
get
{ return tope; }
}
public bool estaVacia()
{
return
tope == -1;
}
public bool
estaLlena()
{
return
tope == vec.Length - 1;
}
public bool Push(int valor)
{
vacia = false;
if
(tope == Max)
llena = true;
tope++;
vec[tope] = valor;
return true;
}
public int Pop()
{
if
(tope == 0)
vacia = true;
llena = false;
return
vec[tope--];
}
}
No hay comentarios:
Publicar un comentario