JavaScript实现的链表数据结构实例


Posted in Javascript onApril 02, 2015

此例是javascript来建立链表。。
并对此进行了排序。。

还可以在GenericList一般链表上进行扩展。
实现各种排序及增,删,改结点。。

function Node(){

  this.data=null;

  this.next=null;

}
function GenericList(){

  this.head=null;

  this.current=null; 

  //打出所有的链表结点

  this.print= function(){

  this.current=this.head;

   while(this.current!=null){

      alert(this.current.data);

      this.current=this.current.next;

    }

  

  },

  //建立链表

  this.addHead =function(t){

      

  

      var node=new Node();

      node.data=t;

      node.next=this.head;

      this.head=node;

 

  }

  

 
}


function SortList(){

//冒泡排序链表 

this.BubbleSort=function()

   {

     if(this.head==null||this.head.next==null)

     {

        return ;

     }

    var swapped;

    do{

    

     this.previous=null;

     this.current=this.head;
     var swapped=false;

     while(this.current.next!=null)

      {

       

       if(this.current.data-this.current.next.data>0)

        {

       

        var tmp=this.current.next;

        this.current.next=this.current.next.next;

        tmp.next=this.current;

        if(this.previous==null)

            {

               this.head=tmp;

            }

         else

           {

               this.previous.next=tmp;

           }

          this.previous=tmp;

          swapped=true;

          

       

       }

       else

        {

        

        this.previous=this.current;

        this.current=this.current.next;

        

        }

     

     }

     

      

    

    }while(swapped);

   

   }
}
SortList.prototype=new GenericList();


(function Main(){

 var sl=new  SortList();

 for(var i=0;i<arguments.length;i++)

 {sl.addHead(arguments[i]);

 }

 alert("未排序的链表");

 sl.print();

 sl.BubbleSort();

  alert("已排序的链表  从小到大");

 sl.print();
})("1","2","3","4")
Javascript 相关文章推荐
js 巧妙去除数组中的重复项
Jan 25 Javascript
javascript与CSS复习(三)
Jun 29 Javascript
容易造成JavaScript内存泄露几个方面
Sep 04 Javascript
jQuery采用连缀写法实现的折叠菜单效果
Sep 18 Javascript
jQuery学习心得总结(必看篇)
Jun 10 Javascript
JS常见简单正则表达式验证功能小结【手机,地址,企业税号,金额,身份证等】
Jan 22 Javascript
vue2 router 动态传参,多个参数的实例
Nov 10 Javascript
vue input输入框模糊查询的示例代码
May 22 Javascript
详解webpack的proxyTable无效的解决方案
Jun 15 Javascript
解决jquery有正确返回值但不执行success函数的问题
Aug 20 jQuery
基于Vue全局组件与局部组件的区别说明
Aug 11 Javascript
Vue 事件的$event参数=事件的值案例
Jan 29 Vue.js
JavaScript实现的字符串replaceAll函数代码分享
Apr 02 #Javascript
通过JS判断联网类型和连接状态的实现代码
Apr 01 #Javascript
Javascript中数组方法汇总(推荐)
Apr 01 #Javascript
javascript闭包的理解
Apr 01 #Javascript
JavaScript数据类型之基本类型和引用类型的值
Apr 01 #Javascript
JavaScript之Object类型介绍
Apr 01 #Javascript
JS修改iframe页面背景颜色的方法
Apr 01 #Javascript
You might like
php SQL之where语句生成器
2009/03/24 PHP
php获取参数的几种方法总结
2014/02/18 PHP
深入理解PHP原理之执行周期分析
2016/06/01 PHP
PHP入门教程之自定义函数用法详解(创建,调用,变量,参数,返回值等)
2016/09/11 PHP
微信封装的调用微信签名包的类库
2017/06/08 PHP
解决thinkPHP 5 nginx 部署时,只跳转首页的问题
2019/10/16 PHP
PHP使用PhpSpreadsheet操作Excel实例详解
2020/03/26 PHP
基于Jquery的文字自动截取(提供源代码)
2011/08/09 Javascript
全面理解面向对象的 JavaScript(来自ibm)
2013/11/10 Javascript
js实现格式化金额,字符,时间的方法
2015/02/26 Javascript
js实现简单排列组合的方法
2016/01/27 Javascript
AngularJS 基础ng-class-even指令用法
2016/08/01 Javascript
Node.js的基本知识简单汇总
2016/09/19 Javascript
JS中使用media实现响应式布局
2017/08/04 Javascript
vue动画效果实现方法示例
2019/03/18 Javascript
Vue + Scss 动态切换主题颜色实现换肤的示例代码
2020/04/27 Javascript
[02:28]DOTA2英雄基础教程 狼人
2013/12/23 DOTA
pycharm 使用心得(九)解决No Python interpreter selected的问题
2014/06/06 Python
python循环监控远程端口的方法
2015/03/14 Python
进一步理解Python中的函数编程
2015/04/13 Python
Python单元测试框架unittest使用方法讲解
2015/04/13 Python
Python使用cookielib模块操作cookie的实例教程
2016/07/12 Python
Python中的连接符(+、+=)示例详解
2017/01/13 Python
sublime python3 输入换行不结束的方法
2018/04/19 Python
Python实现查找字符串数组最长公共前缀示例
2019/03/27 Python
python实现LBP方法提取图像纹理特征实现分类的步骤
2019/07/11 Python
python 发送json数据操作实例分析
2019/10/15 Python
Python基于pandas爬取网页表格数据
2020/05/11 Python
基于python requests selenium爬取excel vba过程解析
2020/08/12 Python
移动端rem布局的两种实现方法
2018/01/03 HTML / CSS
html5 web本地存储将取代我们的cookie
2012/12/26 HTML / CSS
HTML5 在canvas中绘制文本附效果图
2014/06/23 HTML / CSS
北美领先的牛仔品牌:Buffalo David Bitton
2017/05/22 全球购物
关键字throw与throws的用法差异
2016/11/22 面试题
中学校庆方案
2014/03/17 职场文书
2019年大学生职业生涯规划书
2019/03/25 职场文书