cola utilizando lista enlazada

class Elemento
    {
        public int val;
        public Elemento next;
        public Elemento(int i)
        {
            val = i;
            next = null;
        }
        public Elemento()
        {
            next = null;
        }
    }
    class Est_de_datos
    {
        Elemento first, last;
        public Est_de_datos()
        {
        }
        public void Agregar(int dato)
        {
            Elemento nuevo;
            nuevo = new Elemento(dato);
            if (last != null)
                last.next = nuevo;
            last = nuevo;
            if (first == null)
                first = nuevo;
        }
        public int Eliminar()
        {
            Elemento temp = new Elemento();
            int d;
            temp = first;
            if (temp == null)
                return 0;
            first = temp.next;
            d = temp.val;
            if (first == null)
                last = null;
            return d;
        }
    }
}
//--------------------------------------------------------------------------------
 public partial class Form1 : Form
    {
        Est_de_datos dato = new Est_de_datos();
        int n = 0;
        int cont = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Random r = new Random();
            if (cont < 5)
            {
                n = r.Next(1, 20);
                label2.Text = n.ToString();
                dato.Agregar(n);

                cont++;

            }
            else
            {
                MessageBox.Show("Se terminaron los datos");
                button1.Enabled = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (cont > 0)
            {

                label2.Text = dato.Eliminar().ToString();

                cont--;
            }
            else
            {
                MessageBox.Show("Datos disponibles");

            }
        }
    }
}

No hay comentarios:

Publicar un comentario