Skip to content

Instantly share code, notes, and snippets.

@devpeds
Created May 26, 2017 14:52
Show Gist options
  • Save devpeds/c92b0a58d3bb9ec073bb607d30739c60 to your computer and use it in GitHub Desktop.
Save devpeds/c92b0a58d3bb9ec073bb607d30739c60 to your computer and use it in GitHub Desktop.
JavaScript Data Structure - Deque
var Deque = function() {
this.arr = [];
};
Deque.prototype.size = function() {
return this.arr.length;
};
Deque.prototype.empty = function() {
return this.size() === 0;
};
Deque.prototype.push_front = function(val) {
this.arr.unshift(val);
};
Deque.prototype.push_back = function(val) {
this.arr.push(val);
};
Deque.prototype.pop_front = function() {
return !this.empty() ? this.arr.shift() : -1;
};
Deque.prototype.pop_back = function() {
return !this.empty() ? this.arr.pop() : -1;
};
Deque.prototype.front = function() {
return !this.empty() ? this.arr[0] : -1;
};
Deque.prototype.back = function() {
return !this.empty() ? this.arr[this.size()-1] : -1;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment