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 相关文章推荐
JQuery团队打造的javascript单元测试工具QUnit介绍
Feb 26 Javascript
jquery调取json数据实现省市级联的方法
Jan 29 Javascript
Javascript进制转换实例分析
May 14 Javascript
简单的jQuery入门指引
Jul 28 Javascript
Flow之一个新的Javascript静态类型检查器
Dec 21 Javascript
js实现图片无缝滚动特效
Mar 19 Javascript
Bootstrap table 定制提示语的加载过程
Feb 20 Javascript
Vue form 表单提交+ajax异步请求+分页效果
Apr 22 Javascript
js实现首屏延迟加载实现方法 js实现多屏单张图片延迟加载效果
Jul 17 Javascript
Vue项目中添加锁屏功能实现思路
Jun 29 Javascript
微信小程序实现循环动画效果
Jul 16 Javascript
layui table 列宽百分比显示的实现方法
Sep 28 Javascript
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中模拟POST传递数据的两种方法分享
2011/09/16 PHP
Eclipse的PHP插件PHPEclipse安装和使用
2014/07/20 PHP
php对关联数组循环遍历的实现方法
2015/03/13 PHP
thinkPHP模型初始化实例分析
2015/12/03 PHP
yum命令安装php7和相关扩展
2016/07/04 PHP
PHP设计模式之原型模式定义与用法详解
2018/04/03 PHP
javascript当中的代码嗅探扩展原生对象和原型(prototype)
2013/01/11 Javascript
jquery图片不完全按比例自动缩小的简单代码
2013/07/29 Javascript
jQuery的css()方法用法实例
2014/12/24 Javascript
jQuery给多个不同元素添加class样式的方法
2015/03/26 Javascript
深入解读JavaScript中的Iterator和for-of循环
2015/07/28 Javascript
Vue.js开发环境快速搭建教程
2017/03/17 Javascript
JavaScript 过滤关键字
2017/03/20 Javascript
JavaScript 上传文件(psd,压缩包等),图片,视频的实现方法
2017/06/19 Javascript
Angular5中调用第三方库及jQuery的添加的方法
2018/06/07 jQuery
vue 开发企业微信整合案例分析
2019/12/02 Javascript
vue-cli4.x创建企业级项目的方法步骤
2020/06/18 Javascript
关于JavaScript中异步/等待的用法与理解
2020/11/18 Javascript
python实现哈希表
2014/02/07 Python
举例讲解Python中装饰器的用法
2015/04/27 Python
Python如何实现守护进程的方法示例
2017/02/08 Python
基于Linux系统中python matplotlib画图的中文显示问题的解决方法
2017/06/15 Python
Python爬取qq空间说说的实例代码
2018/08/17 Python
详解python statistics模块及函数用法
2019/10/27 Python
python学生信息管理系统实现代码
2019/12/17 Python
Python利用命名空间解析XML文档
2020/08/10 Python
夜大毕业自我鉴定
2013/10/11 职场文书
单位实习证明怎么写
2014/01/17 职场文书
幼儿园教师岗位职责
2014/03/17 职场文书
小学评语大全
2014/04/22 职场文书
单位工作证明书格式
2014/10/04 职场文书
Pytest实现setup和teardown的详细使用详解
2021/04/17 Python
忆童年!用Python实现愤怒的小鸟游戏
2021/06/07 Python
Java基于字符界面的简易收银台
2021/06/26 Java/Android
Python中Numpy和Matplotlib的基本使用指南
2021/11/02 Python
分析SQL窗口函数之取值窗口函数
2022/04/21 Oracle