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实现单链表的更多相关文章

  1. 数据结构篇(2) ts实现单循环链表

    JS的class可以通过extends关键字实现类似其他语言的继承效果,比起使用一个extends关键字,在es5中实现继承要复杂一些,可以通过修改原型链的方法实现继承,让一个原型对象等于另一个类型的 ...

  2. 新秀nginx源代码分析数据结构篇(两) 双链表ngx_queue_t

    nginx源代码分析数据结构篇(两) 双链表ngx_queue_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn. ...

  3. 菜鸟nginx源代码剖析数据结构篇(八) 缓冲区链表ngx_chain_t

    菜鸟nginx源代码剖析数据结构篇(八) 缓冲区链表 ngx_chain_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog. ...

  4. 菜鸟nginx源码剖析数据结构篇(八) 缓冲区链表ngx_chain_t[转]

    菜鸟nginx源码剖析数据结构篇(八) 缓冲区链表 ngx_chain_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.c ...

  5. 菜鸟nginx源码剖析数据结构篇(三) 单向链表 ngx_list_t[转]

    菜鸟nginx源码剖析数据结构篇(三) 单向链表 ngx_list_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csd ...

  6. 数据结构篇(3)ts 实现双向链表

    如今终于到了双向链表了,此前在Node结构中的prev指针终于派上了用场.由于双向链表多了一个前向指针,所以有些操作和单向链表比较起来反而更加的简单. class DbList extends Cir ...

  7. 数据结构篇(1) ts实现栈的基本操作和解决相关问题

    interface Stack { _items: any push(element: any): void pop(): any top(): any size(): any isEmpty(): ...

  8. 图解Redis之数据结构篇——链表

    前言     Redis链表为双向无环链表!     图解Redis之数据结构篇--简单动态字符串SDS提到Redis使用了简单动态字符串,链表,字典(散列表),跳跃表,整数集合,压缩列表这些数据结构 ...

  9. C语言实现的单链表

    链表是一种线性表,但是并不是顺序存储,而是每个节点里面存储着下一个节点的指针,把存储数据元素的数据串链起来. 单链表的基本实现: typedef int DataType;//定义单链表typedef ...

随机推荐

  1. CF736D Permutations(伴随矩阵)

    CF736D Permutations(伴随矩阵) Luogu 题解时间 首先把边直接放进邻接矩阵, 很明显行列式的奇偶和方案数的奇偶一样. 设 $ A_{ i , j } $ 为矩阵的该行列的余子式 ...

  2. JavaScript day03 循环

    循环 while循环 循环是重复性做一件事情 没有办法控制每次循环的时间长度 循环会增大程序时间复杂度(不建议无限循环嵌套 一般情况下不会嵌套超过两次) 死循环 是不会停止的循环 会导致电脑内存溢出 ...

  3. 在JVM的新生代内存中,为什么除了Eden区,还要设置两个Survivor区

    在JVM的新生代内存中,为什么除了Eden区,还要设置两个Survivor区? 1 为什么要有Survivor区 先不去想为什么有两个Survivor区,第一个问题是,设置Survivor区的意义在哪 ...

  4. idea使用maven工程创建web项目并支持jsp

    主要要再pom文件里面添加依赖: <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> & ...

  5. Django获取请求的IP地址

    if request.META.get('HTTP_X_FORWARDED_FOR'): ip = request.META.get("HTTP_X_FORWARDED_FOR") ...

  6. mysql学习 | LeetCode数据库简单查询练习

    力扣:https://leetcode-cn.com/ 力扣网数据库练习:https://leetcode-cn.com/problemset/database/ 文章目录 175. 组合两个表 题解 ...

  7. CCF201403-2窗口

    问题描述 在某图形操作系统中,有 N 个窗口,每个窗口都是一个两边与坐标轴分别平行的矩形区域.窗口的边界上的点也属于该窗口.窗口之间有层次的区别,在多于一个窗口重叠的区域里,只会显示位于顶层的窗口里的 ...

  8. mysql 合并查询结果

     UNION 使用 UNION 关键字是,数据库系统会将所有的查询结果合并到一起,然后去除掉相同的记录:   UNION ALL 使用 UNION ALL,不会去除掉系统的记录:

  9. Python使用递归绘制谢尔宾斯基三角形

    谢尔宾斯基三角形使用了三路递归算法,从一个大三角形开始,通过连接每一个边的中点,将大三角型分为四个三角形,然后忽略中间的三角形,依次对其余三个三角形执行上述操作. 运行效果: 源代码: 1 impor ...

  10. CSS简单样式练习(五)

    运行效果: 源代码: 1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta char ...