In a singly linked list each node in the list stores the contents of the node and a reference (or pointer in some languages) to the next node 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 linked list data structure and how to use its strengths to implement an O(1) FIFO queue.

/**
* Linked list node
*/
export interface LinkedListNode<T> {
value: T
next?: LinkedListNode<T>
} /**
* Linked list for items of type T
*/
export class LinkedList<T> {
public head?: LinkedListNode<T> = undefined;
public tail?: LinkedListNode<T> = undefined; /**
* Adds an item in O(1)
**/
add(value: T) {
const node = {
value,
next: undefined
}
if (!this.head) {
this.head = node;
}
if (this.tail) {
this.tail.next = node;
}
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;
}
return value;
}
} /**
* Returns an iterator over the values
*/
*values() {
let current = this.head;
while (current) {
yield current.value;
current = current.next;
}
}
}
import { LinkedList } from './linkedList';

test('basic', () => {
const list = new LinkedList<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);
expect(Array.from(list.values())).toMatchObject([5]);
});

We can also see the beautiy of Generator with Array.from partten.

Array.from(list.values())

It saves lots of code to keep maintaing the indexing of the array.

More

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

  1. [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 refer ...

  2. LeetCode 206 Reverse a singly linked list.

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  3. [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点

    Implement an algorithm to delete a node in the middle of a singly linked list, given only access to ...

  4. [cc150] check palindrome of a singly linked list

    Problem: Implement a function to check if a singly linked list is a palindrome. 思路: 最简单的方法是 Reverse ...

  5. Singly Linked List

    Singly Linked List Singly linked list storage structure:typedef struct Node{ ElemType data; struct N ...

  6. Reverse a singly linked list

    Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val ...

  7. 单链表反转(Singly Linked Lists in Java)

    单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法   package dsa.linkedlist; public class Node<E> ...

  8. [轉]Reverse a singly linked list

    Reverse a singly linked list  http://angelonotes.blogspot.tw/2011/08/reverse-singly-linked-list.html ...

  9. Singly linked list algorithm implemented by Java

    Jeff Lee blog:   http://www.cnblogs.com/Alandre/  (泥沙砖瓦浆木匠),retain the url when reproduced ! Thanks ...

随机推荐

  1. jasperreport 追加新报表(2)

    用ireport做好模版后,如果要新加一个打印页,如果是新手,直接修改模版应该是理想情况, 可是什么数据源 feild,parameter,var,subreport ,还有路径, 真的可以让一个人疯 ...

  2. 虚拟摄像头vivi的测试(二)

    (前一部分的基础操作来源于作者:LingXiaokai 的 Ubuntu 9.10 下如何使用笔记本摄像头以及虚拟摄像头vivi的测试) 自己仅对实际操作中需要注意的点进行阐述 一.先在Ubuntu ...

  3. 洛谷 P3585 [POI2015]PIE

    P3585 [POI2015]PIE 题目描述 一张n*m的方格纸,有些格子需要印成黑色,剩下的格子需要保留白色.你有一个a*b的印章,有些格子是凸起(会沾上墨水)的.你需要判断能否用这个印章印出纸上 ...

  4. OR1200处理器中Wishbone总线接口模块WB_BIU介绍

    下面内容摘自<步步惊芯--软核处理器内部设计分析>一书 WB_BIU模块是OR1200处理器与外部Wishbone总线连接的接口模块.15.1节给出了WB_BIU模块的对外连接关系,并指出 ...

  5. hdu(2859)——Phalanx(dp)

    题意: 如今有一个n*n的矩阵,然后每一个格子中都有一个字母(大写或小写组成).然后询问你如今最大的对称子矩阵的边长是多少.注意这里的对角线是从左下角到右上角上去的. 思路: 这道题我自己写出了dp的 ...

  6. vue ---- 组件传值之间使用 v-model

    父子组件通信,都是单项的,很多时候需要双向通信.方法如下: 1.父组件使用:msg.sync="aa"  子组件使用$emit('update:msg', 'msg改变后的值xxx ...

  7. SKU=Stock Keeping Unit(库存量单位)。即库存进出计量的单位,可以是以件,盒,托盘等为单位

    SKU=Stock Keeping Unit(库存量单位).即库存进出计量的单位,可以是以件,盒,托盘等为单位.SKU这是对于大型连锁超市DC(配送中心)物流管理的一个必要的方法.现在已经被引申为产品 ...

  8. vue 中表单 [v-bind:true-value="a" v-bind:false-value="b"] 的用法

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

  9. OpenCV —— 图像处理

    使用图像结构中所定义的高层处理方法(图形和视觉范畴)来完成特定任务 平滑处理   cvSmooth 处理后图像与输入图像的大小相同(不用考虑边缘) 中值滤波  CV_MEDIAN 不支持 in pla ...

  10. NOI2017整数

    NOI2017 整数 题意: ​ 让你实现两个操作: 1 \(a\) \(b\):将\(x\)加上整数\(a \cdot 2 ^ b\),其中 \(a\)为一个整数,\(b\)为一个非负整数 2 \( ...