mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-03-13 15:21:15 +08:00
npx standard --fix
This commit is contained in:
@@ -1,197 +1,195 @@
|
||||
//Hamza chabchoub contribution for a university project
|
||||
function doubleLinkedList() {
|
||||
let Node = function(element) {
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
this.prev = null;
|
||||
// Hamza chabchoub contribution for a university project
|
||||
function doubleLinkedList () {
|
||||
const Node = function (element) {
|
||||
this.element = element
|
||||
this.next = null
|
||||
this.prev = null
|
||||
}
|
||||
|
||||
let length = 0
|
||||
let head = null
|
||||
let tail = null
|
||||
|
||||
// Add new element
|
||||
this.append = function (element) {
|
||||
const node = new Node(element)
|
||||
|
||||
if (!head) {
|
||||
head = node
|
||||
tail = node
|
||||
} else {
|
||||
node.prev = tail
|
||||
tail.next = node
|
||||
tail = node
|
||||
}
|
||||
|
||||
let length = 0;
|
||||
let head = null;
|
||||
let tail = null;
|
||||
|
||||
//Add new element
|
||||
this.append = function(element) {
|
||||
let node = new Node(element);
|
||||
|
||||
if(!head){
|
||||
head = node;
|
||||
tail = node;
|
||||
}else{
|
||||
node.prev = tail;
|
||||
tail.next = node;
|
||||
tail = node;
|
||||
}
|
||||
|
||||
length++;
|
||||
}
|
||||
|
||||
|
||||
//Add element
|
||||
this.insert = function(position, element) {
|
||||
|
||||
//Check of out-of-bound values
|
||||
if(position >= 0 && position <= length){
|
||||
let node = new Node(element),
|
||||
current = head,
|
||||
previous,
|
||||
index = 0;
|
||||
|
||||
if(position === 0){
|
||||
if(!head){
|
||||
head = node;
|
||||
tail = node;
|
||||
}else{
|
||||
node.next = current;
|
||||
current.prev = node;
|
||||
head = node;
|
||||
}
|
||||
}else if(position === length){
|
||||
current = tail;
|
||||
current.next = node;
|
||||
node.prev = current;
|
||||
tail = node;
|
||||
}else{
|
||||
while(index++ < position){
|
||||
previous = current;
|
||||
current = current.next;
|
||||
}
|
||||
|
||||
node.next = current;
|
||||
previous.next = node;
|
||||
|
||||
//New
|
||||
current.prev = node;
|
||||
node.prev = previous;
|
||||
|
||||
length++
|
||||
}
|
||||
|
||||
// Add element
|
||||
this.insert = function (position, element) {
|
||||
// Check of out-of-bound values
|
||||
if (position >= 0 && position <= length) {
|
||||
const node = new Node(element)
|
||||
let current = head
|
||||
let previous
|
||||
let index = 0
|
||||
|
||||
if (position === 0) {
|
||||
if (!head) {
|
||||
head = node
|
||||
tail = node
|
||||
} else {
|
||||
node.next = current
|
||||
current.prev = node
|
||||
head = node
|
||||
}
|
||||
|
||||
length++;
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//Remove element at any position
|
||||
this.removeAt = function(position){
|
||||
//look for out-of-bounds value
|
||||
if(position > -1 && position < length){
|
||||
let current = head, previous, index = 0;
|
||||
|
||||
//Removing first item
|
||||
if(position === 0){
|
||||
head = current.next;
|
||||
|
||||
//if there is only one item, update tail //NEW
|
||||
if(length === 1){
|
||||
tail = null;
|
||||
}else{
|
||||
head.prev = null;
|
||||
}
|
||||
}else if(position === length - 1){
|
||||
current = tail;
|
||||
tail = current.prev;
|
||||
tail.next = null;
|
||||
}else{
|
||||
while(index++ < position){
|
||||
previous = current;
|
||||
current = current.next;
|
||||
}
|
||||
|
||||
//link previous with current's next - skip it
|
||||
previous.next = current.next;
|
||||
current.next.prev = previous;
|
||||
} else if (position === length) {
|
||||
current = tail
|
||||
current.next = node
|
||||
node.prev = current
|
||||
tail = node
|
||||
} else {
|
||||
while (index++ < position) {
|
||||
previous = current
|
||||
current = current.next
|
||||
}
|
||||
|
||||
length--;
|
||||
return current.element;
|
||||
}else{
|
||||
return null;
|
||||
|
||||
node.next = current
|
||||
previous.next = node
|
||||
|
||||
// New
|
||||
current.prev = node
|
||||
node.prev = previous
|
||||
}
|
||||
|
||||
length++
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
|
||||
//Get the indexOf item
|
||||
this.indexOf = function(elm){
|
||||
let current = head,
|
||||
index = -1;
|
||||
|
||||
//If element found then return its position
|
||||
while(current){
|
||||
if(elm === current.element){
|
||||
return ++index;
|
||||
}
|
||||
|
||||
// Remove element at any position
|
||||
this.removeAt = function (position) {
|
||||
// look for out-of-bounds value
|
||||
if (position > -1 && position < length) {
|
||||
let current = head; let previous; let index = 0
|
||||
|
||||
// Removing first item
|
||||
if (position === 0) {
|
||||
head = current.next
|
||||
|
||||
// if there is only one item, update tail //NEW
|
||||
if (length === 1) {
|
||||
tail = null
|
||||
} else {
|
||||
head.prev = null
|
||||
}
|
||||
|
||||
index++;
|
||||
current = current.next;
|
||||
}
|
||||
|
||||
//Else return -1
|
||||
return -1;
|
||||
};
|
||||
|
||||
//Find the item in the list
|
||||
this.isPresent = (elm) => {
|
||||
return this.indexOf(elm) !== -1;
|
||||
};
|
||||
|
||||
//Delete an item from the list
|
||||
this.delete = (elm) => {
|
||||
return this.removeAt(this.indexOf(elm));
|
||||
};
|
||||
|
||||
//Delete first item from the list
|
||||
this.deleteHead = function(){
|
||||
this.removeAt(0);
|
||||
}
|
||||
|
||||
//Delete last item from the list
|
||||
this.deleteTail = function(){
|
||||
this.removeAt(length-1);
|
||||
}
|
||||
|
||||
//Print item of the string
|
||||
this.toString = function(){
|
||||
let current = head,
|
||||
string = '';
|
||||
|
||||
while(current){
|
||||
string += current.element + (current.next ? '\n' : '');
|
||||
current = current.next;
|
||||
} else if (position === length - 1) {
|
||||
current = tail
|
||||
tail = current.prev
|
||||
tail.next = null
|
||||
} else {
|
||||
while (index++ < position) {
|
||||
previous = current
|
||||
current = current.next
|
||||
}
|
||||
|
||||
// link previous with current's next - skip it
|
||||
previous.next = current.next
|
||||
current.next.prev = previous
|
||||
}
|
||||
|
||||
return string;
|
||||
};
|
||||
|
||||
//Convert list to array
|
||||
this.toArray = function(){
|
||||
let arr = [],
|
||||
current = head;
|
||||
|
||||
while(current){
|
||||
arr.push(current.element);
|
||||
current = current.next;
|
||||
|
||||
length--
|
||||
return current.element
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// Get the indexOf item
|
||||
this.indexOf = function (elm) {
|
||||
let current = head
|
||||
let index = -1
|
||||
|
||||
// If element found then return its position
|
||||
while (current) {
|
||||
if (elm === current.element) {
|
||||
return ++index
|
||||
}
|
||||
|
||||
return arr;
|
||||
};
|
||||
|
||||
//Check if list is empty
|
||||
this.isEmpty = function(){
|
||||
return length === 0;
|
||||
};
|
||||
|
||||
//Get the size of the list
|
||||
this.size = function(){
|
||||
return length;
|
||||
|
||||
index++
|
||||
current = current.next
|
||||
}
|
||||
|
||||
//Get the head
|
||||
this.getHead = function() {
|
||||
return head;
|
||||
|
||||
// Else return -1
|
||||
return -1
|
||||
}
|
||||
|
||||
// Find the item in the list
|
||||
this.isPresent = (elm) => {
|
||||
return this.indexOf(elm) !== -1
|
||||
}
|
||||
|
||||
// Delete an item from the list
|
||||
this.delete = (elm) => {
|
||||
return this.removeAt(this.indexOf(elm))
|
||||
}
|
||||
|
||||
// Delete first item from the list
|
||||
this.deleteHead = function () {
|
||||
this.removeAt(0)
|
||||
}
|
||||
|
||||
// Delete last item from the list
|
||||
this.deleteTail = function () {
|
||||
this.removeAt(length - 1)
|
||||
}
|
||||
|
||||
// Print item of the string
|
||||
this.toString = function () {
|
||||
let current = head
|
||||
let string = ''
|
||||
|
||||
while (current) {
|
||||
string += current.element + (current.next ? '\n' : '')
|
||||
current = current.next
|
||||
}
|
||||
|
||||
//Get the tail
|
||||
this.getTail = function() {
|
||||
return tail;
|
||||
|
||||
return string
|
||||
}
|
||||
|
||||
// Convert list to array
|
||||
this.toArray = function () {
|
||||
const arr = []
|
||||
let current = head
|
||||
|
||||
while (current) {
|
||||
arr.push(current.element)
|
||||
current = current.next
|
||||
}
|
||||
}
|
||||
|
||||
return arr
|
||||
}
|
||||
|
||||
// Check if list is empty
|
||||
this.isEmpty = function () {
|
||||
return length === 0
|
||||
}
|
||||
|
||||
// Get the size of the list
|
||||
this.size = function () {
|
||||
return length
|
||||
}
|
||||
|
||||
// Get the head
|
||||
this.getHead = function () {
|
||||
return head
|
||||
}
|
||||
|
||||
// Get the tail
|
||||
this.getTail = function () {
|
||||
return tail
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,212 +6,204 @@
|
||||
* a singly linked list.
|
||||
*/
|
||||
|
||||
//Functions - add, remove, indexOf, elementAt, addAt, removeAt, view
|
||||
// Functions - add, remove, indexOf, elementAt, addAt, removeAt, view
|
||||
|
||||
// class LinkedList and constructor
|
||||
//Creates a LinkedList
|
||||
// Creates a LinkedList
|
||||
var LinkedList = (function () {
|
||||
|
||||
function LinkedList() {
|
||||
//Length of linklist and head is null at start
|
||||
this.length = 0;
|
||||
this.head = null;
|
||||
|
||||
function LinkedList () {
|
||||
// Length of linklist and head is null at start
|
||||
this.length = 0
|
||||
this.head = null
|
||||
}
|
||||
|
||||
// class node (constructor)
|
||||
//Creating Node with element's value
|
||||
// Creating Node with element's value
|
||||
var Node = (function () {
|
||||
function Node(element) {
|
||||
this.element = element;
|
||||
this.next = null;
|
||||
function Node (element) {
|
||||
this.element = element
|
||||
this.next = null
|
||||
}
|
||||
return Node;
|
||||
}());
|
||||
return Node
|
||||
}())
|
||||
|
||||
//Returns length
|
||||
// Returns length
|
||||
LinkedList.prototype.size = function () {
|
||||
return this.length;
|
||||
};
|
||||
return this.length
|
||||
}
|
||||
|
||||
//Returns the head
|
||||
// Returns the head
|
||||
LinkedList.prototype.head = function () {
|
||||
return this.head;
|
||||
};
|
||||
return this.head
|
||||
}
|
||||
|
||||
//Creates a node and adds it to linklist
|
||||
// Creates a node and adds it to linklist
|
||||
LinkedList.prototype.add = function (element) {
|
||||
var node = new Node(element);
|
||||
//Check if its the first element
|
||||
var node = new Node(element)
|
||||
// Check if its the first element
|
||||
if (this.head === null) {
|
||||
this.head = node;
|
||||
}
|
||||
else {
|
||||
var currentNode = this.head;
|
||||
this.head = node
|
||||
} else {
|
||||
var currentNode = this.head
|
||||
|
||||
//Loop till there is node present in the list
|
||||
// Loop till there is node present in the list
|
||||
while (currentNode.next) {
|
||||
currentNode = currentNode.next;
|
||||
currentNode = currentNode.next
|
||||
}
|
||||
|
||||
//Adding node to the end of the list
|
||||
currentNode.next = node;
|
||||
// Adding node to the end of the list
|
||||
currentNode.next = node
|
||||
}
|
||||
//Increment the length
|
||||
this.length++;
|
||||
};
|
||||
// Increment the length
|
||||
this.length++
|
||||
}
|
||||
|
||||
//Removes the node with the value as param
|
||||
// Removes the node with the value as param
|
||||
LinkedList.prototype.remove = function (element) {
|
||||
var currentNode = this.head;
|
||||
var previousNode;
|
||||
var currentNode = this.head
|
||||
var previousNode
|
||||
|
||||
//Check if the head node is the element to remove
|
||||
// Check if the head node is the element to remove
|
||||
if (currentNode.element === element) {
|
||||
this.head = currentNode.next;
|
||||
}
|
||||
else {
|
||||
|
||||
//Check which node is the node to remove
|
||||
this.head = currentNode.next
|
||||
} else {
|
||||
// Check which node is the node to remove
|
||||
while (currentNode.element !== element) {
|
||||
previousNode = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
previousNode = currentNode
|
||||
currentNode = currentNode.next
|
||||
}
|
||||
|
||||
//Removing the currentNode
|
||||
previousNode.next = currentNode.next;
|
||||
// Removing the currentNode
|
||||
previousNode.next = currentNode.next
|
||||
}
|
||||
|
||||
//Decrementing the length
|
||||
this.length--;
|
||||
};
|
||||
// Decrementing the length
|
||||
this.length--
|
||||
}
|
||||
|
||||
//Return if the list is empty
|
||||
// Return if the list is empty
|
||||
LinkedList.prototype.isEmpty = function () {
|
||||
return this.length === 0;
|
||||
};
|
||||
return this.length === 0
|
||||
}
|
||||
|
||||
//Returns the index of the element passed as param otherwise -1
|
||||
// Returns the index of the element passed as param otherwise -1
|
||||
LinkedList.prototype.indexOf = function (element) {
|
||||
var currentNode = this.head;
|
||||
var index = -1;
|
||||
var currentNode = this.head
|
||||
var index = -1
|
||||
|
||||
while (currentNode) {
|
||||
index++;
|
||||
index++
|
||||
|
||||
//Checking if the node is the element we are searching for
|
||||
// Checking if the node is the element we are searching for
|
||||
if (currentNode.element === element) {
|
||||
return index + 1;
|
||||
return index + 1
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
currentNode = currentNode.next
|
||||
}
|
||||
|
||||
return -1;
|
||||
};
|
||||
return -1
|
||||
}
|
||||
|
||||
//Returns the element at an index
|
||||
// Returns the element at an index
|
||||
LinkedList.prototype.elementAt = function (index) {
|
||||
var currentNode = this.head;
|
||||
var count = 0;
|
||||
var currentNode = this.head
|
||||
var count = 0
|
||||
while (count < index) {
|
||||
count++;
|
||||
currentNode = currentNode.next;
|
||||
count++
|
||||
currentNode = currentNode.next
|
||||
}
|
||||
return currentNode.element;
|
||||
};
|
||||
return currentNode.element
|
||||
}
|
||||
|
||||
//Adds the element at specified index
|
||||
// Adds the element at specified index
|
||||
LinkedList.prototype.addAt = function (index, element) {
|
||||
index--;
|
||||
var node = new Node(element);
|
||||
index--
|
||||
var node = new Node(element)
|
||||
|
||||
var currentNode = this.head;
|
||||
var previousNode;
|
||||
var currentIndex = 0;
|
||||
var currentNode = this.head
|
||||
var previousNode
|
||||
var currentIndex = 0
|
||||
|
||||
//Check if index is out of bounds of list
|
||||
// Check if index is out of bounds of list
|
||||
if (index > this.length) {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
|
||||
//Check if index is the start of list
|
||||
// Check if index is the start of list
|
||||
if (index === 0) {
|
||||
node.next = currentNode;
|
||||
this.head = node;
|
||||
}
|
||||
else {
|
||||
node.next = currentNode
|
||||
this.head = node
|
||||
} else {
|
||||
while (currentIndex < index) {
|
||||
currentIndex++;
|
||||
previousNode = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
currentIndex++
|
||||
previousNode = currentNode
|
||||
currentNode = currentNode.next
|
||||
}
|
||||
|
||||
//Adding the node at specified index
|
||||
node.next = currentNode;
|
||||
previousNode.next = node;
|
||||
// Adding the node at specified index
|
||||
node.next = currentNode
|
||||
previousNode.next = node
|
||||
}
|
||||
|
||||
//Incrementing the length
|
||||
this.length++;
|
||||
return true;
|
||||
};
|
||||
// Incrementing the length
|
||||
this.length++
|
||||
return true
|
||||
}
|
||||
|
||||
//Removes the node at specified index
|
||||
// Removes the node at specified index
|
||||
LinkedList.prototype.removeAt = function (index) {
|
||||
index--;
|
||||
var currentNode = this.head;
|
||||
var previousNode;
|
||||
var currentIndex = 0;
|
||||
index--
|
||||
var currentNode = this.head
|
||||
var previousNode
|
||||
var currentIndex = 0
|
||||
|
||||
//Check if index is present in list
|
||||
// Check if index is present in list
|
||||
if (index < 0 || index >= this.length) {
|
||||
return null;
|
||||
return null
|
||||
}
|
||||
|
||||
//Check if element is the first element
|
||||
// Check if element is the first element
|
||||
if (index === 0) {
|
||||
this.head = currentNode.next;
|
||||
}
|
||||
else {
|
||||
this.head = currentNode.next
|
||||
} else {
|
||||
while (currentIndex < index) {
|
||||
currentIndex++;
|
||||
previousNode = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
currentIndex++
|
||||
previousNode = currentNode
|
||||
currentNode = currentNode.next
|
||||
}
|
||||
previousNode.next = currentNode.next;
|
||||
previousNode.next = currentNode.next
|
||||
}
|
||||
|
||||
//Decrementing the length
|
||||
this.length--;
|
||||
return currentNode.element;
|
||||
};
|
||||
// Decrementing the length
|
||||
this.length--
|
||||
return currentNode.element
|
||||
}
|
||||
|
||||
//Function to view the LinkedList
|
||||
// Function to view the LinkedList
|
||||
LinkedList.prototype.view = function () {
|
||||
var currentNode = this.head;
|
||||
var count = 0;
|
||||
var currentNode = this.head
|
||||
var count = 0
|
||||
while (count < this.length) {
|
||||
count++;
|
||||
console.log(currentNode.element);
|
||||
currentNode = currentNode.next;
|
||||
count++
|
||||
console.log(currentNode.element)
|
||||
currentNode = currentNode.next
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// returns the constructor
|
||||
return LinkedList;
|
||||
return LinkedList
|
||||
}())
|
||||
|
||||
}());
|
||||
|
||||
//Implementation of LinkedList
|
||||
var linklist = new LinkedList();
|
||||
linklist.add(2);
|
||||
linklist.add(5);
|
||||
linklist.add(8);
|
||||
linklist.add(12);
|
||||
linklist.add(17);
|
||||
console.log(linklist.size());
|
||||
console.log(linklist.removeAt(4));
|
||||
linklist.addAt(4, 15);
|
||||
console.log(linklist.indexOf(8));
|
||||
console.log(linklist.size());
|
||||
linklist.view();
|
||||
// Implementation of LinkedList
|
||||
var linklist = new LinkedList()
|
||||
linklist.add(2)
|
||||
linklist.add(5)
|
||||
linklist.add(8)
|
||||
linklist.add(12)
|
||||
linklist.add(17)
|
||||
console.log(linklist.size())
|
||||
console.log(linklist.removeAt(4))
|
||||
linklist.addAt(4, 15)
|
||||
console.log(linklist.indexOf(8))
|
||||
console.log(linklist.size())
|
||||
linklist.view()
|
||||
|
||||
Reference in New Issue
Block a user