Skip to content

Instantly share code, notes, and snippets.

View rxluz's full-sized avatar

Ricardo Luz rxluz

View GitHub Profile
function PriorityQueue() {
let priority = []
let nonPriority = []
class PublicPriorityQueue {
size() {
return priority.length + nonPriority.length
}
isEmpty() {
function Stack() {
let items = []
class PublicStack {
peek() {
if (this.isEmpty()) throw new Error('Stack is empty')
const lastItemIndex = items.length - 1
return items[lastItemIndex]
}
function Stack() {
let items = []
class PublicStack {
peek() {
if (this.isEmpty()) throw new Error('Stack is empty')
const lastItemIndex = items.length - 1
return items[lastItemIndex]
}
function Stack() {
let items = []
class PublicStack {
peek() {
if (this.isEmpty()) throw new Error('Stack is empty')
const lastItemIndex = items.length - 1
return items[lastItemIndex]
}
@rxluz
rxluz / DATA_STRUCTURES_stackUsingNodes.js
Last active January 29, 2019 04:49
JS Data Structures: Stacks, see more at: https://medium.com/p/7dcea711c091
function Stack() {
let top = null
let size = 0
class Node {
constructor(data) {
this.data = data
this.previous = null
}
}
function Stack() {
let items = []
class PublicStack {
peek() {
if (this.isEmpty()) throw new Error('Stack is empty')
const lastItemIndex = items.length - 1
return items[lastItemIndex]
}
@rxluz
rxluz / DATA_STRUCTURES_reverseArray.js
Last active January 29, 2019 04:50
JS Data Structures: Arrays, see more at: https://medium.com/p/e1bc57bda950
const animals = ['elephant', 'monkey', 'snake', 'lion', 'zebra']
const reverseArray = currentArray => {
for (let index = 0; index < currentArray.length / 2; index++) {
const currentItem = currentArray[index]
const otherItem = currentArray[currentArray.length - 1 - index]
currentArray[index] = otherItem
currentArray[currentArray.length - 1 - index] = currentItem
}
const animals = ['elephant', 'monkey', 'snake', 'lion']
const desiredPositionToRemove = 2
for (var index = desiredPositionToRemove; index < animals.length; index++) {
animals[index] = animals[index + 1]
}
animals.length = animals.length - 1
const animals = ["elephant", "monkey", "snake", "lion"];
for (let index = 0; index < animals.length; index++) {
animals[index] = animals[index + 1];
}
animals.length = animals.length - 1;
console.log(animals);
const animals = ["elephant", "monkey", "snake", "lion"];
const positionToAddNewItem = 2;
for (let index = animals.length; index > positionToAddNewItem; index--) {
animals[index] = animals[index - 1];
}
animals[positionToAddNewItem] = "macaw";