数据结构篇(2) ts实现单链表
interface NodeItem {
prev: NodeItem | null
next: NodeItem | null
data: any
}
class NodeItem {
prev: NodeItem | null = null
next: NodeItem | null = null
constructor(data: any) {
this.data = data ? data : null;
}
}
interface ListNode {
head: any
size: number
currentNode: NodeItem | null
find(item:any):NodeItem | null // 在单链表中寻找item元素
insert(element:any,item:any):void // 向单链表中插入元素
remove(item:any):void // 在单链表中删除一个节点
append(element:any):void // 在单链表的尾部添加元素
findLast():NodeItem // 获取单链表的最后一个节点
isEmpty():boolean // 判断单链表是否为空
show():void // 显示当前节点
getLength():number // 获取单链表的长度
advance(n:number, currNode:NodeItem):void // 从当前节点向前移动n个位置
display():void // 单链表的遍历显示
clear():void // 清空单链表
}
class ListNode {
head: any = null
size: number = 0
currentNode: NodeItem | null = null
constructor() {
this.head = new NodeItem('head');
}
find(item:any):NodeItem | null {
let currNode = this.head;
while(currNode) {
if(currNode.data === item) {
return currNode;
}
currNode = currNode.next;
}
return null;
}
insert(element:any,item:any):any {
if(!this.find(element)) {
return;
}
let prevNode = this.find(element);
if(prevNode == null) {
return ;
}
let currNode = new NodeItem(item);
currNode.next = prevNode.next;
prevNode.next = currNode;
this.size++;
}
remove(item:any):void {
if(!this.find(item)) {
return ;
}
if(item === 'head') {
if(!this.isEmpty()) {
return ;
} else {
this.head.next = null;
return;
}
}
let currNode = this.head;
while(currNode) {
if(currNode&&currNode.next.data === item) {
break;
}
currNode = currNode.next;
}
currNode.next = currNode.next.next;
this.size--;
}
append(element:any):void {
let rear = this.findLast();
let currentNode = new NodeItem(element);
rear.next = currentNode;
this.size++;
}
findLast():NodeItem {
let currNode:NodeItem = this.head;
while(currNode.next) {
currNode = currNode.next;
}
return currNode;
}
isEmpty():boolean {
if(this.head.next) {
return true;
}
return false;
}
show():void {
console.log(this.currentNode?.data);
}
getLength():number {
let i = 0;
let currNode = this.head;
while(currNode) {
i++;
currNode = currNode.next;
}
return i;
}
advance(n:number, currNode:NodeItem = this.head):NodeItem {
this.currentNode = currNode;
while((n--)&&this.currentNode.next) {
this.currentNode = this.currentNode?.next;
}
return this.currentNode;
}
display():void {
let currentNode = this.head;
while(currentNode) {
console.log(currentNode.data);
currentNode = currentNode.next;
}
}
clear():void {
this.head.next = null;
this.size = 0;
}
}
let myList = new ListNode();
let arr = [3, 4, 5, 6, 7, 8, 9];
for(let i=0; i<arr.length; i++){
myList.append(arr[i]);
}
myList.insert(3,2);
myList.display();
数据结构篇(2) ts实现单链表的更多相关文章
- 数据结构篇(2) ts实现单循环链表
JS的class可以通过extends关键字实现类似其他语言的继承效果,比起使用一个extends关键字,在es5中实现继承要复杂一些,可以通过修改原型链的方法实现继承,让一个原型对象等于另一个类型的 ...
- 新秀nginx源代码分析数据结构篇(两) 双链表ngx_queue_t
nginx源代码分析数据结构篇(两) 双链表ngx_queue_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn. ...
- 菜鸟nginx源代码剖析数据结构篇(八) 缓冲区链表ngx_chain_t
菜鸟nginx源代码剖析数据结构篇(八) 缓冲区链表 ngx_chain_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog. ...
- 菜鸟nginx源码剖析数据结构篇(八) 缓冲区链表ngx_chain_t[转]
菜鸟nginx源码剖析数据结构篇(八) 缓冲区链表 ngx_chain_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.c ...
- 菜鸟nginx源码剖析数据结构篇(三) 单向链表 ngx_list_t[转]
菜鸟nginx源码剖析数据结构篇(三) 单向链表 ngx_list_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csd ...
- 数据结构篇(3)ts 实现双向链表
如今终于到了双向链表了,此前在Node结构中的prev指针终于派上了用场.由于双向链表多了一个前向指针,所以有些操作和单向链表比较起来反而更加的简单. class DbList extends Cir ...
- 数据结构篇(1) ts实现栈的基本操作和解决相关问题
interface Stack { _items: any push(element: any): void pop(): any top(): any size(): any isEmpty(): ...
- 图解Redis之数据结构篇——链表
前言 Redis链表为双向无环链表! 图解Redis之数据结构篇--简单动态字符串SDS提到Redis使用了简单动态字符串,链表,字典(散列表),跳跃表,整数集合,压缩列表这些数据结构 ...
- C语言实现的单链表
链表是一种线性表,但是并不是顺序存储,而是每个节点里面存储着下一个节点的指针,把存储数据元素的数据串链起来. 单链表的基本实现: typedef int DataType;//定义单链表typedef ...
随机推荐
- leetcode刷题1--动态规划法回文串2
题目是: Given a string s,partition s such that every substring of the partition is a palindrome Return ...
- Android studio Error occurred during initialization of VM
Unable to start the daemon process. This problem might be caused by incorrect configuration of the d ...
- 关于web以及浏览器处理预加载有哪些思考?
图片等静态资源在使用之前就提前请求资源使用到的时候能从缓存中加载, 提升用户体验页面展示的依赖关系维护
- 在Spring框架中如何更有效地使用JDBC?
使用SpringJDBC 框架,资源管理和错误处理的代价都会被减轻.所以开发者只需写statements 和 queries从数据存取数据,JDBC也可以在Spring框架提供的模板类的帮助下更有效地 ...
- Dubbo 支持分布式事务吗?
目前暂时不支持,可与通过 tcc-transaction 框架实现 介绍:tcc-transaction 是开源的 TCC 补偿性分布式事务框架 Git 地址:https://github.com/c ...
- 如何给 Spring 容器提供配置元数据?
这里有三种重要的方法给 Spring 容器提供配置元数据. XML 配置文件. 基于注解的配置. 基于 java 的配置.
- 学习GlusterFS(二)
环境准备 3台机器,每个机器双网卡,每个机器还需要额外添加1个10GB的磁盘用于测试 机器系统版本是centos6.6 1 2 3 4 5 [root@gluster-1-1 ~]# uname -r ...
- windows本地搭建easy-mock环境
起因:由于easy-mock官网很不稳定,所以想搭建自己本地的mock环境 1.首先安装node.js 环境 (提供地址:https://nodejs.org/en/) 2.下载mongoDB 地址( ...
- 安卓性能优化之计算apk启动时间
之前有人在知乎提问:"怎么计算apk的启动时间?" : 利用Python或者直接用adb命令怎么计算apk的启动时间呢?就是计算从点击图标到apk完全启动所花费的时间.比如,对游戏 ...
- CCF201609-2火车购票
问题描述 请实现一个铁路购票系统的简单座位分配算法,来处理一节车厢的座位分配. 假设一节车厢有20排.每一排5个座位.为方便起见,我们用1到100来给所有的座位编号,第一排是1到5号,第二排是6到10 ...