JavaScript实现的链表数据结构实例
此例是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实现数据结构:稀疏矩阵的十字链表存储表示
- JavaScript数据结构之链表的实现
- C语言数据结构实现链表去重的实例
- 数据结构:JavaScript实现链表
- 数据结构与C语言实现(一)——链表及两个实例
- javascript数据结构之双链表插入排序实例详解
- javascript数据结构之双链表插入排序实例详解
- JavaScript数据结构——链表的实现
- javascript实现可改变滚动方向的无缝滚动实例
- 数据结构Java实现03----单向链表的插入和删除
- javascript实现继承的简单实例
- 使用javascript的stack数据结构,实现进制转换
- JavaScript实现跨浏览器的添加及删除事件绑定函数实例
- java 实现双向链表(数据结构)
- 数据结构 链表的lua实现 仿照C++中list 实现
- JavaScript来实现打开链接页面的简单实例
- javascript小实例,多种方法实现数组去重问题
- JavaScript中实现无缝滚动、分享到侧边栏实例代码