using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoQueue3
{
class SpecialQueue<ELEMENT>
{
private ELEMENT[] data;
private int head;
private int tail;
private int count;
public SpecialQueue(int capacity = 50)
{
this.data = new ELEMENT[capacity];
this.head = this.tail = 0;
this.count = 0;
}
public int Count {
get { return this.count; }
}
private int Next(int position)
{
return (++position >= this.data.Length) ? 0 : position;
}
public void Enqueue(ELEMENT element)
{
if (this.count >= this.data.Length)
{
throw new Exception("Error la Cola está llena ...");
}
this.data[this.tail] = element;
this.tail = this.Next(this.tail);
++this.count;
}
public ELEMENT Dequeue()
{
if (this.count <= 0)
{
throw new Exception("Error la Cola esta vacía ...");
}
ELEMENT temp = this.data[this.head];
this.head = this.Next(this.head);
--this.count;
return temp;
}
public ELEMENT[] ToArray()
{
ELEMENT[] result = new ELEMENT[this.Count];
for (int pos = this.head, c = 0; c < this.count; c++)
{
result[c] = this.data[pos];
pos = this.Next(pos);
}
return result;
}
public SpecialQueue<ELEMENT> join(SpecialQueue<ELEMENT> other)
{
SpecialQueue<ELEMENT> result = new SpecialQueue<ELEMENT>();
ELEMENT[] data = this.ToArray();
foreach (ELEMENT e in data)
{
result.Enqueue(e);
}
data = other.ToArray();
foreach (ELEMENT e in data)
{
result.Enqueue(e);
}
return result;
}
}
}
Comments