#9 C# Colas


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

namespace Colas
{
    class Program
    {
        static void Main(string[] args)
        {
            
            int i, n, dato = 0;
            Console.Write("¿CUANTOS ELEMENTOS? ");
            n = Convert.ToInt32(Console.ReadLine());
            Cola cola = new Cola(n);
            Random r = new Random();
            Console.WriteLine("\nINCERTANDO EN LA COLA");
            for (i = 0; i < n; i++)
            {
                dato = r.Next(100);
                if (cola.agregar(dato))
                    Console.WriteLine("Dato agregado: " + dato);
                else
                {
                    Console.WriteLine("Desbordamiento, cola llena");
                }
            }
            Console.WriteLine("\nEXTRAYENDO DATOS DE LA COLA");
            while (cola.extraer(ref dato))
            {
                Console.WriteLine("Dato extraido: " + dato);
            }
            Console.ReadKey();
        }
    }

    class Cola
    {
        //ATRIBUTOS
        int[] vec;
        int p, u, tam;

        //CONSTRUCTOR
        public Cola(int n)
        {
            p = u = -1;
            tam = n;
            vec = new int[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(int dato)
        {
            if (!esta_llena())
            {
                vec[++u] = dato;
                if (u == 0)
                    p = 0;
                return true;
            }
            return false;
        }

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

No hay comentarios:

Publicar un comentario