sábado, 15 de junio de 2013

COLA SIMPLE (VISUAL)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cola_Simple
{
    class Colas
    {
        const int MAX = 10;
        int p, u;
        int[] vec;

        public Colas(int tam)
        {
            tam = MAX;
            vec = new int[tam];
        }
      

        public void Cola()
        {
            p = u - 1;

        }
        public bool esta_llena()
        {
            if (u >= MAX - 1)
                return true;
            return false;
        }
        public bool esta_vacia()
        {
            if (p == -1)
                return true;
            return false;
        }
        public bool agregar(int dato)
        {
            if (!esta_llena())
            {
                vec[++u] = dato;
                if (u == 0)
                    p = 0;
                return true;
            }
            return false;
        }
        public bool extraer(int dato)
        {
            if (!esta_vacia())
            {
                dato = vec[p];
                if (p == u)
                {
                    p = -1;
                    u = p;
                }
                else
                    p++;
                return true;
            }
            return false;
        }
    }
}

===============================================================
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 Cola_Simple
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Colas cola = new Colas(5);
            int i;
            textBox1.Text = "Desbordamiento:";
            for (i = 0; i < 5; i++)
                if (cola.agregar(+i + 5))

                    textBox1.Text += "\n\r" + ("\n\r" + i + 5).ToString();
                else
                    textBox1.Text = "La cola esta llena";


            //------------------------------------------------------------------------------------

            int d = 0;
            textBox2.Text =  "Extrallendo Datos de Cola";
            while (true)
            {
                if (cola.extraer(d + 5))
                {
                    textBox2.Text += "\n\r" + ("\n\r" + d + 5).ToString();
                    d++;
                }
                else
                {
                    //textBox2.Text = "Subdesbordamiento";
                    break;

                }
            }
        }

    }

}

No hay comentarios:

Publicar un comentario