Queues in Data Structures:
Queue is a collection same as list but it is a FIFO (First In First Out) Collection. It means the element that is entered in a queue at first will the come out at first position. In more easy words whenever we Enqueue (insert) an element in the Queue First will Dequeue (remove) first whenever we want to delete and element of the Queue. It will work like this because Enqueue will insert elements is rear of a queue and after new element is inserted the rear element will go down and new element takes the rear place and Dequeue deletes the element at the Front of the queue which is the oldest one.
Basic Queue Operations:
- New() : Creates a new Queue.
- Enqueue() : Inserts Object or element in the Rear of the Queue.
- Dequeue() : Deletes Object or element from the Front of the Queue.
- Front() : Returns the Object or element in the Front of the Queue but does not delete it from the Stack.
- Size() : Returns the number of Object or element in the Queue i.e. Size of the Queue
- IsEmpty() : It is a Boolean Function, It Indicates that the Queue is empty or not.
Code of Queue Collection in C#:
class Code
{
Queue<int> numbers = new Queue<int>(); // initialization,
int is the data type and numbers is the name of queue
public void working()
{
numbers.Enqueue(100); //100 will be entered in the rear of queue
Console.WriteLine("100 is
Enqueued");
display();
numbers.Enqueue(200); // 200 will bw entered and now 200 is on rear
Console.WriteLine("200 is
Enqueued");
display();
numbers.Enqueue(300); // now 300 is on rear of the queue
Console.WriteLine("300 is
Enqueued");
display();
numbers.Dequeue(); // front (First inserted) value 100 will be deleted and
200 will become the first(Front) value
Console.WriteLine("100 is
Dequeued");
display();
numbers.Dequeue(); // 200 will be deleted and 300 will become the Front value
Console.WriteLine("200 is
Dequeued");
display();
Console.WriteLine("Front =
{0}",numbers.Peek()); //
Return the Front object
}
public void display()
{
Console.WriteLine("Queue :
");
foreach (int a in numbers) // displaying all
Queue members using foreach loop
{
Console.WriteLine(a);
}
Console.WriteLine();
}
}
class Program
{
static void Main(string[] args)
{
Code c = new Code();
c.working();
}
}
0 comments:
Post a Comment