幽灵资源网 Design By www.bzswh.com
使用JavaScript封装单向链表:
1. 封装LinkList的类,用于表示我们的链表结构。
2. 在LinkList类中有一个Node类,用于封装每一个节点上的信息(data与next)。
3. 在链表中保存两个属性,一个是链表的长度,一个是链表中的第一个节点。
4.封装一些链表的常用方法:
- append(element):想列表尾部添加一个新的项;
- insert(position,element):向列表的特定位置插入一个新的项;
- get(position):获取对应位置的元素;
- indexOf(element):返回元素在链表中的索引,如果链表中没有该元素则返回-1;
- update(position,element):修改某个位置的元素;
- removeAt(postion):从列表的特定位置移除一项;
- remove(element):从列表中移除一项;
- isEmpty():如果链表中不包含任何元素,返回true,否则返回false;
- size():返回链表中包含元素的个数;
- toString():输出链表元素的值;
<script type="text/javascript"> function LinkList(){ /* 节点类 */ function Node(data){ this.data = data this.next = null } this.head = null this.length = 0 /* 追加方法 */ LinkList.prototype.append = function(data){ /* 创建新节点 */ var newNode = new Node(data) if(this.length === 0){ this.head = newNode }else{ /* 找到最后一个节点 */ var current = this.head while(current.next){ current = current.next } current.next = newNode } this.length += 1 } /* toString方法 */ LinkList.prototype.toString = function(){ var current = this.head var listString = "" while(current){ listString += current.data +" " current = current.next } return listString } /* insert方法 */ LinkList.prototype.insert = function(position,data){ /* 对position进行越界判断 */ if(position<0||position>this.length) return false var node = new Node(data) if(position == 0){ node.next = this.head this.head = node }else{ var index = 0 var current = this.head var previous = null while(index++ < position){ previous = current current = current.next } node.next = current previous.next = node } this.length += 1 return true } /* get方法 */ LinkList.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 } /* indexOf方法 */ LinkList.prototype.indexOf = function(data){ /* 定义变量 */ var current = this.head var index = 0 /* 开始查找 */ while(current){ if(current.data === data){ return index }else{ current = current.next index += 1 } } return -1 } /* update方法 */ LinkList.prototype.update = function(position,data){ /* 越界判断 */ if(position<0 || position >= this.length) return false var current = this.head var index = 0 while(index++ < position){ current = current.next } /* 修改data */ current.data = data return true } /* removeAt方法 */ LinkList.prototype.removeAt = function(position){ /* 越界判断 */ if(position<0 || position >= this.length) return null var current = this.head if(position === 0){ this.head = this.head.next }else{ var index = 0 var previous = null while(index++ < position){ previous = current current = current.next } previous.next = current.next } this.length -= 1 return current.data } /* remove */ LinkList.prototype.remove = function(data){ /* 根据data找位置 */ var position = this.indexOf(data) return this.removeAt(position) } LinkList.prototype.isEmpty = function(){ return this.length === 0 } LinkList.prototype.size = function(){ return this.length } } /* 测试 */ var list = new LinkList() list.append('a') list.append('b') list.append('c') console.log(list.toString()) /* a b c */ list.insert(3,'d') console.log(list.toString())/* a b c d */ console.log(list.get(2)) /* c */ console.log(list.indexOf('d')) /* 3 */ list.update(1,'bbb') console.log(list.toString()) /* a bbb c d */ console.log(list.removeAt(2)) /* c */ console.log(list.toString())/* a bbb d */ console.log(list.remove('a')) console.log(list.toString())/* bbb d */ console.log(list.isEmpty()) /* false */ console.log(list.size()) /* 2 */ </script>
以上就是JavaScript封装单向链表的示例代码的详细内容,更多关于JavaScript封装单向链表的资料请关注其它相关文章!
幽灵资源网 Design By www.bzswh.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
幽灵资源网 Design By www.bzswh.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。