Skip to content

Instantly share code, notes, and snippets.

@dluciano
Created September 25, 2022 07:21
Show Gist options
  • Save dluciano/66d50d8b31b1fe8dcf25829423987314 to your computer and use it in GitHub Desktop.
Save dluciano/66d50d8b31b1fe8dcf25829423987314 to your computer and use it in GitHub Desktop.
622. Design Circular Queue
public class MyCircularQueue {
private int[] _elements;
private int _start = -1;
private int _end = -1;
private int _capacity = 0;
private int _count = 0;
public MyCircularQueue(int k) {
_elements = new int[k];
_capacity = k;
}
public bool EnQueue(int _value) {
if(IsFull()) return false;
if(_start < 0) _start++;
_end = (_end + 1) % _capacity;
_elements[_end] = _value;
_count++;
return true;
}
public bool DeQueue() {
if(IsEmpty()) return false;
_start = (_start + 1) % _capacity;
_count--;
return true;
}
public int Front() => IsEmpty() ? -1: _elements[_start];
public int Rear() => IsEmpty() ? -1 : _elements[_end];
public bool IsEmpty() => _count <= 0;
public bool IsFull() => _count == _capacity;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment