In a doubly linked list each node in the list stores the contents of the node and a pointer or reference to the next and the previous nodes in the list. It is one of the simplest way to store a collection of items.

In this lesson we cover how to create a doubly linked list data structure and how to use its strengths to implement an O(1) FIFO queue + O(1) LIFO stack. We also demonstrate why one would use it over a singly linked list. We also cover how to approach authoring such data structures.

The reason for use double linked list is that we can do LIFO opreation in O(1), in Single linked List, it requires O(n), because you need to go thought from the head, in order to know what will be the next tail item for new linked list.

/**
* Linked list node
*/
export interface DoublyLinkedListNode<T> {
value: T
next?: DoublyLinkedListNode<T>
prev?: DoublyLinkedListNode<T>
} /**
* Linked list for items of type T
*/
export class DoublyLinkedList<T> {
public head?: DoublyLinkedListNode<T> = undefined;
public tail?: DoublyLinkedListNode<T> = undefined; /**
* Adds an item in O(1)
**/
add(value: T) {
const node: DoublyLinkedListNode<T> = {
value,
next: undefined,
prev: undefined,
}
if (!this.head) {
this.head = node;
}
if (this.tail) {
this.tail.next = node;
node.prev = this.tail;
}
this.tail = node;
} /**
* FIFO removal in O(1)
*/
dequeue(): T | undefined {
if (this.head) {
const value = this.head.value;
this.head = this.head.next;
if (!this.head) {
this.tail = undefined;
}
else {
this.head.prev = undefined;
}
return value;
}
} /**
* LIFO removal in O(1)
*/
pop(): T | undefined {
if (this.tail) {
const value = this.tail.value;
this.tail = this.tail.prev;
if (!this.tail) {
this.head = undefined;
}
else {
this.tail.next = undefined;
}
return value;
}
} /**
* Returns an iterator over the values
*/
*values() {
let current = this.head;
while (current) {
yield current.value;
current = current.next;
}
}
}
import { DoublyLinkedList } from './doublyLinkedList';

test('basic', () => {
const list = new DoublyLinkedList<number>();
list.add(1);
list.add(10);
list.add(5);
expect(Array.from(list.values())).toMatchObject([1, 10, 5]);
expect(list.dequeue()).toBe(1);
expect(Array.from(list.values())).toMatchObject([10, 5]);
expect(list.dequeue()).toBe(10);
expect(list.dequeue()).toBe(5);
expect(list.dequeue()).toBe(undefined);
expect(Array.from(list.values())).toMatchObject([]);
list.add(5);
list.add(10);
list.add(1);
expect(Array.from(list.values())).toMatchObject([5, 10, 1]);
expect(list.pop()).toBe(1);
expect(list.dequeue()).toBe(5);
expect(list.pop()).toBe(10);
expect(list.pop()).toBe(undefined);
expect(list.dequeue()).toBe(undefined);
expect(Array.from(list.values())).toMatchObject([]);
});

[TS] Implement a doubly linked list in TypeScript的更多相关文章

  1. [TS] Implement a singly linked list in TypeScript

    In a singly linked list each node in the list stores the contents of the node and a reference (or po ...

  2. Doubly Linked List

    Doubly Linked List Your task is to implement a double linked list. Write a program which performs th ...

  3. Convert a given Binary Tree to Doubly Linked List

    The question and solution are from: http://www.geeksforgeeks.org/convert-given-binary-tree-doubly-li ...

  4. [geeksforgeeks] Convert a given Binary Tree to Doubly Linked List

    http://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/ Given a Bin ...

  5. [LeetCode] Flatten a Multilevel Doubly Linked List 压平一个多层的双向链表

    You are given a doubly linked list which in addition to the next and previous pointers, it could hav ...

  6. [LeetCode] Convert Binary Search Tree to Sorted Doubly Linked List 将二叉搜索树转为有序双向链表

    Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers ...

  7. Convert Binary Search Tree to Doubly Linked List

    Convert a binary search tree to doubly linked list with in-order traversal. Example Given a binary s ...

  8. 426. Convert Binary Search Tree to Sorted Doubly Linked List把bst变成双向链表

    [抄题]: Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right po ...

  9. [leetcode]426. Convert Binary Search Tree to Sorted Doubly Linked List二叉搜索树转有序双向链表

    Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers ...

随机推荐

  1. net.sf.json Maven依赖配置

    转自:https://blog.csdn.net/qq_36698956/article/details/80772984 今天搭框架开始实现前台的json了,于是逐个找适合的框架,发现要实现json ...

  2. IntelliJ IDEA springmvc demo

    construction pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...

  3. java引用被设置为null的疑惑

    a=null; public class C { protected A webDigester = new A(" first one "); public void test( ...

  4. 分享一段wap框架样式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. ZOJ 3435 Ideal Puzzle Bobble 莫比乌斯反演

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4119 依然是三维空间内求(1,1,1)~(a,b,c)能看到的整点数,平移一下 ...

  6. javaScript学习之正则表达式初探

    正则表达式    正则表达式,又称规则表达式.(英语:Regular Expression,在代码中常简写为regex.regexp或RE),计算机科学的一个概念.正则表达式通常被用来检索.替换那些符 ...

  7. javaScript [[scope]]学习笔记

    在javaScript 学习中,闭包肯定是一个让人头疼的问题,而闭包必定离不开[[scope]]作用域 scope存储了执行期上下文的集合 而了解scope以及scope链又是一个十分抽象的问题 我们 ...

  8. vue+ webpack中的animate.css实现的执行多个连续的动画

    1.安装 npm install animate.css 2.使用方法 入口文件App中进行引入 import animate from 'animate.css' 3.多个连续的动画 实现的效果:实 ...

  9. 彻底解决Linux索引节点(inode)占用率高的告警

    今天邮箱里发现有一封某服务器inode使用率发生告警的邮件 登录到服务器上df -i查看,发现/路径下91%,磁盘使用率却不高,猜测可能是某个目录下的小文件过多,进而造成inode占用率过高,但不清楚 ...

  10. 在resin配置參数实现JConsole远程监控JVM

    在Resin配置參数实现JConsole远程监控JVM 在Resin中配置中配置下列參数,就能够是实现了! <jvm-arg>-Dcom.sun.management.jmxremote& ...