[TS] Implement a doubly linked list in TypeScript
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的更多相关文章
- [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 ...
- Doubly Linked List
Doubly Linked List Your task is to implement a double linked list. Write a program which performs th ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- Django transaction 误用之后遇到的一个问题与解决方法
今天在调试项目开发好的一个模块的时候,发现了一个很诡异的现象,最后追踪发现是因为在项目中事务处理有误所致.这个问题坑了我好一会,所以记录一下,以免再踩坑.下面开始详述. 我们都知道 Django 框架 ...
- blkid---对系统块设备信息查询
在Linux下可以使用blkid命令对查询设备上所采用文件系统类型进行查询.blkid主要用来对系统的块设备(包括交换分区)所使用的文件系统类型.LABEL.UUID等信息进行查询.要使用这个命令必须 ...
- pip版本及升级 pip安装指定模板
昨天在微信聊天,一妹子9点的时候告诉我她要看书了,让明天聊,瞬间自己心中那颗学习的种子燃烧起来,思来想去还是继续学习自己之前未学好的python吧,因为之前有了点点的python基础,所以本次打算从p ...
- IOCP模型总结(总结回想)
IOCP旧代码重提.近期一直在玩其它方面的东东.时不时回想一下,收益多多. IOCP(I/O Completion Port,I/O完毕port)是性能最好的一种I/O模型.它是应用程序使用线程池处理 ...
- 从零開始制作H5应用(2)——V2.0版,多页单张图片滑动,透明过渡及交互指示
上一次.我们制作了我们第一个H5场景应用的V1.0版,这次我们趁热打铁.在上一版的基础上对层序进行改动和扩展. 任务 1.页面数量由3张增加至9张: 2.每张页面中放入一张全屏自适应的图片. 3.修复 ...
- opencms9.0安装
今天安装opencms 9.0遇到了一些问题,因为是初次安装和使用,导致耽误了非常多时间.所以在此记录一下以备以后借鉴. 首先附上安装步骤链接: http://www.51testing.com/ht ...
- Python Web框架Tornado的异步处理代码演示样例
1. What is Tornado Tornado是一个轻量级但高性能的Python web框架,与还有一个流行的Python web框架Django相比.tornado不提供操作数据库的ORM接口 ...
- Java图像渐变
图像渐变我们大体想一下思路无非是这样:将图像所有的像素点的RBG,每个点就减去相同的量,而且这个量是个渐变的量.是的,就是这样,我们的程序也是这个思路,不过就是没有单纯的“想”这么简单了.我这里只编写 ...
- html元素的分类有哪些?
今天零度给大家讲一下基本的html元素分类: HTML元素的分类其实主要有两种元素构成——块级元素和内联元素. html元素的分类有块级元素和行内元素 一.块级元素(block)的特点: 1.总是在新 ...
- 调用C#版gdal库的一个注意事项
作者:朱金灿 来源:http://blog.csdn.net/clever101 在编译完C#版gdal库(x86平台)下,写了一个C#的控制台测试程序,出现下面的错误: 解决办法是将工程的目标平台设 ...