A linked list is a collection of items where each item points to the next one in the list. Because of this structure, linked lists are very slow when searching for an item at a particular index. An array, by comparison, has quick gets when searching for an index, but a linked list must start at the beginning, often called the "head", and loop through each item's next property until we arrive at the item. This makes gets in a linked list an operation that takes O(n) time.

While gets might be slow in a linked list, it's other operations, like push and delete come with some great benefits we will see in the lesson.

/**
* Linked list
*
* API:
* push
* pop
* get
* delete
* isEmpty
* print
*/ function createNode(value) {
return {
value,
next: null
}
} function createLinkedList() {
return {
head: null,
tail: null,
length: 0,
push(value) {
/**Key takeaway:
* Assign new node to current tail's next value
* Then
* Reassign the tail to new node
*/
// Create Node
const node = createNode(value); // If this is the first one
if (this.head === null) {
this.head = node
this.tail = node
this.length++;
return node;
} // if there already has nodes
this.tail.next = node;
this.tail = node;
this.length++;
return node;
},
pop() {
const node = this.tail;
// if this is no node
if (!this.head) {
return null;
} // if there is one node
if (this.head === this.tail) {
this.head = null;
this.tail = null;
return node;
} let current = this.head;
let penultimate = null; while (current) {
const {next} = current;
if (next && next == this.tail) {
penultimate = current;
break;
}
current = current.next;
}
penultimate.next = null;
this.tail = penultimate;
this.length--;
return node;
},
get(index = 0) {
// no node in the list, return null
if (!this.head) {
return null;
} // if the index < 0 or > length - 1, out of range
if (index < 0 || index > this.length - 1) {
return null;
} // if index = 0, then return the first
if (index === 0) {
return this.head;
} let current = this.head;
let i = 0;
while (i < index) {
i++;
current = current.next;
} return current;
},
delete(index = 0) {
/**
* Key takewawy:
* If we delete tail, we need to reassign the tail
*/
// no node in the list, return null
if (!this.head) {
return null;
} // if the index < 0 or > length - 1, out of range
if (index < 0 || index > this.length - 1) {
return null;
} // if index = 0, then return the first
if (index === 0) {
const node = this.head;
this.head = node.next;
this.length--;
return node;
} let i = 0;
let current = this.head;
let previous = null; while (i < index) {
i++;
previous = current;
current = current.next;
} const deleted = current;
previous.next = deleted.next; // If we delete the tail, we need to reassign tail
if (previous.next === null) {
this.tail = previous;
} this.length--;
return deleted;
},
isEmpty() {
return this.length === 0;
},
print() {
/**Key takeway:
* remember to assign next node to current
* Move the while loop
* */
let nodes = []; if (!this.head) {
return 'Empty list';
} let current = this.head;
while (current) {
nodes.push(current.value);
current = current.next;
} return nodes.join(' => ');
}
};
} module.exports = {createLinkedList}

test:

const {createLinkedList} = require('../src/linked-list');

describe('linked list', () => {
test('push: should add node into array', () => {
const l = createLinkedList();
// linked list should be empty
expect(l.isEmpty()).toBe(true);
// push a new node
l.push('a');
expect(l.isEmpty()).toBe(false);
expect(l.length).toEqual(1);
expect(l.print()).toEqual('a');
// push a second node
l.push('b');
expect(l.length).toEqual(2);
expect(l.print()).toEqual('a => b');
}); test('pop: should remove the last node from the list', () => {
const l = createLinkedList();
l.push('a');
l.push('b');
l.push('c');
expect(l.length).toEqual(3);
const p = l.pop();
expect(p.value).toEqual('c');
expect(l.length).toEqual(2);
}); test('get: should return the node for the given index', () => {
const l = createLinkedList();
// empty list, return null
expect(l.get(0)).toBeNull();
l.push('a');
l.push('b');
l.push('c');
expect(l.length).toEqual(3);
// out of index, retur null
expect(l.get(-1)).toBeNull();
expect(l.get(4)).toBeNull(); // return the head
expect(l.get(0).value).toEqual('a'); // index in range not head
expect(l.get(2).value).toEqual('c');
}); test('delete: should delete the node from the given index', () => {
const l = createLinkedList();
// empty list, return null
expect(l.delete(0)).toBeNull();
l.push('a');
l.push('b');
l.push('c');
expect(l.length).toEqual(3);
// out of index, retur null
expect(l.delete(-1)).toBeNull();
expect(l.delete(4)).toBeNull();
// return the head
expect(l.delete(0).value).toEqual('a');
expect(l.length).toEqual(2);
// delete the tail, reassign the tail
expect(l.delete(1).value).toEqual('c');
expect(l.tail.value).toEqual('b');
});
});

