Skip to content

double list with classes #9

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
2 changes: 1 addition & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rules:
MemberExpression: 1
linebreak-style:
- error
- unix
- windows
Copy link
Member

Choose a reason for hiding this comment

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

quotes:
- error
- single
Expand Down
88 changes: 58 additions & 30 deletions JavaScript/2-doubly.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,66 @@
'use strict';

function LinkedList() {
this.first = null;
this.last = null;
this.length = 0;
class Node {
constructor(list, data) {
this.list = list;
this.data = data;
this.prev = null;
this.next = null;
}
}

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

LinkedList.prototype.pop = function() {
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;
class LinkedList {
constructor() {
this.first = null;
this.last = null;
this.length = 0;
}
push(data) {
const node = new Node(this, 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;
}
}
findFirst(name) {
if (this.length > 0) {
let node = this.first;
if (node.data.name === name)
return node;
while (node !== this.last) {
node = node.next;
if (node.data.name === name)
return node;
}
}
}
findAll(name) {
const nodeArray = [];
if (this.length > 0) {
let node = this.first;
if (node.data.name === name)
Copy link
Member

Choose a reason for hiding this comment

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

Добавь {}

nodeArray.push(node);
while (node !== this.last) {
node = node.next;
if (node.data.name === name)
Copy link
Member

Choose a reason for hiding this comment

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

Только однострочные без скобок

nodeArray.push(node);
}
}
return nodeArray;
}
};

function Node(list, data) {
this.list = list;
this.data = data;
this.prev = null;
this.next = null;
}

const list = new LinkedList();
Expand Down