Queue is a data structures. That’s principle is FIFO (first in first out). This aspect similar to stack data structures. We will use 3 class and 1 interface.
Firstly we are creating our interface.
IQueue.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ArrayTypedQueue { public interface IQueue { void Insert(object o); object Remove(); object Peek(); Boolean IsEmpty(); } }
We are creating simple queue class. (extends IQueue)
SimpleArrayTypedQueue.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ArrayTypedQueue { public class SimpleArrayTypedQueue: IQueue { private object[] Queue; private int front = -1; private int rear = -1; private int size = 0; private int count = 0; public SimpleArrayTypedQueue(int size) { this.size = size; Queue = new object[size]; } public void Insert(object o) { if ((count == size) || (rear == size -1)) throw new Exception("Queue is full."); if (front == -1) front = 0; Queue[++rear] = o; count++; } public object Remove() { if (IsEmpty()) throw new Exception("Queue is full."); object temp = Queue[front]; Queue[front] = null; front++; count--; return temp; } public object Peek() { return Queue[front]; } public bool IsEmpty() { return (count == 0); } } }
We are creating circular queue class. (extends IQueue)
CircularArrayTypedQueue.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ArrayTypedQueue { public class CircularArrayTypedQueue: IQueue { private object[] Queue; private int front = -1; private int rear = -1; private int size = 0; private int count = 0; public CircularArrayTypedQueue(int size) { this.size = size; Queue = new object[size]; } public void Insert(object o) { if (count == size) throw new Exception("Queue is full."); if (front == -1) front = 0; //Circular Code Changing if (rear == size - 1) { rear = 0; Queue[rear] = o; } else Queue[++rear] = o; count++; } public object Remove() { if (IsEmpty()) throw new Exception("Queue is full."); object temp = Queue[front]; Queue[front] = null; //Circular Code Changing if (front == size - 1) front = 0; else front++; count--; return temp; } public object Peek() { return Queue[front]; } public bool IsEmpty() { return (count == 0); } } }
We are creating priority queue class. (extends IQueue)
PriorityQueue.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ArrayTypedQueue { public class PriorityQueue: IQueue { private object[] Queue; private int front = -1; private int size = 0; private int count = 0; public PriorityQueue(int size) { this.size = size; Queue = new object[size]; } public void Insert(object o) { if (count == size) { throw new Exception("Queue is full"); } if (IsEmpty()) { front++; Queue[front] = o; count++; } else { int i; for (i = count - 1; i >= 0; i--) { if ((int)o > (int)Queue[i]) Queue[i + 1] = Queue[i]; else break; } Queue[i + 1] = o; front++; count++; } } public object Remove() { if (this.IsEmpty()) { throw new Exception("Queue is empty..."); } object temp = Queue[front]; Queue[front] = null; front--; count--; return temp; } public object Peek() { return Queue[front]; } public bool IsEmpty() { return (count == 0); } } }