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. Slope Trick:解决一类凸代价函数DP优化

    [前言] 在补Codeforce的DP时遇到一个比较新颖的题,然后在知乎上刚好 hycc 桑也写了这道题的相关题解,这里是作为学习并引用博客的部分内容 这道题追根溯源发现2016年这个算法已经在API ...

  2. 「移动端」touch事件,touchEvent对象

    随着智能手机普及,有越来越多的手机网页和网页版游戏,手机触摸.移动.旋转等等,多种操作.一般电脑的人机交互靠的是鼠标,而手机用的就是触摸.区别有: PC 端一个电脑只能有一个鼠标,而移动端有多点触摸. ...

  3. Windows莫名内存到百分之百,需要修改虚拟内存

    借鉴别人的操作: https://blog.csdn.net/xjpdf10/article/details/82849112

  4. WPF 绘图 和动画

    wpf 的动画:https://www.cnblogs.com/TianFang/p/4050845.html

  5. vue + iview 怎样在vue项目下添加ESLint

    参考:https://segmentfault.com/a/1190000012019019?utm_source=tag-newest 使用iview框架的MenuGroup标签,vscode报红, ...

  6. C语言预处理编译链接各个阶段错误,分阶段的说一下

    C语言预处理编译链接各个阶段错误,分阶段的说一下 C语言预处理编译链接各个阶段错误,分阶段的说一下比如指针异常,数组下标越界什么的    我来答 1个回答 #热议# 你觉得这辈子有希望看到996消失 ...

  7. Spring详解(十)------spring 环境切换

    软件开发过程一般涉及"开发 -> 测试 -> 部署上线"多个阶段,每个阶段的环境的配置参数会有不同,如数据源,文件路径等.为避免每次切换环境时都要进行参数配置等繁琐的操 ...

  8. ProjectEuler 003题

    1 //题目:The prime factors of 13195 are 5, 7, 13 and 29. 2 //What is the largest prime factor of the n ...

  9. vue@cli3 public目录下的静态图片,如何使用在css类文件中(sass可行,纯css不行)

    之前写了一篇vue文件怎么使用的文章,有人问我怎么在css文件中使用public下的文件,这是个好问题,因为我之前都没有研究过 需要解决的2个问题 一开始按照vue文件的使用方式(https://ww ...

  10. 基于Linux的系统排错

    1.系统引导过程概述 2.系统异常及恢复 [1]grub系统引导 1)mbr上446字节丢失 模拟问题: dd if=/dev/zero? of=/dev/vda? bs=446? count=1? ...