-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArrayQueue.cs
More file actions
37 lines (26 loc) · 746 Bytes
/
ArrayQueue.cs
File metadata and controls
37 lines (26 loc) · 746 Bytes
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
using System;
namespace AlgorithmsAndDataStructures.DataStructures.Queue;
public class ArrayQueue<T>
{
private readonly T[] queue;
private int pointer = -1;
public ArrayQueue(int initialCapacity = 8)
{
queue = new T[initialCapacity];
}
public bool IsEmpty => pointer == -1;
public void Enqueue(T value)
{
if (pointer == queue.Length - 1) throw new ArgumentException("Queue is full.");
pointer++;
queue[pointer] = value;
}
public T Dequeue()
{
if (IsEmpty) throw new ArgumentException("Queue is empty");
var value = queue[0];
for (var i = 0; i < pointer; i++) queue[i] = queue[i + 1];
pointer--;
return value;
}
}