Skip to content

Implemented with ES6 classes, additional functionality. #7

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 3 commits into
base: master
Choose a base branch
from
Open
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
139 changes: 139 additions & 0 deletions JavaScript/3-classes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
'use strict';

class Node {
constructor(list, name, data) {
this.list = list;
this.data = data;
this.name = name;
this.prev = null;
this.next = null;
}
}

class LinkedList {

constructor() {
this.first = null;
this.last = null;
this.length = 0;
}

push(name, data) {
const node = new Node(this, name, data);
node.prev = this.last;
if (this.length === 0) {
this.first = node;
} else {
this.last.next = node;
}
this.last = node;
this.length++;
return node;
}

pop() {
if (this.length > 0) {
const node = this.last;
this.last = node.prev;
node.list = null;
node.prev = null;
node.next = null;
this.length--;
return node.data;
}

}

itemAtPos(pos) {

let current = this.first;
let counter = 0;

if (!(this.length) || pos < 0 || pos >= this.length) {
return false;
}

while (counter < pos) {
current = current.next;
++counter;
}

return current;
}

remove(remPos) {
let current = this.first;
let position = 0;

if (this.length < 0 || remPos >= this.length) {
return false;
}

if (remPos === 0) {
this.first = current.next;
this.first.prev = null;
this.length--;
return;
}

while (position < remPos) {
current = current.next;
++position;
}

current.prev.next = current.next;
current.next.prev = current.prev;
this.length--;
}

findFirst(name) {
let current = this.first;

while (current && current.name !== name) {
current = current.next;
}
return current;
}

findAll(name) {
let current = this.first;
const arr = [];

while (current !== null) {
if (current.name === name) {
arr.push(current);
}
current = current.next;
}
return arr;
}

find(name, callback) {
const arr = this.findAll(name);
let i;
for (i = 0; i < arr.length; ++i) {
callback(arr[i]);
}
}
}






const list1 = new LinkedList();
list1.push('first', 1);
list1.push('second', 2);
list1.push('third', 3);
list1.push('second', 5);

// console.dir(list1.findFirst('first'));
// console.dir(list1.findAll('second'));
// list1.find('second', n => console.log(n));
// //console.dir(list1.searchPos(-2));
console.dir(list1.itemAtPos(1));

// console.dir(list1);
// list1.remove(0);
// console.dir(list1);