Skip to content
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

single and doubly lists #24

Open
wants to merge 1 commit 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
64 changes: 64 additions & 0 deletions JavaScript/7-single.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

Copy link
Member

Choose a reason for hiding this comment

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

'use strict';

const Node = (value, next = null) => ({
Copy link
Member

Choose a reason for hiding this comment

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

It is not a class or prototype constructor so I'd like to use lowerCamelCase here.

value,
next,
});

const LinkedList = () => {
Copy link
Member

Choose a reason for hiding this comment

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

lowerCamelCase

const list = {
head: null,

append(value) {
if (!list.head) {
list.head = Node(value)
Copy link
Member

Choose a reason for hiding this comment

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

semicolon

} else {
let node = list.head;
while (node.next) {
node = node.next;
}
node.next = Node(value);
}
},

remove(value) {
let prev = null;
let node = list.head;

while (node) {
if (node.value === value) {
if (node === list.head) {
list.head = node.next;
} else {
prev.next = node.next;
}
}
prev = node;
node = node.next;
}
},

[Symbol.iterator]: function* () {
Copy link
Member

Choose a reason for hiding this comment

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

Use shorter syntax for generator as a class/object method:

{
  *[Symbol.iterator]() {
    // code here
  }
}

let node = list.head;
while (node) {
yield node.value;
node = node.next;
}
}
};
return list;
};

// Usage

const list = LinkedList();
Copy link
Member

Choose a reason for hiding this comment

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

lowerCamelCase


list.append(1);
list.append(2);
list.append(3);
list.append(4);
list.remove(3);

for (item of list) {
Copy link
Member

Choose a reason for hiding this comment

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

for (const item of list) {

console.log(item);
}

66 changes: 66 additions & 0 deletions JavaScript/8-doubly.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

Copy link
Member

Choose a reason for hiding this comment

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

'use strict';

const Node = (value, prev = null, next = null) => ({
Copy link
Member

Choose a reason for hiding this comment

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

lowerCamelCase

value,
prev,
next,
});

const DoublyLinkedList = () => {
Copy link
Member

Choose a reason for hiding this comment

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

lowerCamelCase

const list = {
head: null,

append(value) {
if (!list.head) {
list.head = Node(value)
Copy link
Member

Choose a reason for hiding this comment

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

semicolon

} else {
let node = list.head;
while (node.next) {
node = node.next;
}
node.next = Node(value, node);
}
},

remove(value) {
let node = list.head;

while (node) {
if (node.value === value) {
if (node === list.head) {
list.head = node.next;
} else {
node.prev.next = node.next;
if (node.next) {
node.next.prev = node.prev;
}
}
}
node = node.next;
}
},

[Symbol.iterator]: function* () {
Copy link
Member

Choose a reason for hiding this comment

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

Use shorter syntax for generators.

let node = list.head;
while (node) {
yield node.value;
node = node.next;
}
}
};
return list;
};

// Usage

const list = DoublyLinkedList();
Copy link
Member

Choose a reason for hiding this comment

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

lowerCamelCase


list.append(1);
list.append(2);
list.append(3);
list.append(4);
list.remove(3);

for (item of list) {
Copy link
Member

Choose a reason for hiding this comment

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

for (const item of list) {

console.log(item);
}