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 20200204_004602.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)
# Singly Linked List
## challange summary

[2- code challange class 3 array binary search ](https://github.com/Goorob-401-advanced-javascript/data-structures-and-algorithms/pull/2)
adding a new methods to the linked list
## Challenge description
- append(value) which adds a new node with the given value to the end of the list
- insertBefore(value, newVal) which add a new node with the given newValue immediately before the first value node
- insertAfter(value, newVal) which add a new node with the given newValue immediately after the first value node

## Approach & Efficiency
i used classes and constructor format , while loop also i create a tests for it
## Solution

![UML](https://github.com/Goorob-401-advanced-javascript/data-structures-and-algorithms/blob/ll-insertions/20200204_004602.jpg)

[
63 changes: 63 additions & 0 deletions challanges/ll-insertions/ll-insertion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

class Node{
constructor(value){
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor(){
this.head = null;
}
append (value){
let node = new Node(value);
if(!this.head){
this.head = node;
return this;
}
let currentNode = this.head;
while(currentNode.next){
currentNode = currentNode.next;
}
currentNode.next = node;
return this;
}
insertBefore(value, newVal){
let node = new Node(newVal);
let currentNode = this.head;
let firstNode = null;

while(currentNode){
if(currentNode.value === value){
if(firstNode === null){
this.head = node;
}else{
firstNode.next = node;
}node.next = currentNode;
break;
}
firstNode = currentNode;
currentNode = currentNode.next;
}
}
insertAfter(value,newVal){
let newNode = new Node(newVal);
let currentNode = this.head;
let nextValue = null;
while(currentNode){
if(currentNode.value === value){
nextValue = currentNode.next;
currentNode.next = newNode;
newNode.next = nextValue;
}currentNode = currentNode.next;

}
}
}
// let ll =new LinkedList();
// list.append('1');
// list.append('2');
// list.insertBefore('1','3');
// console.log(ll);
module.exports = LinkedList;
35 changes: 35 additions & 0 deletions challanges/ll-insertions/ll-insertion.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const LinkedList = require('./ll-insertion.js');

describe('LL Module',()=>{
it ('constructor',()=>{
let list = new LinkedList();
expect(list.head).toBeNull();

});

it('append()',()=>{
let ll = new LinkedList();

ll.append('1');
expect(ll.head.value).toEqual('1');
expect(ll.head.value.next).toBeAnObject;
});

it('insertBefore()',()=>{
let ll = new LinkedList();
let initVal = '1';
ll.append(initVal);
ll.insertBefore(initVal,'2');
expect(ll.head.value).toEqual('2');
});

it('insertAfter()',()=>{
let ll = new LinkedList();
let initVal = '1';
ll.append(initVal);
ll.insertAfter(initVal,'2');
expect(ll.head.value).toEqual('1');
expect(ll.head.value.next).toBeAnObject;
});

});
Loading