数据结构篇(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 ...
随机推荐
- WeakHashMap 是怎么工作的?
WeakHashMap 的工作与正常的 HashMap 类似,但是使用弱引用作为 key,意思就是当 key 对象没有任何引用时,key/value 将会被回收.
- 如何在 spring 中启动注解装配?
默认情况下,Spring 容器中未打开注解装配.因此,要使用基于注解装配,我们 必须通过配置 <context:annotation-config/> 元素在 Spring 配置文件 中启 ...
- 什么是 MyBatis 的接口绑定?有哪些实现方式?
接口绑定,就是在 MyBatis 中任意定义接口,然后把接口里面的方法和 SQL 语句绑 定, 我们直接调用接口方法就可以,这样比起原来了 SqlSession 提供的方法我们可 以有更加灵活的选择和 ...
- 学习JDK之“Future机制==>多线程”
什么是Future接口 Future是java.util.concurrent.Future,是Java提供的接口,可以用来做异步执行的状态获取,它避免了异步任务在调用者那里阻塞等待,而是让调用者可以 ...
- 聊聊 DisplayObject 的x/y/regX/regY/rotation/scale/skew 属性
首先要指出的是:DisplayObject 实例的属性<x, y> 与 graphics.draw*(x, y, ...) 的参数<x, y>没有关系. 在原生的 Canvas ...
- js随手笔记-------理解JavaScript碰撞检测算法核心简单实现原理
碰撞检测在前端游戏,设计拖拽的实用业务等领域的应用场景非常广泛,今天我们就在这里对于前端JavaScript如何实现碰撞检测算法进行一个原理上的探讨,让大家能够明白如何实现碰撞以及碰撞的理念是什么:1 ...
- 关于websocket制作聊天室的的一些总结
websocket的总结 在一个聊天室系统中,常常使用websocket作为通信的主要方式.参考地址:https://www.jianshu.com/p/00e... 关于自己的看法:websocke ...
- Hibernate快速上手
一. Hibernate介绍 1. Hibernate简介 Hibernate是一个开放源码的对象-关系映射(ORM)框架,他对JDBC进行了轻量级封装,开发人员可以使用面向对象的编程思想来进行持久层 ...
- sring框架的jdbc应用
xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http: ...
- EMS邮箱数据库全局监控设置
案例任务:监控TestDB01邮箱数据库的所有邮件,监控邮箱为用户"王淑江"的邮箱. 1.EMS全局监控设置 使用PowerShell完成操作:"王淑江"监控T ...