javascript LinkedList:

function Node(elem, prev, next) {
this.elem = elem;
this.prev = prev ? prev : null;
this.next = next ? prev : null;
} function LinkedList() {
this.length = 0;
this.head = null; var that = this;
if (arguments.length > 0) {
var a = arguments[0];
if (a instanceof Array) {
a.forEach(function(elem) {
that.push(elem);
});
}
}
} LinkedList.prototype.push = function(elem) {
var newNode = new Node(elem); if (this.length === 0) {
this.head = newNode;
this.head.prev = newNode;
this.head.next = newNode;
} else {
newNode.next = this.head;
newNode.prev = this.head.prev;
this.head.prev.next = newNode; // !!! CAUTION !!!
this.head.prev = newNode;
}
++this.length;
return this.length;
}; // alias for push
LinkedList.prototype.append = function(elem) {
return this.push(elem);
}; LinkedList.prototype.unshift = function(elem) {
var newNode = new Node(elem); if (this.length === 0) {
this.head = newNode;
this.head.prev = newNode;
this.head.next = newNode;
} else {
newNode.next = this.head;
newNode.prev = this.head.prev;
this.head.prev.next = newNode; // !!! CAUTION !!!
this.head.prev = newNode;
}
this.head = newNode;
++this.length;
return this.length;
}; LinkedList.prototype.shift = function() {
if (this.length === 0) {
return null;
}
var head = this.head,
node = new Node(head.elem); head.next.prev = head.prev;
head.prev.next = head.next;
delete(head); this.head = head.next;
--this.length; return node.elem;
}; LinkedList.prototype.pop = function() {
if (this.length === 0) {
return null;
}
var tail = this.head.prev,
node = new Node(tail.elem); tail.prev.next = tail.next;
tail.next.prev = tail.prev; delete(tail);
--this.length; return node.elem;
}; LinkedList.prototype._get = function(index) {
if (index < 0 || index >= this.length) {
throw new DOMException("LinkedList index out of bounds!");
}
if (index===0) {
return this.head;
}
var pivot = Math.round(this.length / 2);
var p = null, i;
if (index <= pivot) {
p = this.head; // head => tail
for (i = 0; i < index; i++) {
p = p.next;
}
} else {
p = this.head.prev; // tail => head
for (i = this.length - 1; i > index; i--) {
p = p.prev;
}
}
return p;
}; LinkedList.prototype._delete = function(node) {
var retNode = new Node(node.elem); node.prev.next = node.next;
node.next.prev = node.prev; var p = node.next;
if (node===this.head) {
this.head = p;
}
node = null; this.length--;
return {
p: 0 <this.length ? p : null,
v: retNode.elem
}
}; LinkedList.prototype._insertBefore = function(node, elem) {
var newNode = new Node(elem);
newNode.next = node;
newNode.prev = node.prev;
node.prev.next = newNode;
node.prev = newNode;
++this.length;
return newNode;
}; LinkedList.prototype.get = function(index) {
var p = this._get(index);
return p.elem;
}; LinkedList.prototype.splice = function(start,deleteCount,items) {
var p = this._get(start), o, list = new LinkedList();
while (deleteCount--) {
o = this._delete(p);
p = o.p;
list.push(o.v);
if (null===p) {break;}
}
var that = this;
if (typeof items !== "undefined") {
var i = 0;
if (items instanceof Array) {
for (i = 0; i < items.length; i++) {
p = that._insertBefore(p, items[i]);
}
} else if (items instanceof LinkedList) {
for (i = 0; i < items.length; i++) {
p = that._insertBefore(p, items.get(i));
}
} else {
that._insertBefore(p, items);
}
}
return list;
}; LinkedList.prototype.forEach = function(callback) {
var p = this.head,
index = 0;
do {
callback(p.elem, index);
p = p.next;
index++;
} while (p != this.head);
return this;
}; LinkedList.prototype.map = function(callback) {
var newList = new this.__proto__.constructor();
this.forEach(function(elem) {
newList.push(callback(elem));
});
return newList;
}; LinkedList.prototype.reduce = function(callback, initValue) {
if (this === null) {
throw new TypeError('LinkedList.prototype.reduce ' +
'called on null or undefined');
}
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}
var acc = initValue,
p = this.head;
do {
acc = callback(acc, p.elem);
p = p.next;
} while (p != this.head); return acc;
}; LinkedList.prototype.join = function(sep) {
var s = "";
if (this.length === 0) {
return s;
}
if (this.length === 1) {
return this.head.elem;
}
var p = this.head;
do {
s += p.elem.toString() + sep;
p = p.next;
} while (p != this.head.prev);
s += p.elem.toString();
return s;
}; LinkedList.prototype.toString = function() {
return '[' + this.join(',') + ']';
}; LinkedList.prototype.insertBeforeIndex = function(index, elem) {
if (index === 0) {
this.unshift(elem);
return this.length;
}
if (index >this.length) {
throw new DOMException('index out of bounds');
}
if (index === this.length) {
this.append(elem);
return this.length;
}
var node = new Node(elem);
if (index === this.length-1) {
var tail = this.head.prev;
node.next = tail;
node.prev = tail.prev;
tail.prev.next = node;
tail.prev = node;
} else {
var p = this._get(index); node.next = p;
node.prev = p.prev;
p.prev.next = node;
p.next.prev = node;
}
++this.length; return this.length;
};

  

  

