JS實現單鏈表

來源:互聯網
上載者:User
                <script>             //Student節點            function Student(no,name){                 this.id=no;                 this.name=name;                 this.scores={chinese:0,math:0,english:0};             }                        //鏈表            function List(){                 this.head=null;                 this.end=null;                 this.curr=null;             }             //添加節點            List.prototype.add=function(o){                 var tem={ob:o,next:null};                                if(this.head){ //鏈表不為空白                     this.end.next=tem;                     this.end=tem;                 }                else{ //鏈表為空白                    this.head=tem;                     this.end=tem;                     this.curr=tem;                 }             }             //刪除第inde個節點            List.prototype.del=function(inde){                 var n=this.head;                 for(var i=0;i<inde;i++){                     n=n.next;                 }                 n.next=n.next.next?n.next.next:null;             }             //下一個節點            List.prototype.next=function(){                 var te=null;                 if(this.curr){                     te=this.curr.ob; this.curr=this.curr.next;                }                 return te;             }             //是否存在下一個節點            List.prototype.hasnext=function(){                 if(this.curr.ob!=null)                    return true;                 return false;             }                         var list=new List();                 for(var i=0;i<1000;i++){                 list.add(new Student(i,'name'+i));             }             var i=0;             while(list.hasnext()){                 document.writeln(list.next().name);                 if(i==10){                    document.writeln('<br/>');                     i=0;                }                 i++;             }         </script>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.