Skip to content

Implementation of LinkedList #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 193 additions & 0 deletions JavaScript/MyLinkedList/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
//Constructor for list element - Node
function Node(prev, next, value) {
this.prev = prev;
this.next = next;
this.value = value;
}
//Constructor for LinkedList
function LinkedList() {
this.head = null;
}
//--------------------Add--------------------
//Push elements to the start of the array
LinkedList.prototype.unshift = (...args) => {
var current;
var prev = null;
var next = this.head;

for (let i = args.length - 1; i >= 0; i--) {
current = new Node(prev, next, args[i]);
next = current;
}

this.head = current;
};
//Push element to the end of array
LinkedList.prototype.push = (val) => {
if (this.head == null){
this.head = new Node(null, null, val);
} else {
let current = this.head;
while (current.next != null){
current = current.next;
}
var item = new Node(current, null, val);
current.next = item;
}
};
//Insert elements inside of the list on the index
LinkedList.prototype.insert = (index, value) => {
let current = this.head;
let i = 0;

while (current.next != null && index != i) {
current = current.next;
i++;
}

if (i == index){
if (i == 0){
this.head.prev = new Node(null, this.head, value);
this.head = this.head.prev;
} else {
var item = new Node(current.prev, current, value);
current.prev.next = item;
}
} else {
if (i == index - 1) {
current.next = new Node(current, null, value);
}
}
};
//Append elements to the end of list
LinkedList.prototype.append = (...args) => {
let current = this.head;
while (current.next != null) {
current = current.next;
}
for (let i = 0; i < args.length; i++) {
LinkedList.prototype.add(args[i]);
}
};
//--------------------Find--------------------
//Find first item with given value
LinkedList.prototype.findFirst = (value) => {
let current = this.head;

do {
if (current.value == value)
return true;

current = current.next;
} while(current != null);

return false;
};
//Returns array of all items with given value
LinkedList.prototype.findAll = (value) => {
let current = this.head;
let results = [];

do {
if (current.value == value)
results.push(current);

current = current.next;
} while(current != null);

return results;
};
//Find(if element is regular expression, we test if item is proper for it
//and call callback function, if not we compare it as ordinal value
//and call callback)
LinkedList.prototype.find = (value, callback) => {
let current = this.head;

var comparator;

if (value instanceof RegExp) {
comparator = (current) => { return value.test(current.value); };
} else {
comparator = (current) => { return current.value == value; };
}

do {
if (comparator(current)) {
callback(current.value);
}
current = current.next;
} while(current != null);
};
//Add array of elements to the end of list
LinkedList.prototype.append = (...args) => {
let current = this.head;
while (current.next != null) {
current = current.next;
}
for (let i = 0; i < args.length; i++) {
LinkedList.prototype.add(args[i]);
}
};
//--------------------Print--------------------
LinkedList.prototype.print = () => {
let current = this.head;
while (current != null) {
console.log(current.value);
current = current.next;
}
};
//--------------------Remove--------------------
//Removal from the start of list
LinkedList.prototype.shift = () => {
if (this.head == null){
return null;
}
else{
var returnVal = this.head.value;

if (this.head.next == null) {
this.head = null;
}
else {
this.head = this.head.next;
this.head.prev = null;
}
return returnVal;
}
}
//Removal from the end of list
LinkedList.prototype.pop = () => {
let current = this.head;
while (current.next != null) {
current = current.next;
}
current.prev.next = null;
return current.value;
};
//Removal of certain value in the list
LinkedList.prototype.erase = (value) => {
let current = this.head;
while (current != null) {
if (current.value == value)
break;
current = current.next;
}

if (current != null){
if (current.next == null) {
current.prev.next = null;
current.prev = null;
} else if (current.prev == null) {
this.head = current.next;
this.head.prev = null;
}
else{
current.prev.next = current.next;
current.next.prev = current.prev;
}
LinkedList.prototype.erase(value);
}
};

//Module exports
module.exports = LinkedList;
57 changes: 57 additions & 0 deletions JavaScript/MyLinkedList/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const LinkedList = require('./list.js');
const lst = new LinkedList();
//lst.push(9);
for (let i = 0; i < 10; i++){
lst.push(i);
}

//lst.push(9);

//lst.print();

// //Find first test
// console.log(lst.findFirst(0));
// console.log(lst.findFirst(4));
// console.log(lst.findFirst(9));
// console.log(lst.findFirst(20));

//Find all test
// console.log(lst.findAll(1));

//Find test
// lst.find(1, (value) => {
// console.log(value);
// });

//Append test
//lst.append(10,11,12,13,14);

//lst.add("vova");
// lst.print();

//Find with RegExp test
// lst.find(new RegExp("\w","g"), (value) => {
// console.log(value);
// });
//

// var retUnshift = lst.shift();
// console.log(retUnshift);
// var retPop = lst.pop();
// console.log(retPop);

//lst.erase(0);
//lst.erase(9);
// lst.insert(0, "Vova");
// lst.insert(4, "Vova1");
// lst.insert(12, "Vova2");
// lst.insert(12, "Vova3");

var number = 0;
lst.unshift("vova", "dima", "vitaliy", "oleksiy");
lst.find(/^[a-z]*$/, (item) => {
console.log(number);
number++;
});

//lst.print();
11 changes: 11 additions & 0 deletions JavaScript/MyLinkedList/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "lab1",
"version": "1.0.0",
"description": "the first laboratory work on NodeJS",
"main": "main.js",
"scripts": {
"test": "node main.js"
},
"author": "Volodymyr Novak",
"license": "ISC"
}