-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCircularQueue.cs
100 lines (91 loc) · 3.1 KB
/
CircularQueue.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.Text;
namespace LeetCodeSolutionsLib
{
/// <summary>
/// 622. Design Circular Queue
/// Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".
/// IE) Input: [1,2,3,1]
/// Output: true
/// </summary>
public class CircularQueue
{
private int _headPtr;
private int _tailPtr;
private int _size;
private int[] _data;
/** Initialize your data structure here. Set the size of the queue to be k. */
public CircularQueue(int k)
{
_size = k;
_headPtr = -1;
_tailPtr = -1;
this._data = new int[_size];
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
public bool EnQueue(int value)
{
if (IsFull())
{
return false;
}
if (IsEmpty())
{
_headPtr = 0;
}
_tailPtr = (_tailPtr + 1) % (_size); // Move tailPtr up 1
_data[_tailPtr] = value;
// Console.WriteLine($"EnQueue : {value}");
return true;
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
public bool DeQueue()
{
if (IsEmpty())
{
return false;
}
// If the _headPtr and _tailPtr are pointing to the same element, then this means that the _tailPtr has gone around the entire queue.
// So we reset the pointers back to -1, this means that the Queue is 'empty'.
// Technically the index may contain data, but for the purpose of a circular queue, it will be considered empty.
if (_headPtr == _tailPtr)
{
_headPtr = -1;
_tailPtr = -1;
return true;
}
_headPtr = (_headPtr + 1) % (_size); // Move headPtr up 1
// Console.WriteLine($"DeQueue : {_data[_tailPtr]}");
return true;
}
/** Get the front item from the queue. */
public int Front()
{
if (IsEmpty())
{
return -1;
}
return _data[_headPtr];
}
/** Get the last item from the queue. */
public int Rear()
{
if (IsEmpty())
{
return -1;
}
return _data[(_tailPtr + _size) % (_size)];
}
/** Checks whether the circular queue is empty or not. */
public bool IsEmpty()
{
return _headPtr == -1;
}
/** Checks whether the circular queue is full or not. */
public bool IsFull()
{
return (_tailPtr + 1) % (_size) == _headPtr;
}
}
}