博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据结构与算法之链表
阅读量:4925 次
发布时间:2019-06-11

本文共 4385 字,大约阅读时间需要 14 分钟。

function Node (element){            this.element = element;            this.next = null;        }        function LinkedList(){            this.length = 0;            this.head = null;        }        LinkedList.prototype = {            //向表尾添加一个元素            append :function(element){                        var node = new Node(element),current;                if(this.head === null){                    this.head = node;                }else {                    current = this.head;                    while(current.next){                        current = current.next;                    }                    current.next = node;                }                length++;            },            //任意位置插入一个元素            insert :function(position,element){                        if(position >= 0 && position <= length){                    var node = new Node(element),                        current = this.head,                        previous,                        index = 0;                    if(position === 0 ){                        node.next = current;                        this.head = node;                    } else {                        while(index ++ < position){                            previous = current;                            current  = current.next;                        }                        node.next = current;                        previous.next = node;                    }                    length++;                    return true;                } else {                    return false;                }            },            //移除指定项            removeAt :function(position){                                if(position>-1 && position 
= 0 && position <= this.length){ var node = new DoublyNode(element), current = this.head, previous, index = 0; if(position === 0){ //第一个位置添加 if(!this.head){ this.head = node; this.tail = node; } else { node.next = current; current.prev = node; this.head = node; } } else if(position === this.length){ //最后一项添加 current = this.tail; current.next = node; node.prev = current; this.tail = node; } else { while(index++ < position){ previous = current; current = current.next; } node.prev = previous; node.next = current; previous.next = node; current.prev = node; } length++; return true; } else { return false; } }, removeAt :function(position){ //任意位置移除元素 if(position > -1 && position < this.length){ var current = this.head, previous, index=0; if(position === 0){ //移除第一项 this.head = current.next; if(this.length === 1){ this.tail = null; } else { this.head.prev = null; } } else if(position === this.length-1){ //移除最后一项 current = this.tail; this.tail = current.prev; this.tail.next = null; } else { while(index++ < position){ previous = current; current = current.next; } previous.next = current.next; current.next.prev = previous; } this.length--; return current.element; } else { return null; } }, };

 

转载于:https://www.cnblogs.com/kerryw/p/8306680.html

你可能感兴趣的文章
CSS可以和不可以继承的属性
查看>>
eclipse每次当我按ctrl+鼠标点击代码,自动关闭,产生原因及解决办法!!
查看>>
hbase
查看>>
用PHP将Unicode 转化为UTF-8
查看>>
HDOJ1002 A+B Problem II
查看>>
ADB server didn't ACK(adb不能开启
查看>>
网页内容抓取
查看>>
分布式和集群的区别
查看>>
Python基础(三)
查看>>
Sql server在cmd下的使用
查看>>
【BZOJ 1019】 1019: [SHOI2008]汉诺塔 (DP?)
查看>>
织梦DEDECMS系统中文章内容为空 用SQL语句如何删除?
查看>>
load data导入数据之csv的用法
查看>>
silverlight调用MVC WebApi方法
查看>>
建表sql实例
查看>>
区块链北大课程总结(课程1-密码学原理)
查看>>
web页面开发笔记(不断更新)
查看>>
<转>C#读取doc,pdf,ppt文件
查看>>
缓存帮助类
查看>>
VC++ 2010 创建高级Ribbon界面详解(1)
查看>>