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.

  1. /**
  2. * Linked list node
  3. */
  4. export interface LinkedListNode<T> {
  5. value: T
  6. next?: LinkedListNode<T>
  7. }
  8.  
  9. /**
  10. * Linked list for items of type T
  11. */
  12. export class LinkedList<T> {
  13. public head?: LinkedListNode<T> = undefined;
  14. public tail?: LinkedListNode<T> = undefined;
  15.  
  16. /**
  17. * Adds an item in O(1)
  18. **/
  19. add(value: T) {
  20. const node = {
  21. value,
  22. next: undefined
  23. }
  24. if (!this.head) {
  25. this.head = node;
  26. }
  27. if (this.tail) {
  28. this.tail.next = node;
  29. }
  30. this.tail = node;
  31. }
  32.  
  33. /**
  34. * FIFO removal in O(1)
  35. */
  36. dequeue(): T | undefined {
  37. if (this.head) {
  38. const value = this.head.value;
  39. this.head = this.head.next;
  40. if (!this.head) {
  41. this.tail = undefined;
  42. }
  43. return value;
  44. }
  45. }
  46.  
  47. /**
  48. * Returns an iterator over the values
  49. */
  50. *values() {
  51. let current = this.head;
  52. while (current) {
  53. yield current.value;
  54. current = current.next;
  55. }
  56. }
  57. }
  1. import { LinkedList } from './linkedList';
  2.  
  3. test('basic', () => {
  4. const list = new LinkedList<number>();
  5. list.add(1);
  6. list.add(10);
  7. list.add(5);
  8. expect(Array.from(list.values())).toMatchObject([1, 10, 5]);
  9. expect(list.dequeue()).toBe(1);
  10. expect(Array.from(list.values())).toMatchObject([10, 5]);
  11. expect(list.dequeue()).toBe(10);
  12. expect(list.dequeue()).toBe(5);
  13. expect(list.dequeue()).toBe(undefined);
  14. expect(Array.from(list.values())).toMatchObject([]);
  15. list.add(5);
  16. expect(Array.from(list.values())).toMatchObject([5]);
  17. });

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

  1. 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. 英语音乐---一、Scarborough Fair

    英语音乐---一.Scarborough Fair 一.总结 一句话总结:斯卡布罗集市 <斯卡布罗集市>诉说了一个缠绵凄美的爱情故事:一个参军的男青年远离自己相爱的姑娘在战争中不幸遇难,但 ...

  2. Centos7 网络出错(failed to start LSB: Bring up/down networking )

    这是我更换了VM虚拟机内存,重启后无法连接网络. 然后这是因为NetworkManager.service这个程序造成 解决方法: systemctl disable NetworkManager.s ...

  3. BZOJ 1012 单调队列+二分

    思路: 维护一个单减的序列 序号是单增的 每回二分查找第一个比询问的大的值 我手懒 用得lower_bound //By SiriusRen #include <cstdio> #incl ...

  4. Ubuntu16.04添加HP Laserjet Pro M128fn打印机和驱动

    一.全部设置->打印机->添加新打印机  添加打印机 二.选择自动搜索到的网络打印机HP Laserjet Pro M128fn,点击添加. 三.添加打印机完成,打印测试页进行测试. 四. ...

  5. 打印机共享 : 客户端 连接服务器打印机时提示"无法连接到打印机“

    1.就是重启一下服务器端的Print Spooler服务就行了,这么简单! 2.修改打印机的共享名 操作无法完成(错误0x00000709).再次检查打印机名称,并确保打印机已连接到网络.(xp系统本 ...

  6. OpenCASCADE Extended Data Exchange - XDE

    OpenCASCADE Extended Data Exchange - XDE eryar@163.com Abstract. OpenCASCADE Data Exchange allows de ...

  7. 配置远程访问阿里云服务器的Redis

    1.默认情况Redis不是在后台运行,我们需要修改把redis放在后台运行:daemonize yes 2.Redis安全策略默认本机访问,所以远程访问的话需要将 bind 127.0.0.1加#注释 ...

  8. 怎样通过MSG_WAITALL设置阻塞时间,IO模式精细讲解: MSG_DONTWAIT 、 MSG_WAITALL

    首先给出MSDN上一段设置阻塞超时的代码:(网址为http://social.msdn.microsoft.com/Forums/zh-SG/visualcpluszhchs/thread/3d9da ...

  9. 最大子矩阵和 51Nod 1051 模板题

    一个M*N的矩阵,找到此矩阵的一个子矩阵,并且这个子矩阵的元素的和是最大的,输出这个最大的值. 例如:3*3的矩阵:   -1 3 -1 2 -1 3 -3 1 2   和最大的子矩阵是:   3 - ...

  10. 2017国家集训队作业[arc076d/f][Exhausted?]

    2017国家集训队作业[arc076d/f][Exhausted?] 题意: ​ 有\(N\)个人,\(M\)把椅子,给出\(...L_i.R_i\)表示第\(i\)个人可以选择编号为\(1\sim ...