Implement a circular queue

Implement a circular queue of integers of user-specified size using a simple array. Provide routines to initialize(), enqueue() and dequeue() the queue. Make it thread safe.

   1: using System;

   2: using System.Text;

   3: using System.Threading;

   4:  

   5: public class IntegerQueue

   6: {

   7:     int[] _queue;

   8:     int _in;

   9:     int _out;

  10:     int _count;

  11:  

  12:     private static Object QueueLock = new Object(); // Make the Queue thread safe

  13:  

  14:     public IntegerQueue(int size)

  15:     {

  16:         Initialize(size);

  17:     }

  18:  

  19:     public void Initialize(int size)

  20:     {

  21:         _queue = new int[size];

  22:         _count = 0;

  23:         _in = 0;

  24:         _out = 0;

  25:     }

  26:  

  27:     public void Enqueue(int value)

  28:     {

  29:         lock (QueueLock)

  30:         {

  31:             Console.WriteLine("Enqueue: {0}", value);

  32:             if (_queue.Length <= _count)

  33:             {

  34:                 Console.WriteLine("Overflow.");

  35:             }

  36:             else

  37:             {

  38:                 _queue[_in] = value;

  39:                 _in = (_in + 1) % _queue.Length;

  40:                 _count++;

  41:             }

  42:             DebugPrint();

  43:         }

  44:     }

  45:     public int Dequeue()

  46:     {

  47:         lock (QueueLock)

  48:         {

  49:             Console.WriteLine("Dequeue:");

  50:             int value = 0;

  51:             if (_count <= 0)

  52:             {

  53:                 Console.WriteLine("Underflow.");

  54:             }

  55:             else

  56:             {

  57:                 value = _queue[_out];

  58:                 _out = (_out + 1) % _queue.Length;

  59:                 _count--;

  60:             }

  61:             DebugPrint();

  62:             return value;

  63:         }

  64:  

  65:     }

  66:     public override String ToString()

  67:     {

  68:         StringBuilder builder = new StringBuilder();

  69:         if (_count > 0)

  70:         {

  71:             for (int i = 0; i < _count; i++)

  72:             {

  73:                 builder.Append(_queue[(_out + i) % _queue.Length]);

  74:                 builder.Append(", ");

  75:             }

  76:         }

  77:         else

  78:         {

  79:             builder.Append("(empty)");

  80:         }

  81:         return builder.ToString();

  82:     }

  83:     public void DebugPrint()

  84:     {

  85:  

  86:         lock (QueueLock)

  87:         {

  88:             Console.WriteLine("\t{0}", ToString());

  89:         }

  90:  

  91:     }

  92:  

  93:  

  94:     public void QueueTest()

  95:     {

  96:         Enqueue(1);

  97:         Console.WriteLine();

  98:  

  99:         Enqueue(2);

 100:         Console.WriteLine();

 101:  

 102:        Enqueue(3);

 103:         Console.WriteLine();

 104:  

 105:         Console.WriteLine("--> {0}", Dequeue());

 106:         Console.WriteLine();

 107:  

 108:         Console.WriteLine("--> {0}", Dequeue());

 109:         Console.WriteLine();

 110:  

 111:         Console.WriteLine("--> {0}", Dequeue());

 112:         Console.WriteLine();

 113:  

 114:         Enqueue(4);

 115:         Console.WriteLine();

 116:  

 117:         Enqueue(5);

 118:         Console.WriteLine();

 119:  

 120:         Console.WriteLine("--> {0}", Dequeue());

 121:         Console.WriteLine();

 122:  

 123:         Enqueue(6);

 124:         Console.WriteLine();

 125:  

 126:         Enqueue(7);

 127:         Console.WriteLine();

 128:  

 129:         Enqueue(8);

 130:         Console.WriteLine();

 131:  

 132:         Enqueue(9);

 133:         Console.WriteLine();

 134:  

 135:         Console.WriteLine("--> {0}", Dequeue());

 136:         Console.WriteLine();

 137:  

 138:         Console.WriteLine("--> {0}", Dequeue());

 139:         Console.WriteLine();

 140:  

 141:         Console.WriteLine("--> {0}", Dequeue());

 142:         Console.WriteLine();

 143:  

 144:         Console.WriteLine("--> {0}", Dequeue());

 145:         Console.WriteLine();

 146:  

 147:         Console.WriteLine("--> {0}", Dequeue());

 148:         Console.WriteLine();

 149:  

 150:         Console.ReadLine();

 151:     }

 152:  

 153: }

 154:  

 155: public class QueueApp

 156: {

 157:     public static void Main(String[] args)

 158:     {

 159:         IntegerQueue queue = new IntegerQueue(5);

 160:  

 161:         Thread[] threads = new Thread[1];

 162:  

 163:         for (int i = 0; i < threads.Length; i++)

 164:         {

 165:             threads[i] = new Thread(new ThreadStart(queue.QueueTest));

 166:         }

 167:  

 168:         for (int i = 0; i < threads.Length; i++)

 169:         {

 170:             threads[i].Start();

 171:         }

 172:         Console.ReadLine();

 173:     }

 174: }

Advertisements