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" ]
}
}

23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
# Table of Contents
[1- code challange class 2 array shift ](https://github.com/Goorob-401-advanced-javascript/data-structures-and-algorithms/pull/3)
# Stack and Queue
## challange summary
stack is a type of data structure that is linear where order is conserved. For many people, stack is known to have a LIFO (Last In First Out) mechanism .
queue is a linear data structure where it obeys the FIFO (First In First Out) mechanism.

## Challenge description
1. create a Stack class that has a top property. It creates an empty Stack when instantiated.
- Define a method called push which takes any value as an argument and adds a new node with that value to the top of the stack with an O(1) Time performance.
- Define a method called pop that does not take any argument, removes the node from the top of the stack, and returns the node’s value.
- Define a method called peek that does not take an argument and returns the value of the node located on top of the stack, without removing it from the stack.
- Define a method called isEmpty that does not take an argument, and returns a boolean indicating whether or not the stack is empty.
1. Create a Queue class that has a front property. It creates an empty Queue when instantiated.
- Define a method called enqueue which takes any value as an argument and adds a new node with that value to the back of the queue with an O(1) Time performance.
- Define a method called dequeue that does not take any argument, removes the node from the front of the queue, and returns the node’s value.
- Define a method called peek that does not take an argument and returns the value of the node located in the front of the queue, without removing it from the queue.
- Define a method called isEmpty that does not take an argument, and returns a boolean indicating whether or not the queue is empty.

## Approach & Efficiency
The Big of O is o(1) for all the method

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

[
35 changes: 35 additions & 0 deletions challanges/stack-and-queue/__test__/queue.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const Queue = require('../queue.js');

describe('Queue', () => {
it('add item to the end ', () => {
let queue = new Queue();
queue.enqueue('shoroq');
queue.enqueue('goorob');
queue.enqueue('duha');
expect(queue.front).toEqual('shoroq');
expect(queue.rear).toEqual('duha');

});

it('remove from the front ', () => {
let queue = new Queue();
queue.enqueue('shoroq');
queue.enqueue('goorob');
queue.enqueue('duha');
let item = queue.dequeue();
expect(item).toEqual('shoroq');
expect(queue.front).toEqual('goorob');
expect(queue.rear).toEqual('duha');
queue.dequeue();
queue.dequeue();
expect(queue.dequeue()).toBeNull();
expect(queue.front).toBeNull();
expect(queue.rear).toBeNull();
});
describe('isEmpty()' , ()=> {
let queue = new Queue();
expect(queue.isEmpty()).toBeTruthy();
});
});
45 changes: 45 additions & 0 deletions challanges/stack-and-queue/__test__/stack.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const Stack = require('../stack.js');

describe('Stack', () => {
describe('push()', () => {
it('(add) push an item', () => {
let stack = new Stack();
expect(stack.peek()).toBeNull();
stack.push('Instance');
expect(stack.peek()).toEqual('Instance');
stack.push('Sally');
expect(stack.peek()).toEqual('Sally');
});
});

describe('pop()', () => {
it('remove the top item ', () => {
let stack = new Stack();
stack.push('shoroq');
stack.push('goorob');
stack.push('duha');
let item = stack.pop();
expect(item).toEqual('duha');
stack.pop();
item = stack.pop();
expect(item).toEqual('shoroq');
console.log(stack);
expect(stack.peek()).toBeNull();
});
});
describe('peek()', ()=> {
it('rturn top of stack', () => {
let stack = new Stack();
stack.push('shoroq');
expect(stack.peek()).toEqual('shoroq');
});
});
describe('isEmpty()' , ()=> {
it('should be empty' , () =>{
let stack = new Stack();
expect(stack.isEmpty()).toBeTruthy();
});
});
});
42 changes: 42 additions & 0 deletions challanges/stack-and-queue/queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

class Queue {
constructor(){
this.storage = [];
this.front = null;
this.rear = null;
}

enqueue(item){
this.storage.push(item);
if(this.storage.length == 1){
this.front = item;
this.rear = item;
}
else{
this.rear = item;
}
}

dequeue(){
if(!this.storage.length){
return null;
}

let item = this.storage.shift();
if(this.storage.length == 0){
this.front = this.rear = null;
}
else{
this.front = this.storage[0];
this.rear = this.storage[this.storage.length-1];
}

return item;
}
isEmpty(){
if(this.storage.length == 0){ return true ; }
}
}

module.exports = Queue;
28 changes: 28 additions & 0 deletions challanges/stack-and-queue/stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

class Stack{
constructor(){
this.storage = [];
this.top = null;
}

push(item){
this.storage.push(item);
this.top = item;
}

pop(){
let item = this.storage.pop();
this.top = !this.storage.length ? null : this.storage[this.storage.length-1];
return item;
}

peek(){
return this.top;
}
isEmpty(){
if(this.storage.length == 0){ return true ;}
}
}

module.exports = Stack;
Loading