Repository navigation
Expand file tree
/
Copy pathQueue.cs
More file actions
68 lines (61 loc) · 1.69 KB
/
Copy pathQueue.cs
File metadata and controls
68 lines (61 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
namespace Code.QueueDS;
/// <summary>
/// Implement Queue data structure with List.
/// Alternatively, Queue can also be implemented with LinkedList.
/// C# provide Queue<T> class out of the box with performance optimisation.
/// C# Queue is performant because it is implemented using circular array with Head and Tail pointers.
/// and the ability to resize the array when it is full.
/// </summary>
public class Queue<T>
{
private List<T> items;
/// <summary>
/// Constructor to initialize the queue.
/// </summary>
/// <param name="data"></param>
public Queue()
{
items = [];
}
/// <summary>
/// Add an item to the queue.
/// </summary>
/// <param name="data"></param>
public void Enqueue(T data)
{
items.Add(data);
}
/// <summary>
/// Remove an item from the queue.
/// </summary>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public T Dequeue()
{
if(items.Count == 0)
{
throw new InvalidOperationException("Queue is empty");
}
T data = items[0];
items.RemoveAt(0);
return data;
}
/// <summary>
/// Get the first item from the queue without removing it.
/// </summary>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public T Peek()
{
if(items.Count == 0)
{
throw new InvalidOperationException("Queue is empty");
}
return items[0];
}
public int Count => items.Count;
public bool IsEmpty => items.Count == 0;
public List<T> Items => items;
}