Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Feat/linkedlist #92

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

Merged
amejiarosario merged 3 commits into master from feat/linkedlist
Oct 23, 2020
Merged
Show file tree
Hide file tree
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
Next Next commit
chore(docs): improve comments here and there
  • Loading branch information
amejiarosario committed Oct 23, 2020
commit 7659ac626e86a1685403dedc546d1a64a891d24e
2 changes: 1 addition & 1 deletion book/content/part02/hash-map.asc
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A Map is a data structure where a `key` is mapped to a `value`. It's used for a

NOTE: Map has many terms depending on the programming language. Here are some other names: Hash Map, Hash Table, Associative Array, Unordered Map, Dictionary.

==== Map Application
==== Map Applications

Maps are one of the most popular data structures because of their fast lookup time.

Expand Down
5 changes: 3 additions & 2 deletions book/content/part02/linked-list.asc
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ ifndef::imagesdir[]
:codedir: ../../../src
endif::[]

[[linked-list]]
=== Linked List
(((Linked List)))
(((List)))
(((Data Structures, Linear, Linked List)))
[[linked-list]]
=== Linked List

A list (or Linked List) is a linear data structure where each node is "linked" to the next.

.Linked Lists can be:
Expand Down
38 changes: 18 additions & 20 deletions src/data-structures/linked-lists/linked-list.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ class LinkedList {
}
// end::constructor[]

/**
* Alias for size
*/
get length() {
return this.size;
}

// tag::addFirst[]
/**
* Adds element to the begining of the list. Similar to Array.unshift
Expand Down Expand Up @@ -92,25 +85,22 @@ class LinkedList {
}
// Adding element in the middle
const current = this.get(position);
if (current) {
const newNode = new Node(value); // <3>
newNode.previous = current.previous; // <4>
newNode.next = current; // <5>

current.previous.next = newNode; // <6>
current.previous = newNode; // <7>
this.size += 1;
return newNode;
}
if (!current) return undefined; // out of bound index

return undefined; // out of bound index
const newNode = new Node(value); // <3>
newNode.previous = current.previous; // <4>
newNode.next = current; // <5>
current.previous.next = newNode; // <6>
current.previous = newNode; // <7>
this.size += 1;
return newNode;
}
// end::addMiddle[]

// tag::searchByValue[]
/**
* Search by value. It finds first occurrence of
* the element matching the value.
* the position of element matching the value.
* Runtime: O(n)
* @example: assuming a linked list with: a -> b -> c
* linkedList.indexOf('b') // ↪️ 1
Expand All @@ -136,7 +126,8 @@ class LinkedList {
* linkedList.get(1) // ↪️ 'b'
* linkedList.get(40) // ↪️ undefined
* @param {Number} index position of the element
* @returns {Node} element at the specified position in this list.
* @returns {Node|undefined} element at the specified position in
* this list or undefined if was not found.
*/
get(index = 0) {
return this.find((current, position) => {
Expand Down Expand Up @@ -302,6 +293,13 @@ class LinkedList {
const parts = [...this]; // see [Symbol.iterator]()
return parts.map((n) => util.inspect(n.node.value)).join(' -> ');
}

/**
* Alias for size
*/
get length() {
return this.size;
}
}

// Aliases
Expand Down
2 changes: 1 addition & 1 deletion src/data-structures/linked-lists/node.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Node {
constructor(value) {
this.value = value;
this.next = null;
this.previous = null;
this.previous = null; // for doubly linked list
}
}
// end::snippet[]
Expand Down

AltStyle によって変換されたページ (->オリジナル) /