using System;
namespace Queue_Array_Implementation
{
class Queue
{
private Object[] queue;
private int rear;
private int front;
private int size;
private int count;
public Queue(int size)
{
this.size = size;
queue = new Object[size];
front = 0;
rear = size - 1;
}
public bool isFull()
{
return count == size;
}
public void enQueue(Object item)
{
if (!isFull())
{
rear = (rear + 1) % (size);
queue[rear] = item;
count++;
}
else
{
Console.WriteLine("Queue is full");
}
}
public bool isEmpty()
{
return (count == 0);
}
public Object Front()
{
Object item = null;
if (!isEmpty())
{
item = queue[front];
}
else
{
Console.WriteLine("Queue is Empty");
}
return item;
}
public Object deQueue()
{
Object item = null;
if (!isEmpty())
{
item = queue[front];
front = (front + 1) % (size);
count--;
}
else
{
Console.WriteLine("Queue is Empty");
}
return item;
}
}
}
---------------------------------------------------------------------------------------------------------------
Driver Program using Queue Class
using System;
namespace Queue_Array_Implementation
{
class Program
{
static void Main(string[] args)
{
Queue queue = new Queue(5);
Object fruit;
queue.enQueue("Apple"); // add apple at the rear of the queue
queue.enQueue("Banana"); // add banana at the rear of the queue
queue.enQueue("Grape"); // add grape at the rear of the queue
Console.WriteLine(queue.Front()); // return the item at the front of the queue
queue.enQueue("Guava"); // add gauva at the rear of the queue
queue.enQueue("Orange"); // add orange at the rear of the queue
queue.enQueue("Melon"); // can't add melon. queue is full.
fruit = queue.deQueue(); // return and remove the item at the front of the queue
Console.WriteLine(fruit.ToString());
fruit = queue.deQueue(); // return and remove the item at the front of the queue
Console.WriteLine(fruit.ToString());
fruit = queue.deQueue(); // return and remove the item at the front of the queue
Console.WriteLine(fruit.ToString());
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
}
}
No comments:
Post a Comment