Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"env": {
"browser": true,
"node": true,
"commonjs": true,
"jest": true,
"es6": true
},
"globals": {
"err": true,
"req": true,
"res": true,
"next": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-console": "off",
"indent": [ "error", 2 ],
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
"comma-dangle": ["error", "always-multiline"],
"semi": [ "error", "always" ]
}
}

Binary file added 20200210_141653.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# Table of Contents
[1- code challange class 2 array shift ](https://github.com/Goorob-401-advanced-javascript/data-structures-and-algorithms/pull/3)
# Queue-with-stacks
## challange summary

[2- code challange class 3 array binary search ](https://github.com/Goorob-401-advanced-javascript/data-structures-and-algorithms/pull/2)
Implement a Queue using two Stacks.

[
## Challenge description
Create a brand new PseudoQueue class , this PseudoQueue class will implement our standard queue , but will internally only utilize 2 Stack objects.
- enqueue(value) which inserts value into the PseudoQueue, using a first-in, first-out approach.
- dequeue() which extracts a value from the PseudoQueue, using a first-in, first-out approach.

## Approach & Efficiency
only take up O(1) at most and for space it should be O(n) .

## Solution
![UML](https://github.com/Goorob-401-advanced-javascript/data-structures-and-algorithms/blob/queue-with-stacks/20200210_141653.jpg)
29 changes: 29 additions & 0 deletions challanges/queue-with-stacks/queue-with-stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

class PsuedoQueue {

constructor(){

this.stack_1 = [];
this.stack_2 = [];

}
enqueue(value){
this.stack_1.push(value);
}
dequeue(){
if(this.stack_1.length === 0){
return 'null';
} while (this.stack_1.length > 0) {
let popFromStack = this.stack_1.pop();
this.stack_2.push(popFromStack);
}

while(this.stack_2.length > 0){
let popFromStack = this.stack_2.pop();
this.stack_1.push(popFromStack);
}
return this.stack_2.pop();
}
}
module.exports = PsuedoQueue;
20 changes: 20 additions & 0 deletions challanges/queue-with-stacks/queue-with-stacks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
const PseudoQueue = require('./queue-with-stack.js');
let pseudoQueue = new PseudoQueue();

describe('Testing queue-with-stacks', () => {
beforeEach(() => {
pseudoQueue = new PseudoQueue();
});
it('Enqueue values', () => {
pseudoQueue.enqueue('301');
pseudoQueue.enqueue('401');
expect(pseudoQueue.stack_1).toEqual(['301', '401']);
});
it('Dequeue values', () => {
pseudoQueue.enqueue('301');
pseudoQueue.enqueue('401');
pseudoQueue.dequeue();
expect(pseudoQueue.stack_1).toEqual(['301' ,'401']);
});
});
Loading