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 1 commit
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
42 changes: 20 additions & 22 deletions JavaScript/3-classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,41 +44,39 @@ class LinkedList {

}

searchPos(searchPosition) {
itemAtPos(pos) {

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

if (this.length === 0 ||
searchPosition < 0 ||
searchPosition >= this.length) {
throw new Error('Position out of bounds.');
if (!(this.length) || pos < 0 || pos >= this.length) {
return false;
}

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

return current;
}

remove(removePosition) {
remove(remPos) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше просто pos, не нужно дублировать имя функции в ее аргументах

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

if (this.length < 0 || removePosition >= this.length) {
throw new Error('Position out of bounds.');
if (this.length < 0 || remPos >= this.length) {
return false;
}

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

while (position < removePosition) {
while (position < remPos) {
current = current.next;
++position;
}
Expand All @@ -91,7 +89,7 @@ class LinkedList {
findFirst(name) {
let current = this.first;

while (current !== null && current.name !== name) {
while (current && current.name !== name) {
current = current.next;
}
return current;
Expand Down Expand Up @@ -130,12 +128,12 @@ 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.searchPos(1));
// 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);
// console.dir(list1);
// list1.remove(0);
// console.dir(list1);