// Array like

 1 // test
2 var list = new LinkedList();
3
4 for (var i = 1; i < 10; i++) {
5 list.push(i);
6 }
7 for (i = 0; i < list.length; i++) {
8 console.log(list.get(i)); // 1,2,3,4,5,6,7,8,9
9 }
10
11 // list.forEach(function(elem) {console.log(elem);});
12 var newList = list.map(function(elem) {
13 return elem - 1;
14 });
15 console.log(newList.toString()); // 0,1,2,3,4,5,6,7,8
16 newList.shift();
17 console.log(newList.toString()); // 1,2,3,4,5,6,7,8
18
19 var oList = new LinkedList();
20 oList.push({ x: 2 });
21 oList.push({ x: 2 });
22 oList.push({ x: 3 });
23
24 var mul = oList.reduce(function(a, c) {
25 return a * c.x;
26 }, 1);
27 console.log(mul); // 12

map,reduce

// test 2

var a = [3, 12, 17, 16, 73, 32, 61, 46, 52, 49, 6, 5];
var list = new LinkedList(a);
console.log(list.toString());
console.log('array: ' + a.length + ' list: ' + list.length); list.insertBeforeIndex(0, 99);
console.log(list.toString()); list.insertBeforeIndex(3, 99);
console.log(list.toString()); list.insertBeforeIndex(list.length, 100);
console.log(list.toString()); list.insertBeforeIndex(list.length-1, 98);
console.log(list.toString()); console.log(list.length);

insertBeforeIndex

// test splice

function getTopN(a, n) {

    function _getMaxElem(a) {
if (a.length === 0)
throw "empty array"; if (a.length === 1)
return { index: 0, value: a[0] }; var max = a.get(0), index = 0, c;
for (var i = 0; i < a.length; i++) {
c = a.get(i);
if (c > max) {
max = c;
index = i;
}
}
return {
index: index,
value: max
}
} var o = {}, b = [];
while (n--) {
o = _getMaxElem(a);
b.push(o.value);
a.splice(o.index, 1);
} return b;
} /**
* 5 largest elements: [73,61,52,49,46]
* -------------sorted------------------
* [ 73, 61, 52, 49, 46, 32, 17, 16, 12, 6, 5, 3 ]
*/
var a = [3, 12, 17, 16, 73, 32, 61, 46, 52, 49, 6, 5];
var list = new LinkedList(a);
var top5 = getTopN(list, 5);
console.log('5 largest elements: [' + top5.toString() + ']');
console.log('-------------sorted------------------');
console.log(a.sort(function(a, b) { return b - a; }));

  

