mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-12-19 06:58:15 +08:00
Fixing non compliant files (Ciphers, Conversions and Data Structures)
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
// Functions: insert, delete, peek, isEmpty, print, heapSort, sink
|
||||
|
||||
class MinPriorityQueue {
|
||||
// calss the constructor and initializes the capacity
|
||||
// calls the constructor and initializes the capacity
|
||||
constructor (c) {
|
||||
this.heap = []
|
||||
this.capacity = c
|
||||
@@ -43,13 +43,12 @@ class MinPriorityQueue {
|
||||
|
||||
// returns boolean value whether the heap is empty or not
|
||||
isEmpty () {
|
||||
if (this.size == 0) return true
|
||||
return false
|
||||
return this.size === 0
|
||||
}
|
||||
|
||||
// returns boolean value whether the heap is full or not
|
||||
isFull () {
|
||||
if (this.size == this.capacity) return true
|
||||
if (this.size === this.capacity) return true
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -111,7 +110,7 @@ class MinPriorityQueue {
|
||||
}
|
||||
|
||||
// testing
|
||||
q = new MinPriorityQueue(8)
|
||||
const q = new MinPriorityQueue(8)
|
||||
|
||||
q.insert(5)
|
||||
q.insert(2)
|
||||
|
||||
@@ -1,198 +1,202 @@
|
||||
//Hamza chabchoub contribution for a university project
|
||||
function doubleLinkedList() {
|
||||
let 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) {
|
||||
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++;
|
||||
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;
|
||||
}
|
||||
|
||||
length--;
|
||||
return current.element;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
return string;
|
||||
};
|
||||
|
||||
//Convert list to array
|
||||
this.toArray = function(){
|
||||
let arr = [],
|
||||
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;
|
||||
}
|
||||
}
|
||||
// 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
|
||||
}
|
||||
|
||||
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 = 0
|
||||
let 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++
|
||||
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
|
||||
let previous = 0
|
||||
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
|
||||
}
|
||||
} 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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
let string = ''
|
||||
|
||||
while (current) {
|
||||
string += current.element + (current.next ? '\n' : '')
|
||||
current = current.next
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
const newDoubleLinkedList = new DoubleLinkedList()
|
||||
newDoubleLinkedList.append(1)
|
||||
newDoubleLinkedList.append(2)
|
||||
console.log('Testing: ' + newDoubleLinkedList.size()) // returns 2
|
||||
|
||||
@@ -23,7 +23,7 @@ var Queue = (function () {
|
||||
// Removes the value at the front of the queue
|
||||
Queue.prototype.dequeue = function () {
|
||||
if (this.queue.length === 0) {
|
||||
throw 'Queue is Empty'
|
||||
throw new Error('Queue is Empty')
|
||||
}
|
||||
|
||||
var result = this.queue[0]
|
||||
|
||||
@@ -13,7 +13,7 @@ var Stack = (function () {
|
||||
// The top of the Stack
|
||||
this.top = 0
|
||||
// The array representation of the stack
|
||||
this.stack = new Array()
|
||||
this.stack = []
|
||||
}
|
||||
|
||||
// Adds a value onto the end of the stack
|
||||
|
||||
@@ -21,7 +21,7 @@ var Node = (function () {
|
||||
|
||||
// Search the tree for a value
|
||||
Node.prototype.search = function (val) {
|
||||
if (this.value == val) {
|
||||
if (this.value === val) {
|
||||
return this
|
||||
} else if (val < this.value && this.left != null) {
|
||||
return this.left.search(val)
|
||||
|
||||
Reference in New Issue
Block a user