mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Add linked_list.
This commit is contained in:
parent
e9a3b9d30c
commit
83b3a35834
11
.babelrc
Normal file
11
.babelrc
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"presets": [
|
||||
[
|
||||
"env", {
|
||||
"targets": {
|
||||
"node": "current"
|
||||
}
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
8
.editorconfig
Normal file
8
.editorconfig
Normal file
@ -0,0 +1,8 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
charset = utf-8
|
||||
indent_style = space
|
||||
indent_size = 2
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -1 +1,3 @@
|
||||
node_modules
|
||||
build
|
||||
.idea
|
||||
|
@ -1 +1,5 @@
|
||||
# Data Structures
|
||||
|
||||
```
|
||||
npm run babel-node --silent ./src/linked_list
|
||||
```
|
||||
|
2522
package-lock.json
generated
Normal file
2522
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
25
package.json
Normal file
25
package.json
Normal file
@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "data-structures",
|
||||
"version": "1.0.0",
|
||||
"description": "Data structures on JavaScript",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"build": "babel src -d build",
|
||||
"babel-node": "babel-node"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/trekhleb/data-structures.git"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Oleksii Trekhleb (https://www.linkedin.com/in/trekhleb/)",
|
||||
"license": "ISC",
|
||||
"bugs": {
|
||||
"url": "https://github.com/trekhleb/data-structures/issues"
|
||||
},
|
||||
"homepage": "https://github.com/trekhleb/data-structures#readme",
|
||||
"devDependencies": {
|
||||
"babel-cli": "^6.26.0",
|
||||
"babel-preset-env": "^1.6.1"
|
||||
}
|
||||
}
|
51
src/linked_list/LinkedList.js
Normal file
51
src/linked_list/LinkedList.js
Normal file
@ -0,0 +1,51 @@
|
||||
import { Node } from './Node';
|
||||
|
||||
export class LinkedList {
|
||||
constructor() {
|
||||
this.head = null;
|
||||
}
|
||||
|
||||
append(value) {
|
||||
const newNode = new Node(value);
|
||||
|
||||
// If there is no head yet let's make new node a head.
|
||||
if (!this.head) {
|
||||
this.head = newNode;
|
||||
|
||||
return newNode;
|
||||
}
|
||||
|
||||
// Rewind to last node.
|
||||
let currentNode = this.head;
|
||||
while (currentNode.nextNode !== null) {
|
||||
currentNode = currentNode.nextNode;
|
||||
}
|
||||
|
||||
// Attach new node to the end of linked list.
|
||||
currentNode.nextNode = newNode;
|
||||
|
||||
return newNode;
|
||||
}
|
||||
|
||||
prepend(value) {
|
||||
this.head = new Node(value, this.head);
|
||||
}
|
||||
|
||||
deleteWithValue(value) {
|
||||
|
||||
}
|
||||
|
||||
render() {
|
||||
let currentNode = this.head;
|
||||
let index = 0;
|
||||
|
||||
while(currentNode.nextNode !== null) {
|
||||
console.log(`Node #${index} data: ${currentNode.value}`);
|
||||
currentNode = currentNode.nextNode;
|
||||
index++;
|
||||
}
|
||||
|
||||
// Print the tail.
|
||||
console.log(`Node #${index} data: ${currentNode.value}`);
|
||||
}
|
||||
}
|
6
src/linked_list/Node.js
Normal file
6
src/linked_list/Node.js
Normal file
@ -0,0 +1,6 @@
|
||||
export class Node {
|
||||
constructor(value, nextNode = null) {
|
||||
this.value = value;
|
||||
this.nextNode = nextNode;
|
||||
}
|
||||
}
|
11
src/linked_list/index.js
Normal file
11
src/linked_list/index.js
Normal file
@ -0,0 +1,11 @@
|
||||
import { LinkedList } from "./LinkedList";
|
||||
|
||||
const linkedList = new LinkedList();
|
||||
|
||||
linkedList.append(1);
|
||||
linkedList.append(2);
|
||||
linkedList.append(3);
|
||||
linkedList.prepend(4);
|
||||
linkedList.prepend(5);
|
||||
|
||||
linkedList.render();
|
Loading…
Reference in New Issue
Block a user