mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
merge: replaced constructor function with es6 class syntax (#900)
* replaced constructor function with es6 class syntax * formatted code with standard.js
This commit is contained in:
@ -1,54 +1,57 @@
|
|||||||
// Hamza chabchoub contribution for a university project
|
class Node {
|
||||||
function DoubleLinkedList () {
|
constructor (element) {
|
||||||
const Node = function (element) {
|
|
||||||
this.element = element
|
this.element = element
|
||||||
this.next = null
|
this.next = null
|
||||||
this.prev = null
|
this.prev = null
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let length = 0
|
class DoubleLinkedList {
|
||||||
let head = null
|
constructor () {
|
||||||
let tail = null
|
this.length = 0
|
||||||
|
this.head = null
|
||||||
// Add new element
|
this.tail = null
|
||||||
this.append = function (element) {
|
|
||||||
const node = new Node(element)
|
|
||||||
|
|
||||||
if (!head) {
|
|
||||||
head = node
|
|
||||||
tail = node
|
|
||||||
} else {
|
|
||||||
node.prev = tail
|
|
||||||
tail.next = node
|
|
||||||
tail = node
|
|
||||||
}
|
}
|
||||||
|
|
||||||
length++
|
// Add new element
|
||||||
|
append (element) {
|
||||||
|
const node = new Node(element)
|
||||||
|
|
||||||
|
if (!this.head) {
|
||||||
|
this.head = node
|
||||||
|
this.tail = node
|
||||||
|
} else {
|
||||||
|
node.prev = this.tail
|
||||||
|
this.tail.next = node
|
||||||
|
this.tail = node
|
||||||
|
}
|
||||||
|
|
||||||
|
this.length++
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add element
|
// Add element
|
||||||
this.insert = function (position, element) {
|
insert (position, element) {
|
||||||
// Check of out-of-bound values
|
// Check of out-of-bound values
|
||||||
if (position >= 0 && position <= length) {
|
if (position >= 0 && position <= this.length) {
|
||||||
const node = new Node(element)
|
const node = new Node(element)
|
||||||
let current = head
|
let current = this.head
|
||||||
let previous = 0
|
let previous = 0
|
||||||
let index = 0
|
let index = 0
|
||||||
|
|
||||||
if (position === 0) {
|
if (position === 0) {
|
||||||
if (!head) {
|
if (!this.head) {
|
||||||
head = node
|
this.head = node
|
||||||
tail = node
|
this.tail = node
|
||||||
} else {
|
} else {
|
||||||
node.next = current
|
node.next = current
|
||||||
current.prev = node
|
current.prev = node
|
||||||
head = node
|
this.head = node
|
||||||
}
|
}
|
||||||
} else if (position === length) {
|
} else if (position === this.length) {
|
||||||
current = tail
|
current = this.tail
|
||||||
current.next = node
|
current.next = node
|
||||||
node.prev = current
|
node.prev = current
|
||||||
tail = node
|
this.tail = node
|
||||||
} else {
|
} else {
|
||||||
while (index++ < position) {
|
while (index++ < position) {
|
||||||
previous = current
|
previous = current
|
||||||
@ -63,7 +66,7 @@ function DoubleLinkedList () {
|
|||||||
node.prev = previous
|
node.prev = previous
|
||||||
}
|
}
|
||||||
|
|
||||||
length++
|
this.length++
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
return false
|
return false
|
||||||
@ -71,27 +74,27 @@ function DoubleLinkedList () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remove element at any position
|
// Remove element at any position
|
||||||
this.removeAt = function (position) {
|
removeAt (position) {
|
||||||
// look for out-of-bounds value
|
// look for out-of-bounds value
|
||||||
if (position > -1 && position < length) {
|
if (position > -1 && position < this.length) {
|
||||||
let current = head
|
let current = this.head
|
||||||
let previous = 0
|
let previous = 0
|
||||||
let index = 0
|
let index = 0
|
||||||
|
|
||||||
// Removing first item
|
// Removing first item
|
||||||
if (position === 0) {
|
if (position === 0) {
|
||||||
head = current.next
|
this.head = current.next
|
||||||
|
|
||||||
// if there is only one item, update tail //NEW
|
// if there is only one item, update this.tail //NEW
|
||||||
if (length === 1) {
|
if (this.length === 1) {
|
||||||
tail = null
|
this.tail = null
|
||||||
} else {
|
} else {
|
||||||
head.prev = null
|
this.head.prev = null
|
||||||
}
|
}
|
||||||
} else if (position === length - 1) {
|
} else if (position === this.length - 1) {
|
||||||
current = tail
|
current = this.tail
|
||||||
tail = current.prev
|
this.tail = current.prev
|
||||||
tail.next = null
|
this.tail.next = null
|
||||||
} else {
|
} else {
|
||||||
while (index++ < position) {
|
while (index++ < position) {
|
||||||
previous = current
|
previous = current
|
||||||
@ -103,7 +106,7 @@ function DoubleLinkedList () {
|
|||||||
current.next.prev = previous
|
current.next.prev = previous
|
||||||
}
|
}
|
||||||
|
|
||||||
length--
|
this.length--
|
||||||
return current.element
|
return current.element
|
||||||
} else {
|
} else {
|
||||||
return null
|
return null
|
||||||
@ -111,8 +114,8 @@ function DoubleLinkedList () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the indexOf item
|
// Get the indexOf item
|
||||||
this.indexOf = function (elm) {
|
indexOf (elm) {
|
||||||
let current = head
|
let current = this.head
|
||||||
let index = -1
|
let index = -1
|
||||||
|
|
||||||
// If element found then return its position
|
// If element found then return its position
|
||||||
@ -130,28 +133,28 @@ function DoubleLinkedList () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find the item in the list
|
// Find the item in the list
|
||||||
this.isPresent = (elm) => {
|
isPresent (elm) {
|
||||||
return this.indexOf(elm) !== -1
|
return this.indexOf(elm) !== -1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete an item from the list
|
// Delete an item from the list
|
||||||
this.delete = (elm) => {
|
delete (elm) {
|
||||||
return this.removeAt(this.indexOf(elm))
|
return this.removeAt(this.indexOf(elm))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete first item from the list
|
// Delete first item from the list
|
||||||
this.deleteHead = function () {
|
deleteHead () {
|
||||||
this.removeAt(0)
|
this.removeAt(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete last item from the list
|
// Delete last item from the list
|
||||||
this.deleteTail = function () {
|
deleteTail () {
|
||||||
this.removeAt(length - 1)
|
this.removeAt(this.length - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print item of the string
|
// Print item of the string
|
||||||
this.toString = function () {
|
toString () {
|
||||||
let current = head
|
let current = this.head
|
||||||
let string = ''
|
let string = ''
|
||||||
|
|
||||||
while (current) {
|
while (current) {
|
||||||
@ -163,9 +166,9 @@ function DoubleLinkedList () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Convert list to array
|
// Convert list to array
|
||||||
this.toArray = function () {
|
toArray () {
|
||||||
const arr = []
|
const arr = []
|
||||||
let current = head
|
let current = this.head
|
||||||
|
|
||||||
while (current) {
|
while (current) {
|
||||||
arr.push(current.element)
|
arr.push(current.element)
|
||||||
@ -176,27 +179,27 @@ function DoubleLinkedList () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if list is empty
|
// Check if list is empty
|
||||||
this.isEmpty = function () {
|
isEmpty () {
|
||||||
return length === 0
|
return this.length === 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the size of the list
|
// Get the size of the list
|
||||||
this.size = function () {
|
size () {
|
||||||
return length
|
return this.length
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the head
|
// Get the this.head
|
||||||
this.getHead = function () {
|
getHead () {
|
||||||
return head
|
return this.head
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the tail
|
// Get the this.tail
|
||||||
this.getTail = function () {
|
getTail () {
|
||||||
return tail
|
return this.tail
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method to iterate over the LinkedList
|
// Method to iterate over the LinkedList
|
||||||
this.iterator = function () {
|
iterator () {
|
||||||
let currentNode = this.getHead()
|
let currentNode = this.getHead()
|
||||||
if (currentNode === null) return -1
|
if (currentNode === null) return -1
|
||||||
|
|
||||||
@ -211,7 +214,7 @@ function DoubleLinkedList () {
|
|||||||
|
|
||||||
// Method to log the LinkedList, for debugging
|
// Method to log the LinkedList, for debugging
|
||||||
// it' a circular structure, so can't use stringify to debug the whole structure
|
// it' a circular structure, so can't use stringify to debug the whole structure
|
||||||
this.log = function () {
|
log () {
|
||||||
let currentNode = this.getHead()
|
let currentNode = this.getHead()
|
||||||
while (currentNode) {
|
while (currentNode) {
|
||||||
console.log(currentNode.element)
|
console.log(currentNode.element)
|
||||||
|
Reference in New Issue
Block a user