[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 ...
随机推荐
- 英语音乐---一、Scarborough Fair
英语音乐---一.Scarborough Fair 一.总结 一句话总结:斯卡布罗集市 <斯卡布罗集市>诉说了一个缠绵凄美的爱情故事:一个参军的男青年远离自己相爱的姑娘在战争中不幸遇难,但 ...
- Centos7 网络出错(failed to start LSB: Bring up/down networking )
这是我更换了VM虚拟机内存,重启后无法连接网络. 然后这是因为NetworkManager.service这个程序造成 解决方法: systemctl disable NetworkManager.s ...
- BZOJ 1012 单调队列+二分
思路: 维护一个单减的序列 序号是单增的 每回二分查找第一个比询问的大的值 我手懒 用得lower_bound //By SiriusRen #include <cstdio> #incl ...
- Ubuntu16.04添加HP Laserjet Pro M128fn打印机和驱动
一.全部设置->打印机->添加新打印机 添加打印机 二.选择自动搜索到的网络打印机HP Laserjet Pro M128fn,点击添加. 三.添加打印机完成,打印测试页进行测试. 四. ...
- 打印机共享 : 客户端 连接服务器打印机时提示"无法连接到打印机“
1.就是重启一下服务器端的Print Spooler服务就行了,这么简单! 2.修改打印机的共享名 操作无法完成(错误0x00000709).再次检查打印机名称,并确保打印机已连接到网络.(xp系统本 ...
- OpenCASCADE Extended Data Exchange - XDE
OpenCASCADE Extended Data Exchange - XDE eryar@163.com Abstract. OpenCASCADE Data Exchange allows de ...
- 配置远程访问阿里云服务器的Redis
1.默认情况Redis不是在后台运行,我们需要修改把redis放在后台运行:daemonize yes 2.Redis安全策略默认本机访问,所以远程访问的话需要将 bind 127.0.0.1加#注释 ...
- 怎样通过MSG_WAITALL设置阻塞时间,IO模式精细讲解: MSG_DONTWAIT 、 MSG_WAITALL
首先给出MSDN上一段设置阻塞超时的代码:(网址为http://social.msdn.microsoft.com/Forums/zh-SG/visualcpluszhchs/thread/3d9da ...
- 最大子矩阵和 51Nod 1051 模板题
一个M*N的矩阵,找到此矩阵的一个子矩阵,并且这个子矩阵的元素的和是最大的,输出这个最大的值. 例如:3*3的矩阵: -1 3 -1 2 -1 3 -3 1 2 和最大的子矩阵是: 3 - ...
- 2017国家集训队作业[arc076d/f][Exhausted?]
2017国家集训队作业[arc076d/f][Exhausted?] 题意: 有\(N\)个人,\(M\)把椅子,给出\(...L_i.R_i\)表示第\(i\)个人可以选择编号为\(1\sim ...