JavaScript之链表操作
来源:互联网 发布:y系列电动机技术数据 编辑:程序博客网 时间:2024/06/05 03:26
单链表
//Node类和LList类function Node(element){ this.element=element; this.next=null;}function LList(){ this.head=new Node("head"); this.find=find; this.insert=insert; this.display=display; this.findPrevious=findPrevious; this.remove=remove;}//找到前一个//即找到当前的下一个元素的节点是否等于item,若相等则返回当前元素,此时的当前元素相对于item即为找到其前一个元素。function findPrevious(item){ var currNode=this.head; while(!(currNode.next==null)&&(currNode.next.element!=item)){ currNode=currNode.next; } return currNode;}//显示function display(){ var currNode=this.head; while(!(currNode.next==null)){ print(currNode.next.element); currNode=currNode.next;}//查找function find(item){ var currNode=this.head; while(currNode.element!=item){ currNode=currNode.next; } return currNode;}//插入function insert(newElement,item){ var newNode=new Node(newElement); var current=this.find(item); newNode.next=current.next; current.next=newNode;}//移除function remove(item){ var preNode=this.findPrevious(item); if(!(preNode.next==null)){ preNode.next=preNode.next.next; }}
双向链表
为节点添加一个前驱和一个后继。
//Node类构造函数function Node(element){ this.element=element; this.next=null; this.previous=null;}//display()function display(){ var currNode=this.head; while(!(currNode.next==null)){ print(currNode.next.element); currNode=currNode.next; }}//findfunction find(item){ var currNode=this.head; while(currNode.next!=item){ currNode=currNode.next; } return currNode;}//insert插入(在当前节点的下一个插入新节点)//插入节点时候,先将新节点的next连指向当前的节点的下一个,然后再将新节点的previous连接到当前节点,让当前节点的next指向新节点。function insert(newElement,item){ var newNode=new Node(newElement); var current=this.find(item); newNode.next=current.next; newNode.previous=current; current.next=newNode;}//remove移除//移除的时候,将当前节点的previous的next等于当前节点的next.将当前节点next的previous等于当前节点的previous.然后删除currNode节点。function remove(item){ var currNode=this.find(item); if(!(currNode.next==null)){ currNode.previous.next=currNode.next; currNode.next.previous=currNode.previous; currNode.next=null; currNode.previous=null; }}//找到双向链表中的最后一个节点findLastfunction findLast(){ var currNode=this.head; while(!(currNode.next==null)){ currNode=currNode.next; } return currNode;}//反序显示双向链表中的元素function disReverse(){ var currNode=this.head; currNode=this.findLast(); while(!(currNode.next==null)){ print(currNode.element); currNode=currNode.previous; }}
循环链表
在创建循环链表中,其头节点的next属性指向它本身,即:
head.next=head;
因此,以原来的方式进行会在循环链表中陷入死循环,while循环条件需要修改,需要检查头节点,当循环到头节点时退出循环。
//node类和LList类//Node类和LList类function Node(element){ this.element=element; this.next=null;}function LList(){ this.head=new Node("head"); this.head.next=this.head; this.find=find; this.insert=insert; this.display=display; this.findPrevious=findPrevious; this.remove=remove;}//显示function display(){ var currNode=this.head; while(!(currNode.next==null)&&!(currNode.next.element=="head")){ print(currNode.next.element); currNode=currNode.next;}//查找function find(item){ var currNode=this.head; while(currNode.element!=item && currNode.next.element!="head"){ currNode=currNode.next; } return currNode;}//插入function insert(newElement,item){ var newNode=new Node(newElement); var current=this.find(item); newNode.next=current.next; current.next=newNode;}
0 0
- JavaScript之链表操作
- javascript链表操作
- javascript之数组操作
- javascript之数组操作
- javascript之数组操作
- javascript之数组操作
- javascript之数组操作
- javascript之数组操作
- javascript之数组操作
- javascript之数组操作
- javascript之数组操作
- javascript之数组操作
- javascript之数组操作
- javascript之数组操作
- javascript之数组操作
- javascript之数组操作
- javascript之数组操作
- Javascript之文件操作
- Android Studio系列教程四--Gradle基础
- 设计模式——策略模式
- Android开发Eclipse常用快捷键
- 词语相似度计算:5、训练各种相似度模型(LR,RF,NMF,LDA等)【待续】
- Python 数据结构与算法——归并排序
- JavaScript之链表操作
- 168. Excel Sheet Column Title
- 【bzoj2820】【YY的gcd】【莫比乌斯反演】
- 了解Boilerplate - 学习笔记 一
- 在编译时检查sizeof
- Android Studio快捷键
- Smobiler基于.NET框架开发移动应用内部系统—周报日志等功能(开发日志四)
- android——长期在后台又进app导致的crash
- -UTC时间及时区获取