Skip to content

Commit 83b3a35

Browse files
committedMar 25, 2018
Add linked_list.
1 parent e9a3b9d commit 83b3a35

9 files changed

+2640
-0
lines changed
 

‎.babelrc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"presets": [
3+
[
4+
"env", {
5+
"targets": {
6+
"node": "current"
7+
}
8+
}
9+
]
10+
]
11+
}

‎.editorconfig

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
charset = utf-8
7+
indent_style = space
8+
indent_size = 2

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
node_modules
2+
build
13
.idea

‎README.md

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
# Data Structures
2+
3+
```
4+
npm run babel-node --silent ./src/linked_list
5+
```

‎package-lock.json

+2,522
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "data-structures",
3+
"version": "1.0.0",
4+
"description": "Data structures on JavaScript",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "babel src -d build",
8+
"babel-node": "babel-node"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/trekhleb/data-structures.git"
13+
},
14+
"keywords": [],
15+
"author": "Oleksii Trekhleb (https://www.linkedin.com/in/trekhleb/)",
16+
"license": "ISC",
17+
"bugs": {
18+
"url": "https://github.com/trekhleb/data-structures/issues"
19+
},
20+
"homepage": "https://github.com/trekhleb/data-structures#readme",
21+
"devDependencies": {
22+
"babel-cli": "^6.26.0",
23+
"babel-preset-env": "^1.6.1"
24+
}
25+
}

‎src/linked_list/LinkedList.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Node } from './Node';
2+
3+
export class LinkedList {
4+
constructor() {
5+
this.head = null;
6+
}
7+
8+
append(value) {
9+
const newNode = new Node(value);
10+
11+
// If there is no head yet let's make new node a head.
12+
if (!this.head) {
13+
this.head = newNode;
14+
15+
return newNode;
16+
}
17+
18+
// Rewind to last node.
19+
let currentNode = this.head;
20+
while (currentNode.nextNode !== null) {
21+
currentNode = currentNode.nextNode;
22+
}
23+
24+
// Attach new node to the end of linked list.
25+
currentNode.nextNode = newNode;
26+
27+
return newNode;
28+
}
29+
30+
prepend(value) {
31+
this.head = new Node(value, this.head);
32+
}
33+
34+
deleteWithValue(value) {
35+
36+
}
37+
38+
render() {
39+
let currentNode = this.head;
40+
let index = 0;
41+
42+
while(currentNode.nextNode !== null) {
43+
console.log(`Node #${index} data: ${currentNode.value}`);
44+
currentNode = currentNode.nextNode;
45+
index++;
46+
}
47+
48+
// Print the tail.
49+
console.log(`Node #${index} data: ${currentNode.value}`);
50+
}
51+
}

‎src/linked_list/Node.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export class Node {
2+
constructor(value, nextNode = null) {
3+
this.value = value;
4+
this.nextNode = nextNode;
5+
}
6+
}

‎src/linked_list/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { LinkedList } from "./LinkedList";
2+
3+
const linkedList = new LinkedList();
4+
5+
linkedList.append(1);
6+
linkedList.append(2);
7+
linkedList.append(3);
8+
linkedList.prepend(4);
9+
linkedList.prepend(5);
10+
11+
linkedList.render();

0 commit comments

Comments
 (0)
Please sign in to comment.