[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 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.
[TS] Implement a singly linked list in TypeScript的更多相关文章
- [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 ...
- LeetCode 206 Reverse a singly linked list.
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- [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 ...
- [cc150] check palindrome of a singly linked list
Problem: Implement a function to check if a singly linked list is a palindrome. 思路: 最简单的方法是 Reverse ...
- Singly Linked List
Singly Linked List Singly linked list storage structure:typedef struct Node{ ElemType data; struct N ...
- Reverse a singly linked list
Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val ...
- 单链表反转(Singly Linked Lists in Java)
单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法 package dsa.linkedlist; public class Node<E> ...
- [轉]Reverse a singly linked list
Reverse a singly linked list http://angelonotes.blogspot.tw/2011/08/reverse-singly-linked-list.html ...
- Singly linked list algorithm implemented by Java
Jeff Lee blog: http://www.cnblogs.com/Alandre/ (泥沙砖瓦浆木匠),retain the url when reproduced ! Thanks ...
随机推荐
- centos7 ssh免口令认证登录
摘要:centos7, xshell, 公钥, ssh ssh登录方式有口令认证登录和密钥认证登录 接下来本次介绍是ssh密钥登录方式 (1)产生公钥 (2)将公钥放置到centos7的(/root ...
- 异步Ajax
Ajax异步刷新应用在Web开发中经常用到,在过去WebForm中通常是使用JQuery和一般处理程序或者aspx页面来实现: 在MVC中,虽然依然可以使用一般处理程序,但是一般还是通过在Contro ...
- Sqoop Export原理和详细流程讲解
Sqoop Export原理 Sqoop Export详细流程讲解
- Announcing Zuul: Edge Service in the Cloud--转
原文地址:http://techblog.netflix.com/2013/06/announcing-zuul-edge-service-in-cloud.html The Netflix st ...
- oracle 高水位线问题
--查询高水位线 50295 0 28185 0select blocks, empty_blocks from dba_tables where table_name='TODAYOTHERCONS ...
- ipad无法连接到app store怎么办
之前入手的air2提示无法连接到app store:你需要首先更新系统到最新的ios版本,去通用设置里面,有个update software, 点击即可,然后才能用apple id 联入,否选择提示连 ...
- RAID信息存放位置!
今天偶然的机会,客户打电话说有一台DELL T110的服务器换了主板电池RAID信息没了进不去系统了,问我怎么处理,T110的RAID是主板集成的S100的RAID卡(算是软RAID,通过BIOS配置 ...
- JS 引擎基础之 Shapes and Inline Caches
阅读下面这篇文章,需要20分钟: 一起了解下 JS 引擎是如何运作的吧! JS 的运作机制可以分为 AST 分析.引擎执行两个步骤: JS 源码通过 parser(分析器)转化为 AST(抽象语法树) ...
- tensorflow学习之路-----卷积神经网络个人总结
卷积神经网络大总结(个人理解) 神经网络 1.概念:从功能他们模仿真实数据 2.结构:输入层.隐藏层.输出层.其中隐藏层要有的参数:权重.偏置.激励函数.过拟合 3.功能:能通过模仿,从而学到事件 其 ...
- Scrapy发送POST请求
一.发送post请求需要将start_urls注释,然后重写start_requests方法二.使用yield scrapy.FormRequest(url=post_url, formdata=fo ...