javascript LinkedList js 双向循环链表 Circular Linked List的更多相关文章

  1. 双向链表、双向循环链表的JS实现

    关于链表简介.单链表.单向循环链表.JS中的使用以及扩充方法:  单链表.循环链表的JS实现 关于四种链表的完整封装: https://github.com/zhuwq585/Data-Structu ...

  2. C语言通用双向循环链表操作函数集

    说明 相比Linux内核链表宿主结构可有多个链表结构的优点,本函数集侧重封装性和易用性,而灵活性和效率有所降低.     可基于该函数集方便地构造栈或队列集.     本函数集暂未考虑并发保护. 一  ...

  3. 1.Go语言copy函数、sort排序、双向链表、list操作和双向循环链表

    1.1.copy函数 通过copy函数可以把一个切片内容复制到另一个切片中 (1)把长切片拷贝到短切片中 package main import "fmt" func main() ...

  4. 1.Go-copy函数、sort排序、双向链表、list操作和双向循环链表

    1.1.copy函数 通过copy函数可以把一个切片内容复制到另一个切片中 (1)把长切片拷贝到短切片中 ? 1 2 3 4 5 6 7 8 9 10 11 12 package main   imp ...

  5. Vue.js双向绑定的实现原理和模板引擎实现原理(##########################################)

    Vue.js双向绑定的实现原理 解析 神奇的 Object.defineProperty 这个方法了不起啊..vue.js和avalon.js 都是通过它实现双向绑定的..而且Object.obser ...

  6. 双向循环链表(C语言描述)(四)

    下面以一个电子英汉词典程序(以下简称电子词典)为例,应用双向循环链表.分离数据结构,可以使逻辑代码独立于数据结构操作代码,程序结构更清晰,代码更简洁:电子词典的增.删.查.改操作分别对应于链表的插入. ...

  7. 双向循环链表(C语言描述)(一)

    双向循环链表是链表的一种,它的每个节点也包含数据域和指针域.为了方便程序维护,可以单独为数据域定义一种数据类型,这里以整型为例: typedef int LinkedListData; 双向循环链表( ...

  8. "《算法导论》之‘线性表’":双向循环链表

    本文双链表介绍部分参考自博文数组.单链表和双链表介绍 以及 双向链表的C/C++/Java实现. 1 双链表介绍 双向链表(双链表)是链表的一种.和单链表一样,双链表也是由节点组成,它的每个数据结点中 ...

  9. Angular JS - 3 - Angular JS 双向数据绑定

    一 .数据绑定 1. 数据绑定: 数据从一个地方A转移(传递)到另一个地方B, 而且这个操作由框架来完成2. 双向数据绑定: 数据可以从View(视图层)流向Model(模型,也就是数据), 也可以从 ...

随机推荐

  1. Windows常用命令汇总以及基础知识

    命令部分: dir dir指定要列出的驱动器.目录和/或文件 ,/?显示所有命令 例:dir /b /s /o:n /a:a 表示显示当前路径下的所有文件的绝对路径,包含子文件夹的内容 /b表示去除摘 ...

  2. SSM自学笔记(七)

    14.MyBatis的多表操作 1.MyBatis的多表操作 1.1 一对一查询 一对一查询的模型 用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户 一对一查询的需求:查询一个订单 ...

  3. C# 查询所有设备的插拔事件

    private void test() { //Win32_DeviceChangeEvent  Win32_VolumeChangeEvent ManagementEventWatcher watc ...

  4. 一 MongoDB入门

    一.MongoDB概念解析(对比MySQL学习): 举个例子: MongoDB可视化操作工具:推荐Robomongo 二.MongoDB默认的概念: 1.MongoDB的单个实例可以容纳多个独立的数据 ...

  5. 超详细教程2021新版oracle官网下载Windows JAVA-jdk11并安装配置(其他版本流程相同)

    异想之旅:本人博客完全手敲,绝对非搬运,全网不可能有重复:本人无团队,仅为技术爱好者进行分享,所有内容不牵扯广告.本人所有文章发布平台为CSDN.博客园.简书和开源中国,后期可能会有个人博客,除此之外 ...

  6. Django的基本运用(垃圾分类)

    title: 利用Django实现一个能与用户交互的初级框架 author: Sun-Wind date: September 1, 2021 Django实现基本的框架 此框架的功能是搭建服务器,使 ...

  7. MySQL主库手动复制至从库

    原文转自:https://www.cnblogs.com/itzgr/p/10233932.html作者:木二 目录 一 主库手动复制至从库 1.1 Master主库锁表 1.2 主库备份 1.3 从 ...

  8. 基于Linux系统的网络服务——高速缓存DNS及企业级域名解析服务

    1.DNS域名系统 DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数 ...

  9. pip install 报错 TypeError: 'module' object is not callable

    $ pip install filetype Traceback (most recent call last): File "/usr/local/bin/pip2", line ...

  10. openresty lua_ssl_trusted_certificate 问题

    lua_ssl_trusted_certificate 语法: lua_ssl_trusted_certificate 默认: no 环境: http, server, location 指定一个 P ...