数据结构篇(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 ...
随机推荐
- [bzoj2878][Noi2012]迷失游乐园(基环树dp)
[bzoj2878][Noi2012]迷失游乐园(基环树dp) bzoj luogu 题意:一颗数或是基环树,随机从某个点开始一直走,不走已经到过的点,求无路可走时的路径长期望. 对于一棵树: 用两个 ...
- Hystrix相关注解?
@EnableHystrix:开启熔断 @HystrixCommand(fallbackMethod="XXX"):声明一个失败回滚处理函数XXX,当被注解的方法执行超时(默认是 ...
- Centos6 编译安装Python3.6
1. 安装依赖 yum install gcc openssl-devel bzip2-devel 2. 下载Python3.6 cd /usr/src wget https://www.python ...
- Oracle 数据库备份实战
最近公司的客户希望使用oracle数据库,所以我们只好将数据从mysql数据库迁移到oracle数据库,并对oracle数据库制定了一个备份策略,之前虽然对oracle很熟悉,但做备份策略还是第一次, ...
- (stm32f103学习总结)—stm32中断系统
一.NVIC 介绍 NVIC 英文全称是 Nested Vectored Interrupt Controller,中文意思就是嵌套向量中断控制器,它属于 M3 内核的一个外设,控制着芯片的中断相关功 ...
- 基于MPC算法的车辆多目标自适应巡航控制系统研究_荆亚杰
- 推荐一款强大的轻量级模块化WEB前端快速开发框架--UIkit
前言 今天给大家分享一款强大的轻量级模块化WEB前端快速开发框架--UIkit 到目前(2016-06-20)为止,UIkit在github上的Forks已达到了1350个,而Stars更是达到了69 ...
- css-theme 通过一套源码生成一份包含多套皮肤配置的样式文件
css-theme 通过单一css文件生成多套主题,并合并入一个css文件中 特性 只加载一个css,通过切换rootClass瞬间切换主题 体积压缩,将多套css合并,去除冗余代码,避免文件体积膨胀 ...
- Issues with position fixed & scroll(移动端 fixed 和 scroll 问题)
转载请注明英文原文及译文出处 原文地址:Issues with position fixed & scrolling on iOS 原文作者:Remy Sharp译文地址:移动端 fixed ...
- ES6-11学习笔记--模块化
模块化规范有: CommonJS:Node.js AMD:require.js CMD:sea.js ES6:Module ES6模块化使用: 关键词:export.import.as.exp ...