Advertisement
damarijsilva

Queue.cs

Sep 29th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace TP3
  7. {
  8.     public class Queue<E> //cola circular priorizando memoria
  9.     {
  10.         private E[] datos;
  11.         private int head;
  12.         private int tail;
  13.         private int cuenta; //contador de elementos
  14.  
  15.         public int Cuenta
  16.         {
  17.             get{return this.cuenta;}
  18.             protected set
  19.             {
  20.                 this.cuenta = value;
  21.             }
  22.         }
  23.        
  24.         public bool Empty
  25.         {
  26.             get { return this.cuenta <= 0; }
  27.         }
  28.  
  29.         public Queue(int capacidad)
  30.         {
  31.             this.datos = new E[capacidad];
  32.             this.head = this.tail = 0;
  33.             this.cuenta = 0;
  34.         }
  35.  
  36.         private int Next(int pos)
  37.         {
  38.             return (++pos > this.datos.Length) ? 0 : pos;
  39.         }
  40.  
  41.         /*
  42.          *tail indica el lugar donde agregar el elemento
  43.          *se agrega el elemento en el contenedor
  44.          *luego se pasa a la siguiente posicion
  45.          *finalmente se incrementa la cuenta
  46.          */
  47.  
  48.         public void Enqueue(E elemento)
  49.         {
  50.             if (this.cuenta >= this.datos.Length)
  51.             {
  52.                 throw new Exception("Error la cola esta llena.");
  53.             }
  54.             this.datos[this.tail] = elemento;
  55.             this.tail = this.Next(this.tail);
  56.             ++this.cuenta;
  57.         }
  58.  
  59.         /*
  60.          * head indica el proximo elemento a extraer
  61.          * se toma el elemento a extraer
  62.          * luego se pasa a la siguiente posicion
  63.          * se decrementa la cuenta
  64.          * finalmente se devuelve el elemento
  65.         */
  66.  
  67.         public E Dequeue()
  68.         {
  69.             if (this.Empty)
  70.             {
  71.                 throw new Exception("Error la cola esta vacia..");
  72.             }
  73.             E temp = this.datos[this.head];
  74.             this.head = this.Next(this.head);
  75.             --this.cuenta;
  76.             return temp;
  77.         }
  78.  
  79.         public E Peek()
  80.         {
  81.             if (this.Empty)
  82.             {
  83.                 throw new Exception("Error la cola esta vacia..");
  84.             }
  85.             return this.datos[this.head];
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement