DoublyLinkedList
//双向链表//链接是双向的,一个链向下一个元素,一个链向上一个元素,同时控制next和prev两个指针//head/tailfunction DoublyLinkedList(){function Node(data){this.data = null;this.prev = null;this.next = null;}this.head = null;this.tail = null;this.length = 0;DoublyLinkedList.prototype.append = function (data){var newNode = new Node(data);if(this.length == 0){this.head = newNode;this.tail = newNode;}else{newNode.prev = this.tail;this.tail.next = newNode;this.tail = newNode;}this.length += 1;}DoublyLinkedList.prototype.toString = function(){return this.backwardString();}DoublyLinkedList.prototype.forwardString = function(){var current = this.tail;var resultString = "";while(current){resultString += current.data + "";current = current.prev;}return resultString;}DoublyLinkedList.prototype.backwardString = function(){var current = this.head;var resultString = "";while(current){resultString += current.data + "";current = current.next;}return resultString;}DoublyLinkedList.prototype.insert = function(position, data){if(position < 0 || position > this.length) return false;var newNode = new Node(data);if(this.length == 0){this.head = newNode;this.tail = newNode;}else{if(postion == 0){this.head.prev = newNode;newNode.next = this.head;this.head = newNode;}else if(position == this.length){newNode.prev = this.tail;this.tail.next = newNode;this.tail = newNode;}else{var current = this.head;var index = 0;while(index++ < position){current = current.next;}newNode.next = current;newNode.prev = current.prev;current.prev.next = newNode;current.prev = newNode;}}this.length += 1;return true;}DoublyLinkedList.prototype.get = function(position){if(position < 0 || position >= this.length) return null;var current = this.head;var index = 0;while(index++ < position){current = current.next;}return current.data;}DoublyLinkedList.prototype.indexOf = function(data){var current = this.head;var index = 0;while(current){if(current.data == data){return index;}current = current.next;index += 1;}return -1;}DoublyLinkedList.prototype.update = function(position, newData){if(position < 0 || position >= this.length) return false;var current = this.head;var index = 0;while(index++ < position){current = current.next;}current.data = newData;return true;}DoublyLinkedList.prototype.removeAt = function(position){if(position < 0 || position >= this.length) return null;var current = this.head;if(this.length == 1){this.head = null;this.tail = null;}else{if(position == 0){this.head.next.prev = null;this.head = this.head.next;}else if(position == this.length - 1){var current = this.tail;this.tail.prev.next = null;this.tail = this.tail.prev;}else{var index = 0;while(index++ < position){current = current.next;}current.prev.next = current.next;current.next.prev = current.prev;}}this.length -=1;return current.data;}DoublyLinkedList.prototype.remove = function(){var index = this.indexOf(data);return this.removeAt(index);}}
DoublyLinkedList相关推荐
- 看动画学算法之:doublyLinkedList
文章目录 简介 doublyLinkedList的构建 doublyLinkedList的操作 头部插入 尾部插入 插入给定的位置 删除指定位置的节点 简介 今天我们来学习一下复杂一点的LinkedL ...
- Python数据结构实战——双向链表(DoublyLinkedList)
文章目录 1.定义结点类 2.定义链表类 2.1.打印正向链表 2.2.打印反向链表 2.3.寻找链表最后一个结点 2.4.计算链表长度 2.5.链表首部插入元素 2.6.链表尾部插入元素 2.7.链 ...
- 【JS数据结构与算法】双向链表(DoublylinkedList)封装及其方法
目录 双向链表结构 双向链表的封装 一.追溯方法(append(data)) 二.toString()的两种方式. 1.forwardString()(返回正向遍历的节点字符串形式) 2.backwa ...
- 数据结构与算法(四)-双向链表(DoublyLinkedList)
双向链表(DoublyLinkedList)结构 文章目录 双向链表(DoublyLinkedList)结构 一.双向链表的介绍 单向链表的特性 双向链表结构 双向链表的缺点 二.双向链表的封装 常见 ...
- 数据结构与算法 | 线性表 —— 链表
原文链接:wangwei.one/posts/java-- 链表 定义 逻辑结构上一个挨一个的数据,在实际存储时,并没有像顺序表那样也相互紧挨着.恰恰相反,数据随机分布在内存中的各个位置,这种存储结构 ...
- Javascript的数据结构与算法(一)
1数组 1.1方法列表 数组的常用方法如下: concat: 链接两个或者更多数据,并返回结果. every: 对数组中的每一项运行给定的函数,如果该函数对每一项都返回true,则返回true. fi ...
- python链表的创建_《大话数据结构》配套源码:链表(Python版)
该书随书源码的语言为C:我参考书中内容和配套源码,写了一套Python格式的配套源码.这套配套源码并非直接翻译C语言的配套源码,而是结合我的理解略作了修改. SinglyLinkedNode 单链表结 ...
- JS数据结构与算法_链表
上一篇:JS数据结构与算法_栈&队列 下一篇:JS数据结构与算法_集合&字典 写在前面 说明:JS数据结构与算法 系列文章的代码和示例均可在此找到 上一篇博客发布以后,仅几天的时间竟然 ...
- libevent中的缓冲区(一)
libevent中的缓冲区定义为evbuffer,主要在文件evbuffer-internal.h文件中,定义如下 struct evbuffer {/** The first chain in ...
- Java数据结构和算法:数组、单链表、双链表
1. 概要 线性表是一种线性结构,它是具有相同类型的n(n≥0)个数据元素组成的有限序列.本章先介绍线性表的几个基本组成部分:数组.单向链表.双向链表:随后给出双向链表的C.C++和Java三种语言的 ...
最新文章
- Linux学习:shell命令(文件权限、用户、用户组)
- vim语法高亮的错误解决办法
- python制作计算机程序_用 Python 开发实用程序 – 计算器
- javascrpit树(未完)
- 关于Eclipse安装Scala插件不显示
- [vue] 写出多种定义组件模板的方法
- Mybatis源码阅读(四):核心接口4.2——Executor(上)
- MVC教程第六篇:拦截器
- CSS盒子的三种类型(border-box、content-box)
- 鼠标放到图片上替换图片,改变样式。
- 图灵奖得主Alan Kay如何读书
- 吉林大学计算机数据中心排名,2018年度中国医院排行榜发布,吉大一院跻身50强!...
- windows系统设置保护视力方法
- 计算机二进制除法除数为0,怎么做二进制数的除法运算
- 关于用C#实现宽带的连接
- 实现类似PS魔棒功能(漫水填充floodfill()) OpenCV
- C语言 简易计算器 //支持求阶乘
- MT【109】线面角最大时为二面角平面角
- 水下SLAM论文!!!
- Archetype是什么?
热门文章
- 《项目开发团队分配管理软件》
- 服务器虚拟内存可以设置其他盘,Win7系统如何把虚拟内存设置在其它盘符?
- 我的前端“先行”之路
- python opencv颜色通道_OpenCV Python NumPy操作色彩通道
- element表格设置fixed后滚动条滚动时右下角空白块
- 唯品会收购第三方支付牌照正式落槌 浙江贝付完成更名
- 解决 Cannot uninstall 'ipython'. It is a distutils installed project and thus we cannot accurately det
- php命名空间namespace应用
- 设置虚拟机dns服务器域名,域名服务器DNS的设置实验
- 红外测温之MLX90614教程