Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace TP3
- {
- public class Queue<E> //cola circular priorizando memoria
- {
- private E[] datos;
- private int head;
- private int tail;
- private int cuenta; //contador de elementos
- public int Cuenta
- {
- get{return this.cuenta;}
- protected set
- {
- this.cuenta = value;
- }
- }
- public bool Empty
- {
- get { return this.cuenta <= 0; }
- }
- public Queue(int capacidad)
- {
- this.datos = new E[capacidad];
- this.head = this.tail = 0;
- this.cuenta = 0;
- }
- private int Next(int pos)
- {
- return (++pos > this.datos.Length) ? 0 : pos;
- }
- /*
- *tail indica el lugar donde agregar el elemento
- *se agrega el elemento en el contenedor
- *luego se pasa a la siguiente posicion
- *finalmente se incrementa la cuenta
- */
- public void Enqueue(E elemento)
- {
- if (this.cuenta >= this.datos.Length)
- {
- throw new Exception("Error la cola esta llena.");
- }
- this.datos[this.tail] = elemento;
- this.tail = this.Next(this.tail);
- ++this.cuenta;
- }
- /*
- * head indica el proximo elemento a extraer
- * se toma el elemento a extraer
- * luego se pasa a la siguiente posicion
- * se decrementa la cuenta
- * finalmente se devuelve el elemento
- */
- public E Dequeue()
- {
- if (this.Empty)
- {
- throw new Exception("Error la cola esta vacia..");
- }
- E temp = this.datos[this.head];
- this.head = this.Next(this.head);
- --this.cuenta;
- return temp;
- }
- public E Peek()
- {
- if (this.Empty)
- {
- throw new Exception("Error la cola esta vacia..");
- }
- return this.datos[this.head];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement