1 Star 0 Fork 7

MrMriacle/cocos_creator_proj_base

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
linklist.ts 2.20 KB
一键复制 编辑 原始数据 按行查看 历史
caochao 提交于 2019-05-15 10:14 +08:00 . 更新代码
export type LinkListNode<T> = {
key:number;
data:T;
next:LinkListNode<T>;
}
export class LinkList<T>
{
private pool:LinkListNode<T>[];
private _head:LinkListNode<T>;
private _tail:LinkListNode<T>;
constructor()
{
this._head = this._tail = null;
this.pool = [];
}
private spawn_node(key:number, data:T):LinkListNode<T>
{
let node:LinkListNode<T> = this.pool.pop();
if(node)
{
node.key = key;
node.data = data;
node.next = null;
}
else
{
node = {key:key, data:data, next:null};
}
return node;
}
get head():LinkListNode<T>
{
return this._head;
}
get tail():LinkListNode<T>
{
return this._tail;
}
append(key:number, data:T):number
{
let node:LinkListNode<T> = this.spawn_node(key, data);
//将node加到linklist末尾
if(this._tail)
{
this._tail.next = node;
this._tail = node;
}
else
{
this._head = this._tail = node;
}
return node.key;
}
remove(key:number):LinkListNode<T>
{
if(!key)
{
return null;
}
if(!this._head)
{
return null;
}
let prev:LinkListNode<T>;
let curr:LinkListNode<T> = this._head;
while(curr && curr.key != key)
{
prev = curr;
curr = curr.next;
}
//没找到
if(!curr)
{
return null;
}
if(!prev)
{
//curr为头节点(要区分curr是否同时为尾节点)
this._head = curr.next;
if(!curr.next)
{
this._tail = null;
}
}
else
{
//curr非头节点(要区分curr是否为尾节点)
prev.next = curr.next;
if(!curr.next)
{
this._tail = prev;
}
}
this.pool.push(curr);
return curr;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mrmriacle/cocos_creator_proj_base.git
git@gitee.com:mrmriacle/cocos_creator_proj_base.git
mrmriacle
cocos_creator_proj_base
cocos_creator_proj_base
master

搜索帮助