[Algorithm] Linked List Data Structure in JavaScript的更多相关文章

  1. [Algorithms] Tree Data Structure in JavaScript

    In a tree, nodes have a single parent node and may have many children nodes. They never have more th ...

  2. [Algorithom] Stack Data Structure in JavaScript

    A stack is a collection of items that obeys the principle of "last in, first out". Like a ...

  3. [Algorithm] JavaScript Graph Data Structure

    A graph is a data structure comprised of a set of nodes, also known as vertices, and a set of edges. ...

  4. [Algorithm] Heap data structure and heap sort algorithm

    Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...

  5. [Algorithm] Trie data structure

    For example we have an array of words: [car, done, try, cat, trie, do] What is the best data structu ...

  6. HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...

  7. hdu-5929 Basic Data Structure(双端队列+模拟)

    题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  8. CDOJ 483 Data Structure Problem DFS

    Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/proble ...

  9. HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

随机推荐

  1. [每日App一]QQ主题要逆天!轻轻松松月入30万!

    听从吾师秦刚(微信或QQ:1111884)酋长的建议,谋哥(微信viyi88)我开始新的征程,每日更新一篇干货文章(要坚持啊!否则被酋长鄙视了). 好了,废话不多说,今天我给大家揭秘一个你从来想也木有 ...

  2. ES6 异步编程之一:Generator

    Generator 生成器是es6原生提供的异步编程方案,其语法行为和传统函数完全不同,阮大的<ECMAScript 6 入门>一书中对生成器有比较详尽的介绍,还有一些其他的文章可以参考, ...

  3. Text Region Mask

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/52886351 Python code ...

  4. 九度oj 题目1363:欢乐斗地主

    题目描述: 如果大家玩过欢乐斗地主这个游戏,就一定知道有一个具有“提示”功能的按钮.如果你不知道你现在手里的牌有没有比上家大的牌,并且你也懒得去一张一张地看你手中的牌.这时候你就可以点“提示”按钮,系 ...

  5. POJ 2033 Alphacode

    Alphacode Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11666   Accepted: 3564 Descri ...

  6. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

  7. 【bzoj1040】[ZJOI2008]骑士 并查集+基环树dp

    题目描述 Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火绵延五百里,在 ...

  8. Linux 终端操作之「I/O Redirection」

    I/O 重定向是在终端运行程序时很常用的技巧,但是我对它所知甚少.今天我在 DigitalOcean 上发现了一篇很好的 tutorial.这篇随笔记录一下我的心得体会和发现的一个问题. I/O re ...

  9. BZOJ-2829 信用卡凸包

    凸包题. 我们先把所有信用卡的四个定点的坐标求出来,然后计算凸包长度,最后加上一个圆的周长就行. #include <cstdlib> #include <cstdio> #i ...

  10. BZOJ3697 采药人的路径 【点分治】

    题目 采药人的药田是一个树状结构,每条路径上都种植着同种药材. 采药人以自己对药材独到的见解,对每种药材进行了分类.大致分为两类,一种是阴性的,一种是阳性的. 采药人每天都要进行采药活动.他选择的路径 ...