-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueue_Array.cs
More file actions
78 lines (63 loc) · 1.46 KB
/
Queue_Array.cs
File metadata and controls
78 lines (63 loc) · 1.46 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
69
70
71
72
73
74
75
76
77
78
namespace CSharp.DS.Queue
{
/// <summary>
/// Queue implementation using an Array
/// </summary>
/// <typeparam name="T"></typeparam>
public class Queue_Array<T>
{
private int size;
private int head = -1;
private int tail = -1;
private readonly T[] _elements;
public Queue_Array(int capacity)
{
_elements = new T[capacity];
size = 0;
}
public bool Enequeue(T e)
{
if (size == _elements.Length)
return false;
head = (head + 1) % _elements.Length;
_elements[head] = e;
size++;
if (tail == -1)
{
tail = head;
}
return true;
}
public bool Dequeue()
{
if (size == 0)
{
return false;
}
size--;
tail = (tail + 1) % _elements.Length;
if (size == 0)
{
head = -1;
tail = -1;
}
return true;
}
public T Front()
{
if (size == 0)
return default;
return _elements[head];
}
public T Rear()
{
if (size == 0)
return default;
return _elements[tail];
}
public int Size()
{
return size;
}
}
}