[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 ...
随机推荐
- android事件学习
一.android处理事件有两种形式. 1.基于监听的事件处理,就是通过setXxxListenter()进行处理的. 2.基于回调的事件处理,就是View类内部的onTouchEvent(),一般是 ...
- css3 列表图片hover左右滚动效果
- yum配置中driver-class-name: com.mysql.jdbc.Driver报错
错误: 原因: 解决方法:把方框中的<scope>runtime</scope>删掉
- Python中import和from的一些事。。。
摘自python学习手册, 用于记录. 客户端可以执行import或from语句.如果模块还没有加载,这两个语句会去搜索.编译以及执行模块文件程序.主要差别在于,import会读取整个模块,所以必须进 ...
- qqwry - 纯真ip库的golang服务
qqwry 纯真 IP 库的一个服务.通过http提供一个ip地址归属地查询支持 软件介绍 我们大家做网站的时候,都会需要将用户的IP地址转换为归属地址功能,而之前的作法大都是从硬盘的数据文件中读取, ...
- HBase概念学习(八)开发一个类twitter系统之表设计
这边文章先将可能的需求分析一下,设计出HBase表,下一步再開始编写client代码. TwiBase系统 1.背景 为了加深HBase基本概念的学习,參考HBase实战这本书实际动手做了这个样例. ...
- amaze ui表格斑马纹效果
amaze ui表格斑马纹效果 需要注意的是样式的写法,都是 am-table- ,很好记的 如果是条纹就是striped,如果是hover状态就是hover 用法很简单,点对应class,不同的cl ...
- POJ 2227 FloodFill (priority_queue)
题意: 思路: 搞一个priority_queue 先把边界加进去 不断取最小的 向中间扩散 //By SiriusRen #include <queue> #include <cs ...
- OpenCV特征点检测——ORB特征
ORB算法 目录(?)[+] 什么是ORB 如何解决旋转不变性 如何解决对噪声敏感的问题 关于尺度不变性 关于计算速度 关于性能 Related posts 什么是ORB 七 4 Ye ...
- learn ES6
介绍 ES6,也叫ECMAScript2015(以下统称ES6),是ECMAScript标准的最新版本.这个标准在2015年6月份被正式批准.ES6是js语言很有意义的一次更新,也是2009年ES5被 ...