js 单链表
来源:互联网 发布:用友软件固定资产报废 编辑:程序博客网 时间:2024/06/16 20:48
用JavaScript只用过数组,没有写过别的数据结构,今天练习了一下一个简单的单链表。
var Node = function(value){ this.value = value; this.next = null; } var myList = function(){ this.head = new Node(null); //设头结点为空 this.insert = function(value){ //插入节点 var p = this.head; while(p.next!=null){ p = p.next; } p.next = new Node(value); } this.print = function(){ //打印节点 var p = this.head; while(p.next!=null){ p = p.next; document.write(p.value+" "); } } this.findNode = function(position){ //寻找某个位置的节点 if(position<0) return; var p = this.head; var i = 0; while(p.next!=null && i<position){ i++; p = p.next; } return p; //若position大于链表长度返回最后一个 } this.remove = function(position){ //移除第position个节点 this.findNode(position-1).next=this.findNode(position).next; } this.length=function(){ //链表长度 var len = 0; var p = this.head; while(p.next!=null){ len++; p = p.next; } document.write(len); } } var list = new myList(); list.insert("a"); list.insert("b"); list.insert("c"); list.insert("d"); list.print(); //a b c d list.remove(2); list.print(); //a c d list.length(); //3
0 0
- js 单链表
- js 实现的单链表
- JS模拟单链表
- js 单链表翻转
- JS实现单链表
- js
- js
- JS
- JS
- JS
- js
- js
- js
- js
- js
- JS
- js
- JS
- python之递归
- 乒乓球对打的机器人
- LinkedHashMap源码分析与LRU实现
- SNR
- 利用JDK自带工具构建一个简单的Java SOAP Web Service
- js 单链表
- Xcode中的Group和fold的区别
- Android SlidingMenu
- 如何实现编译时注入
- Linux下查找大文件并清理
- Ubuntu的VPN设置
- [sqlite3]_[初级]_[使用正则表达式REGEXP查询]
- C++ traits技术浅谈
- poj 2763 Housewife Wind(LCA经典题目 处理树链)