[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 ...
随机推荐
- Create an ASP.NET Core web app in Visual Studio Code
https://www.microsoft.com/net/core#windowscmd https://download.microsoft.com/download/B/9/F/B9F1AF57 ...
- excel导入数据的
.aspx 文件 <form id="form1" runat="server"> <div> <asp:FileUpload I ...
- BZOJ2115: [Wc2011] Xor(Dfs树,Xor线性无关组)
Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 ...
- CSDN博客给我带来的一些诱惑和选择机会(二):HR“邀请于我”,猎头“有求于我”
上次,2013年10月8日 ,分享了一篇颇具"正能量"的文章CSDN博客给我带来的一些诱惑和选择机会,获得了很好的正面效果. 10月份,又发生了很多有趣.有意义的事情. 其中,有一 ...
- CSUOJ 1549 Navigition Problem
1549: Navigition Problem Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 65 Solved: 12 Description N ...
- WPF通用框架ZFS《项目结构介绍01》_模块介绍
首页介绍: 下图为项目运行首页图片, 大的结构分为三块: 1.Header首部模块(存放通知组件[全局通知.消息管理 ].扩展模块[皮肤.系统设置.关于作者.退出系统]) 2.Left左侧菜单模块(存 ...
- python创建多层目录的方式
将 os.mkdir 改成 os.makedirs(opDir) 哈.
- 多路I/O转接之select模型
I/O复用使得程序可以同一时候监听多个文件描写叙述符.这对提高程序的性能至关重要.通常,网络程序同一时候处理或者监听多个socket文件描写叙述符的时候可以考虑使用I/O复用模型. 值得强调的是.I/ ...
- OJ刷题---猜算式
题目要求: 输入代码: #include<iostream> using namespace std; void Calc(); int main() { Calc(); return 0 ...
- HTTP服务器状态码定义
HTTP服务器状态代码定义 1.1 消息1xx(Informational 1xx) 该类状态代码用于表示临时回应.临时回应由状态行(Status-Line)及可选标题组成, 由空行终止.HTTP/1 ...