Heim  >  Artikel  >  Web-Frontend  >  javascript写的一个链表实现代码_javascript技巧

javascript写的一个链表实现代码_javascript技巧

WBOY
WBOYOriginal
2016-05-16 18:43:211143Durchsuche

本来要用Array来保存数据的,没试过用JS来数据结构,就用JS来试试吧。
JS效率真的很低一个链表装1000个对象浏览器就提示运行缓慢了。
之前觉得AJAX3D挺用前景的,现在看来还没有流行就要夭折了。用delphi开发的游戏人们都觉得太慢了,何况用JS。
下面是我实现的一个链表:

复制代码 代码如下:

/*@author eric
*@mail shmilyhe@163.com
*blog.csdn.net/shmilyhe
*/
<script> <BR>function Student(no,name){ <BR>this.id=no; <BR>this.name=name; <BR>this.scores={chinese:0,math:0,english:0}; <BR>} <BR>function List(){ <BR>this.head=null; <BR>this.end=null; <BR>this.curr=null; <BR>} <BR>List.prototype.add=function(o){ <BR>var tem={ob:o,next:null}; <BR>if(this.head){ <BR>this.end.next=tem; <BR>this.end=tem; <BR>}else{ <BR>this.head=tem; <BR>this.end=tem; <BR>this.curr=tem; <BR>} <BR>} <BR>List.prototype.del=function(inde){ <BR>var n=this.head; <BR>for(var i=0;i<inde;i++){ <BR>n=n.next; <BR>} <BR>n.next=n.next.next?n.next.next:null; <BR>} <BR>List.prototype.next=function(){ <BR>var te=null; <BR>if(this.curr){ <BR>te=this.curr.ob; this.curr=this.curr.next;} <BR>return te; <BR>} <BR>List.prototype.hasnext=function(){ <BR>if(this.curr.ob!=null)return true; <BR>return false; <BR>} <BR>var list=new List(); <BR>for(var i=0;i<1000;i++){ <BR>list.add(new Student(i,'name'+i)); <BR>} <BR>var i=0; <BR>while(list.hasnext()){ <BR>document.writeln(list.next().name); <BR>if(i==10){document.writeln('<br/>'); i=0;} <BR>i++; <BR>} <BR></script>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn