From 6fafdda0f4a0bf96f78559aff172ad84b4e230dd Mon Sep 17 00:00:00 2001 From: hmizz Date: Sun, 26 Apr 2020 23:25:27 +0100 Subject: [PATCH 1/4] doublylinkedlist --- .../Linked List/DoublyLinkedList.js | 199 ++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 Data Structures/Linked List/DoublyLinkedList.js diff --git a/Data Structures/Linked List/DoublyLinkedList.js b/Data Structures/Linked List/DoublyLinkedList.js new file mode 100644 index 000000000..00931a2d7 --- /dev/null +++ b/Data Structures/Linked List/DoublyLinkedList.js @@ -0,0 +1,199 @@ +//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), + current = head, + previous; + + 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; + } + } \ No newline at end of file From 992553944b5211895feda04b73b0044590529afe Mon Sep 17 00:00:00 2001 From: hmizz Date: Sun, 26 Apr 2020 23:52:50 +0100 Subject: [PATCH 2/4] add-doublylinkedlist --- DIRECTORY.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index ef58990d1..3a603c7ab 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -23,13 +23,15 @@ * [MinPriorityQueue](https://github.com/TheAlgorithms/Javascript/blob/master/Data%20Structures/Heap/MinPriorityQueue.js) * Linked List * [singlylinklist](https://github.com/TheAlgorithms/Javascript/blob/master/Data%20Structures/Linked%20List/singlylinklist.js) + * [doublylinkedlist]C:\Users\Hamza\Desktop\junior prjct\Javascript\Data Structures\Linked List\DoublyLinkedList.js * Queue * [Queue](https://github.com/TheAlgorithms/Javascript/blob/master/Data%20Structures/Queue/Queue.js) * Stack * [Stack](https://github.com/TheAlgorithms/Javascript/blob/master/Data%20Structures/Stack/Stack.js) * Tree * [Binary Search Tree](https://github.com/TheAlgorithms/Javascript/blob/master/Data%20Structures/Tree/Binary%20Search%20Tree.js) - + + ## Hashes * [SHA1](https://github.com/TheAlgorithms/Javascript/blob/master/Hashes/SHA1.js) * [SHA256](https://github.com/TheAlgorithms/Javascript/blob/master/Hashes/SHA256.js) From db6d24556ab7579d02ce7b1c6e8658095e5d3136 Mon Sep 17 00:00:00 2001 From: hmizz Date: Mon, 27 Apr 2020 00:20:00 +0100 Subject: [PATCH 3/4] add-doublylinkedlist --- Data Structures/Linked List/DoublyLinkedList.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Data Structures/Linked List/DoublyLinkedList.js b/Data Structures/Linked List/DoublyLinkedList.js index 00931a2d7..aca5b7eb7 100644 --- a/Data Structures/Linked List/DoublyLinkedList.js +++ b/Data Structures/Linked List/DoublyLinkedList.js @@ -12,9 +12,7 @@ function doubleLinkedList() { //Add new element this.append = function(element) { - let node = new Node(element), - current = head, - previous; + let node = new Node(element); if(!head){ head = node; From afc870ec2cd7d589e7baaf37574d66b07c576374 Mon Sep 17 00:00:00 2001 From: hmizz Date: Wed, 29 Apr 2020 11:01:21 +0100 Subject: [PATCH 4/4] change in Directory.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 3a603c7ab..bbdebf098 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -23,7 +23,7 @@ * [MinPriorityQueue](https://github.com/TheAlgorithms/Javascript/blob/master/Data%20Structures/Heap/MinPriorityQueue.js) * Linked List * [singlylinklist](https://github.com/TheAlgorithms/Javascript/blob/master/Data%20Structures/Linked%20List/singlylinklist.js) - * [doublylinkedlist]C:\Users\Hamza\Desktop\junior prjct\Javascript\Data Structures\Linked List\DoublyLinkedList.js + * [doublylinkedlist](https://github.com/TheAlgorithms/Javascript/blob/master/Data%20Structures/Linked%20List/doublylinkedlist.js) * Queue * [Queue](https://github.com/TheAlgorithms/Javascript/blob/master/Data%20Structures/Queue/Queue.js) * Stack