mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
style: Fixed most styles (according to standardjs)
This commit is contained in:
@ -10,7 +10,7 @@
|
||||
|
||||
// class LinkedList and constructor
|
||||
// Creates a LinkedList
|
||||
var LinkedList = (function () {
|
||||
const LinkedList = (function () {
|
||||
function LinkedList () {
|
||||
// Length of linklist and head is null at start
|
||||
this.length = 0
|
||||
@ -19,7 +19,7 @@ var LinkedList = (function () {
|
||||
|
||||
// class node (constructor)
|
||||
// Creating Node with element's value
|
||||
var Node = (function () {
|
||||
const Node = (function () {
|
||||
function Node (element) {
|
||||
this.element = element
|
||||
this.next = null
|
||||
@ -39,12 +39,12 @@ var LinkedList = (function () {
|
||||
|
||||
// Creates a node and adds it to linklist
|
||||
LinkedList.prototype.add = function (element) {
|
||||
var node = new Node(element)
|
||||
const node = new Node(element)
|
||||
// Check if its the first element
|
||||
if (this.head === null) {
|
||||
this.head = node
|
||||
} else {
|
||||
var currentNode = this.head
|
||||
let currentNode = this.head
|
||||
|
||||
// Loop till there is node present in the list
|
||||
while (currentNode.next) {
|
||||
@ -60,8 +60,8 @@ var LinkedList = (function () {
|
||||
|
||||
// Removes the node with the value as param
|
||||
LinkedList.prototype.remove = function (element) {
|
||||
var currentNode = this.head
|
||||
var previousNode
|
||||
let currentNode = this.head
|
||||
let previousNode
|
||||
|
||||
// Check if the head node is the element to remove
|
||||
if (currentNode.element === element) {
|
||||
@ -88,8 +88,8 @@ var LinkedList = (function () {
|
||||
|
||||
// Returns the index of the element passed as param otherwise -1
|
||||
LinkedList.prototype.indexOf = function (element) {
|
||||
var currentNode = this.head
|
||||
var index = -1
|
||||
let currentNode = this.head
|
||||
let index = -1
|
||||
|
||||
while (currentNode) {
|
||||
index++
|
||||
@ -106,8 +106,8 @@ var LinkedList = (function () {
|
||||
|
||||
// Returns the element at an index
|
||||
LinkedList.prototype.elementAt = function (index) {
|
||||
var currentNode = this.head
|
||||
var count = 0
|
||||
let currentNode = this.head
|
||||
let count = 0
|
||||
while (count < index) {
|
||||
count++
|
||||
currentNode = currentNode.next
|
||||
@ -118,11 +118,11 @@ var LinkedList = (function () {
|
||||
// Adds the element at specified index
|
||||
LinkedList.prototype.addAt = function (index, element) {
|
||||
index--
|
||||
var node = new Node(element)
|
||||
const node = new Node(element)
|
||||
|
||||
var currentNode = this.head
|
||||
var previousNode
|
||||
var currentIndex = 0
|
||||
let currentNode = this.head
|
||||
let previousNode
|
||||
let currentIndex = 0
|
||||
|
||||
// Check if index is out of bounds of list
|
||||
if (index > this.length) {
|
||||
@ -153,9 +153,9 @@ var LinkedList = (function () {
|
||||
// Removes the node at specified index
|
||||
LinkedList.prototype.removeAt = function (index) {
|
||||
index--
|
||||
var currentNode = this.head
|
||||
var previousNode
|
||||
var currentIndex = 0
|
||||
let currentNode = this.head
|
||||
let previousNode
|
||||
let currentIndex = 0
|
||||
|
||||
// Check if index is present in list
|
||||
if (index < 0 || index >= this.length) {
|
||||
@ -181,8 +181,8 @@ var LinkedList = (function () {
|
||||
|
||||
// Function to view the LinkedList
|
||||
LinkedList.prototype.view = function () {
|
||||
var currentNode = this.head
|
||||
var count = 0
|
||||
let currentNode = this.head
|
||||
let count = 0
|
||||
while (count < this.length) {
|
||||
count++
|
||||
console.log(currentNode.element)
|
||||
@ -195,7 +195,7 @@ var LinkedList = (function () {
|
||||
}())
|
||||
|
||||
// Implementation of LinkedList
|
||||
var linklist = new LinkedList()
|
||||
const linklist = new LinkedList()
|
||||
linklist.add(2)
|
||||
linklist.add(5)
|
||||
linklist.add(8)
|
||||
|
@ -8,7 +8,7 @@
|
||||
// Functions: push, pop, peek, view, length
|
||||
|
||||
// Creates a stack constructor
|
||||
var Stack = (function () {
|
||||
const Stack = (function () {
|
||||
function Stack () {
|
||||
// The top of the Stack
|
||||
this.top = 0
|
||||
@ -29,7 +29,7 @@ var Stack = (function () {
|
||||
}
|
||||
|
||||
this.top--
|
||||
var result = this.stack[this.top]
|
||||
const result = this.stack[this.top]
|
||||
this.stack = this.stack.splice(0, this.top)
|
||||
return result
|
||||
}
|
||||
@ -46,14 +46,14 @@ var Stack = (function () {
|
||||
|
||||
// To see all the elements in the stack
|
||||
Stack.prototype.view = function () {
|
||||
for (var i = 0; i < this.top; i++) { console.log(this.stack[i]) }
|
||||
for (let i = 0; i < this.top; i++) { console.log(this.stack[i]) }
|
||||
}
|
||||
|
||||
return Stack
|
||||
}())
|
||||
|
||||
// Implementation
|
||||
var myStack = new Stack()
|
||||
const myStack = new Stack()
|
||||
|
||||
myStack.push(1)
|
||||
myStack.push(5)
|
||||
|
@ -11,7 +11,7 @@
|
||||
*/
|
||||
|
||||
// class Node
|
||||
var Node = (function () {
|
||||
const Node = (function () {
|
||||
// Node in the tree
|
||||
function Node (val) {
|
||||
this.value = val
|
||||
@ -67,7 +67,7 @@ var Node = (function () {
|
||||
}())
|
||||
|
||||
// class Tree
|
||||
var Tree = (function () {
|
||||
const Tree = (function () {
|
||||
function Tree () {
|
||||
// Just store the root
|
||||
this.root = null
|
||||
@ -103,7 +103,7 @@ var Tree = (function () {
|
||||
}())
|
||||
|
||||
// Implementation of BST
|
||||
var bst = new Tree()
|
||||
const bst = new Tree()
|
||||
bst.addValue(6)
|
||||
bst.addValue(3)
|
||||
bst.addValue(9)
|
||||
|
@ -1,4 +1,4 @@
|
||||
var TrieNode = function TrieNode (key, parent) {
|
||||
const TrieNode = function TrieNode (key, parent) {
|
||||
this.key = key
|
||||
this.count = 0
|
||||
this.children = Object.create(null)
|
||||
@ -20,7 +20,7 @@ Trie.findAllWords = function (root, word, output) {
|
||||
if (root.count > 0) {
|
||||
if (typeof output === 'object') { output.push({ word: word, count: root.count }) }
|
||||
}
|
||||
var key
|
||||
let key
|
||||
for (key in root.children) {
|
||||
word += key
|
||||
this.findAllWords(root.children[key], word, output)
|
||||
@ -34,9 +34,9 @@ Trie.prototype.insert = function (word) {
|
||||
this.root.count += 1
|
||||
return
|
||||
}
|
||||
var node = this.root
|
||||
var len = word.length
|
||||
var i
|
||||
let node = this.root
|
||||
const len = word.length
|
||||
let i
|
||||
for (i = 0; i < len; i++) {
|
||||
if (node.children[word.charAt(i)] === undefined) { node.children[word.charAt(i)] = new TrieNode(word.charAt(i), node) }
|
||||
node = node.children[word.charAt(i)]
|
||||
@ -46,9 +46,9 @@ Trie.prototype.insert = function (word) {
|
||||
|
||||
Trie.prototype.findPrefix = function (word) {
|
||||
if (typeof word !== 'string') return null
|
||||
var node = this.root
|
||||
var len = word.length
|
||||
var i
|
||||
let node = this.root
|
||||
const len = word.length
|
||||
let i
|
||||
// After end of this loop node will be at desired prefix
|
||||
for (i = 0; i < len; i++) {
|
||||
if (node.children[word.charAt(i)] === undefined) return null // No such prefix exists
|
||||
@ -69,9 +69,9 @@ Trie.prototype.remove = function (word, count) {
|
||||
return
|
||||
}
|
||||
|
||||
var child = this.root
|
||||
var len = word.length
|
||||
var i, key
|
||||
let child = this.root
|
||||
const len = word.length
|
||||
let i, key
|
||||
// child: node which is to be deleted
|
||||
for (i = 0; i < len; i++) {
|
||||
key = word.charAt(i)
|
||||
@ -93,9 +93,9 @@ Trie.prototype.remove = function (word, count) {
|
||||
}
|
||||
|
||||
Trie.prototype.findAllWords = function (prefix) {
|
||||
var output = []
|
||||
const output = []
|
||||
// find the node with provided prefix
|
||||
var node = this.findPrefix(prefix)
|
||||
const node = this.findPrefix(prefix)
|
||||
// No such prefix exists
|
||||
if (node === null) return output
|
||||
Trie.findAllWords(node, prefix, output)
|
||||
@ -104,7 +104,7 @@ Trie.prototype.findAllWords = function (prefix) {
|
||||
|
||||
Trie.prototype.contains = function (word) {
|
||||
// find the node with given prefix
|
||||
var node = this.findPrefix(word)
|
||||
const node = this.findPrefix(word)
|
||||
// No such word exists
|
||||
if (node === null || node.count === 0) return false
|
||||
return true
|
||||
@ -112,7 +112,7 @@ Trie.prototype.contains = function (word) {
|
||||
|
||||
Trie.prototype.findOccurences = function (word) {
|
||||
// find the node with given prefix
|
||||
var node = this.findPrefix(word)
|
||||
const node = this.findPrefix(word)
|
||||
// No such word exists
|
||||
if (node === null) return 0
|
||||
return node.count
|
||||
@ -120,7 +120,7 @@ Trie.prototype.findOccurences = function (word) {
|
||||
|
||||
// To test
|
||||
(function demo () {
|
||||
var x = new Trie()
|
||||
const x = new Trie()
|
||||
x.insert('sheldon')
|
||||
x.insert('hello')
|
||||
x.insert('anyword')
|
||||
|
Reference in New Issue
Block a user