Advertisement
jtentor

DemoQueue3 - SpecialQueue.cs

May 18th, 2020
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace DemoQueue3
  8. {
  9.     class SpecialQueue<ELEMENT>
  10.     {
  11.         private ELEMENT[] data;
  12.         private int head;
  13.         private int tail;
  14.         private int count;
  15.  
  16.         public SpecialQueue(int capacity = 50)
  17.         {
  18.             this.data = new ELEMENT[capacity];
  19.             this.head = this.tail = 0;
  20.             this.count = 0;
  21.         }
  22.  
  23.         public int Count {
  24.             get { return this.count; }
  25.         }
  26.  
  27.         private int Next(int position)
  28.         {
  29.             return (++position >= this.data.Length) ? 0 : position;
  30.         }
  31.  
  32.         public void Enqueue(ELEMENT element)
  33.         {
  34.             if (this.count >= this.data.Length)
  35.             {
  36.                 throw new Exception("Error la Cola está llena ...");
  37.             }
  38.             this.data[this.tail] = element;
  39.             this.tail = this.Next(this.tail);
  40.             ++this.count;
  41.         }
  42.  
  43.         public ELEMENT Dequeue()
  44.         {
  45.             if (this.count <= 0)
  46.             {
  47.                 throw new Exception("Error la Cola esta vacía ...");
  48.             }
  49.             ELEMENT temp = this.data[this.head];
  50.             this.head = this.Next(this.head);
  51.             --this.count;
  52.             return temp;
  53.         }
  54.  
  55.         public ELEMENT[] ToArray()
  56.         {
  57.             ELEMENT[] result = new ELEMENT[this.Count];
  58.             for (int pos = this.head, c = 0; c < this.count; c++)
  59.             {
  60.                 result[c] = this.data[pos];
  61.                 pos = this.Next(pos);
  62.             }
  63.             return result;
  64.         }
  65.  
  66.         public SpecialQueue<ELEMENT> join(SpecialQueue<ELEMENT> other)
  67.         {
  68.             SpecialQueue<ELEMENT> result = new SpecialQueue<ELEMENT>();
  69.  
  70.             ELEMENT[] data = this.ToArray();
  71.             foreach (ELEMENT e in data)
  72.             {
  73.                 result.Enqueue(e);
  74.             }
  75.  
  76.             data = other.ToArray();
  77.             foreach (ELEMENT e in data)
  78.             {
  79.                 result.Enqueue(e);
  80.             }
  81.  
  82.             return result;
  83.         }
  84.  
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement