博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js数据结构--链表(likedList)
阅读量:6826 次
发布时间:2019-06-26

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

链表

  1. append(element): 向列表尾部添加一个新的项
  2. insert(position,element):向列表的特定位置插入一个新的项
  3. remove(element): 从列表中移除一项
  4. indexOf(element): 返回元素在列表中的索引.如果列表中没有该元素则返回-1.
  5. removeAt(position):从列表的特定位置移除一项
  6. isEmpty(): 如果链表中不包含任何元素,返回true,如果链表长度大于0则返回false
  7. size(): 返回链表包含的元素个数。与数组的length属性类似
  8. toString(): 由于列表项使用了Node类,就需要重写继承自JavaScript对象默认的toString()方法,让其只输出元素的值

链表实现append,getHead

likedList.js

var LikedList = function() {    //链表头    var head = null;    // 链表长度    var length = 0;    // 节点    var Node = function(el) {            this.el = el;            this.next = null;        }        //链表尾添加元素    this.append = function(el) {            var node = new Node(el) // 1     2            if (head == null) {                head = node; //head =1            } else {                var current = head; //current=1                while (current.next) { //null                    current = current.next;                }                //while循环执行完之后,current已经是链表的最后一项了                current.next = node //current.next = node = 2            }            length++        }    this.getHead = function() {        return head;    }}复制代码

实例化

链表插入 insert

likedList.js

var LikedList = function() {    //链表头    var head = null;    // 链表长度    var length = 0;    // 节点    var Node = function(el) {            this.el = el;            this.next = null;        }        //链表尾添加元素    this.append = function(el) {            var node = new Node(el) // 1     2            if (head == null) {                head = node; //head =1            } else {                var current = head; //current=1                while (current.next) { //null                    current = current.next;                }                //while循环执行完之后,current已经是链表的最后一项了                current.next = node //current.next = node = 2            }            length++        }        //链表某一个位置添加元素    this.insert = function(position, el) {        //越界        if (position > -1 && position < length) {            var node = new Node(el);            if (position == 0) {                var current = head;                head = node;                head.next = current;            } else {                var index = 0;                var current = head;                var previous = null;                while (index < position) {                    previous = current;                    current = current.next;                    index++                }                previous.next = node;                node.next = current;            }            length++        }    }    this.getHead = function() {        return head;    }}复制代码

实例化

remove removeAt

likedList.js

var LikedList = function() {    //链表头    var head = null;    // 链表长度    var length = 0;    // 节点    var Node = function(el) {            this.el = el;            this.next = null;        }    this.removeAt = function(position) {        if (position > -1 && position < length) {            if (position == 0) {                var current = head;                head = current.next;            } else {                var current = head;                var previous = null;                var index = 0;                while (index < position) {                    previous = current                    current = current.next;                    index++                }                previous.next = current.next;            }            length--            return current        }        return null    }    this.indexOf = function(el) {        var current = head;        var index = 0;        while (current) {            if (current.el == el) {                return index            }            current = current.next            index++        }        return -1    }    this.remove = function(el) {        return this.removeAt(this.indexOf(el))    }}复制代码

实例化

链表所有功能

likedList.js

var LikedList = function() {    //链表头    var head = null;    // 链表长度    var length = 0;    // 节点    var Node = function(el) {            this.el = el;            this.next = null;        }        //链表尾添加元素    this.append = function(el) {            var node = new Node(el) // 1     2            if (head == null) {                head = node; //head =1            } else {                var current = head; //current=1                while (current.next) { //null                    current = current.next;                }                //while循环执行完之后,current已经是链表的最后一项了                current.next = node //current.next = node = 2            }            length++        }    //链表某一个位置添加元素    this.insert = function(position, el) {        //越界        if (position > -1 && position < length) {            var node = new Node(el);            if (position == 0) {                var current = head;                head = node;                head.next = current;            } else {                var index = 0;                var current = head;                var previous = null;                while (index < position) {                    previous = current;                    current = current.next;                    index++                }                previous.next = node;                node.next = current;            }            length++        }    }    //删除指定位置的元素    this.removeAt = function(position) {        if (position > -1 && position < length) {            if (position == 0) {                var current = head;                head = current.next;            } else {                var current = head;                var previous = null;                var index = 0;                while (index < position) {                    previous = current                    current = current.next;                    index++                }                previous.next = current.next;            }            length--            return current        }        return null    }    //查找链表元素下标    this.indexOf = function(el) {        var current = head;        var index = 0;        while (current) {            if (current.el == el) {                return index            }            current = current.next            index++        }        return -1    }    //删除指定元素    this.remove = function(el) {        return this.removeAt(this.indexOf(el))    }    // 是否为空    this.isEmpty = function() {        return length == 0;    }    //链表长度    this.size = function() {        return length    }    //链表头    this.getHead = function() {        return head;    }}复制代码

转载地址:http://riykl.baihongyu.com/

你可能感兴趣的文章
Oracle trunc()函数的用法
查看>>
col-md-*和col-sm-*
查看>>
前端开发大众手册(包括工具、网址、经验等)
查看>>
IOC容器
查看>>
“利益相关者”课堂讨论电子版
查看>>
意见总结
查看>>
servlet增删改查
查看>>
php - 中文字符串分割
查看>>
图解HTTP
查看>>
HTML - form (转)
查看>>
浅析C#深拷贝与浅拷贝 (转)
查看>>
3226. [SDOI2008]校门外的区间【线段树】
查看>>
如何解决jersey框架中以json格式返回数组,当数组中元素一个时json格式不对
查看>>
HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
查看>>
Kafka参数调优实战,看这篇文章就够了!
查看>>
delphi 把一个表的内容转到另一个表暂存时出错的解决方法。
查看>>
JavaScript 操作cookie
查看>>
BeanUtils.copyProperties() 用法
查看>>
微信公众平台开发 - 基础篇
查看>>
WinForm更新文件
查看>>