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 相关文章推荐
javascript:void(0)的真正含义实例分析
Aug 20 Javascript
封装html的select标签的js操作实例
Jul 02 Javascript
js导入导出excel(实例代码)
Nov 25 Javascript
js window.open弹出新的网页窗口
Jan 16 Javascript
javascript生成随机大小写字母的方法
Feb 20 Javascript
php+js实现倒计时功能
Jun 02 Javascript
深入剖析JavaScript中的函数currying柯里化
Apr 29 Javascript
Bootstrap3 input输入框插入glyphicon图标的方法
May 16 Javascript
jQuery EasyUI tree 使用拖拽时遇到的错误小结
Oct 10 Javascript
vue.js实现的经典计算器/科学计算器功能示例
Jul 11 Javascript
分享一个vue项目“脚手架”项目的实现步骤
May 26 Javascript
vue 实现图片懒加载功能
Dec 31 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 输出简单动态WAP页面
2009/06/09 PHP
语义化 H1 标签
2008/01/14 Javascript
Javascript 构造函数,公有,私有特权和静态成员定义方法
2009/11/30 Javascript
JavaScript Distilled 基础知识与函数
2010/04/07 Javascript
Js(JavaScript)中,弹出是或否的选择框示例(confirm用法的实例分析)
2013/07/09 Javascript
改变隐藏的input中value的值代码
2013/12/30 Javascript
IE 下Enter提交表单存在重复提交问题的解决方法
2014/05/04 Javascript
jQuery修改CSS伪元素属性的方法
2014/07/30 Javascript
js脚本实现数据去重
2014/11/27 Javascript
轻松创建nodejs服务器(1):一个简单nodejs服务器例子
2014/12/18 NodeJs
超级简单实现JavaScript MVC 样式框架
2015/03/24 Javascript
js实现一个链接打开两个链接地址的方法
2015/05/12 Javascript
JS+Canvas绘制时钟效果
2020/08/20 Javascript
JavaScript中子对象访问父对象的方式详解
2016/09/01 Javascript
jQuery Chosen通用初始化
2017/03/07 Javascript
js实现三级联动效果(简单易懂)
2017/03/27 Javascript
BootStrap selectpicker后台动态绑定数据
2017/06/01 Javascript
vue项目中使用axios上传图片等文件操作
2017/11/02 Javascript
详解如何实现一个简单的Node.js脚手架
2017/12/04 Javascript
vue动画效果实现方法示例
2019/03/18 Javascript
浅谈layer弹出层按钮颜色修改方法
2019/09/11 Javascript
NumPy 数学函数及代数运算的实现代码
2018/07/18 Python
python创造虚拟环境方法总结
2019/03/04 Python
Pandas之Dropna滤除缺失数据的实现方法
2019/06/25 Python
python分数表示方式和写法
2019/06/26 Python
python speech模块的使用方法
2020/09/09 Python
详解Scrapy Redis入门实战
2020/11/18 Python
详解CSS3+JS完美实现放大镜模式
2020/12/03 HTML / CSS
Europcar德国:全球汽车租赁领域的领导者
2018/08/15 全球购物
优秀应届毕业生自荐信
2013/11/16 职场文书
留学推荐信怎么写
2014/01/25 职场文书
最新奶茶店创业计划书
2014/01/25 职场文书
班主任寄语2015
2015/02/26 职场文书
讲座通知范文
2015/04/23 职场文书
鸦片战争观后感
2015/06/09 职场文书
使用CSS实现一个搜索引擎的原理解析
2021/09/25 HTML / CSS