#12 C# Cola simple plantilla


namespace cola_simple_plantilla
{
    class Cola
    {
        T[] vec;
        int p;
        int u;
        int tam;

        public Cola(int n)
        {
            p = u = -1;
            tam = n;
            vec = new T[tam];
        }
        public bool esta_llena()
        {
            if (u >= tam - 1)
                return true;
            return false;
        }
        public bool esta_vacia()
        {
            if (p == -1)
                return true;
            return false;
        }
        public bool Agregar(T dato)
        {
            if (!esta_llena())
            {
                vec[++u] = dato;
                if (u == 0)
                    p = 0;
                return true;
            }
            return false;
        }
        public bool extraer(ref T dato, T n)
        {
            if (!esta_vacia())
            {
                dato = vec[p];
                vec[p] = n;
                if (p == u)
                {
                    p = -1;
                    u = p;
                }
                else
                    p++;
                return true;
            }
            return false;
        }
        public void mostrar(DataGridView dgv)
        {
            dgv.ColumnCount = tam;
            for (int i = 0; i < tam; i++)
            {
                dgv[i, 0].Value = vec[i];
            }
        }
    }
}

namespace cola_simple_plantilla
{
    public partial class Form1 : Form
    {
        Cola cola;
        int i;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int n = Convert.ToInt32(textBox1.Text);
            cola = new Cola(n);
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int num = Convert.ToInt32(textBox2.Text);
            if(cola.Agregar(num))
            {
                cola.mostrar(dataGridView1);
            }
            else
            {
                MessageBox.Show("Cola llena");
                i++;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int dato = 0;
            if(cola.extraer(ref dato,0))
            {
                label1.Text = dato.ToString();
                cola.mostrar(dataGridView1);
            }
            else
            {
                MessageBox.Show("Cola vacia");
            }
        }
    }
}

No hay comentarios:

Publicar un comentario