Queue Implementation in C#
class Queue
{
int[] item;
int rear;
int front;
readonly int capacity;
public Queue()
{
capacity = 100;
rear = -1;
front = 0;
item = new int[capacity];
}
public Queue( int capacity)
{
this.capacity = capacity;
rear = -1;
front = 0;
item = new int[capacity];
}
public void Enqueue(int _item)
{
if (rear == capacity - 1)
{
Console.WriteLine("Queue is full");
return;
}
item[rear + 1] = _item;
rear++;
}
public int Dequeue()
{
if (front - 1==rear )
{
Console.WriteLine("Queue is empty");
return -1;
}
int itemtoReturn = item[front];
front = front + 1;
return itemtoReturn;
}
}
public class QueueTest
{
//public static void Main()
//{
// Queue que = new Queue(5);
// que.Enqueue(1);
// que.Enqueue(2);
// que.Enqueue(3);
// que.Enqueue(4);
// que.Enqueue(5);
// que.Enqueue(6);
// Console.WriteLine("items in queue :");
// Console.WriteLine(que.Dequeue());
// Console.WriteLine(que.Dequeue());
// Console.WriteLine(que.Dequeue());
// Console.WriteLine(que.Dequeue());
// Console.WriteLine(que.Dequeue());
// Console.WriteLine(que.Dequeue());
// Console.Read();
//}
}
No comments:
Post a Comment