-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.cs
82 lines (73 loc) · 1.81 KB
/
Queue.cs
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
79
80
81
82
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Backend
{
public class Queue<T>
{
private T[] _queue;
private int _headPointer = 0;
private int _tailPointer = -1;
private int _queueSize;
public Queue(int queueSize)
{
_queue = new T[queueSize];
_queueSize = queueSize;
}
public T Peek()
{
if (_headPointer > _tailPointer)
{
throw new InvalidOperationException("Queue is empty");
}
return _queue[_headPointer];
}
public T PeekEnd()
{
if (_headPointer > _tailPointer)
{
throw new InvalidOperationException("Queue is empty");
}
return _queue[_tailPointer];
}
public void Enqueue(T item)
{
if (_tailPointer + 1 == _queueSize)
{
throw new InvalidOperationException("Queue is full");
}
else
{
_tailPointer++;
_queue[_tailPointer] = item;
}
}
public T Dequeue()
{
if (_headPointer > _tailPointer)
{
throw new InvalidOperationException("Queue is empty");
}
else
{
_headPointer++;
return _queue[_headPointer - 1];
}
}
public int GetQueueSize()
{
return _queueSize;
}
public void ResetPointers()
{
_headPointer = 0;
_tailPointer = -1;
}
public void ResetHeadPointer()
{
_headPointer = 0;
}
}
}