js不能取地址
想实现一个简单的连表该如何实现了
data  为  "1"  "2"  "3" "4" 等等等等
next  为  ??????
大虾们指教下吧!~

解决方案 »

  1.   

    链表?List?lz 想实现啥效果?!
      

  2.   


    <html>
      <head>
    <title>Linked List Example</title>
        <script language="javascript" type="text/javascript" src="linked-list.js"></script>
      </head>
      <body>
        <h2>Linked List Example</h2>
        <hr />
        <p>This example shows the usage of the linked list implementation.</p>
        <script>//test creating the linked list
    var list = new LinkedList;//test the add method
    list.add("red");
    list.add("orange");
    list.add("yellow");
    list.add("green");
    list.add("blue");
    list.add("indigo");
    list.add("violet");//test the toString() method
    document.write("<p><b>Original List: </b><br />" + list + "</p>");//test the item() method
    document.write("<p><b>Value of node at position 2: </b><br />" + list.item(2) + "</p>");//test the remove() method
    list.remove(2);
    document.write("<p><b>After removing node at position 2: </b><br />" + list + "</p>");list.remove(0);
    document.write("<p><b>After removing node at position 0: </b><br />" + list + "</p>");//test the toArray() method
    var array = list.toArray();document.write("<p><b>New Array from toArray() method: </b><br />" + array + "</p>");//prove that the Array is actually a JavaScript Array object by sorting it
    array.sort();document.write("<p><b>Sorted Array from toArray() method: </b><br />" + array + "</p>");//try emptying the list
    while (list.size()){
        list.remove(0);
    }document.write("<p><b>List after removing all: </b><br />" + list + "</p>");
       </script>
      </body>
    </html>
    linked-list.js/*
     * Linked List implementation in JavaScript
     * Copyright (c) 2009 Nicholas C. Zakas
     * See LICENSE for details on license.
     *///=================================================================
    // LinkedListNode Implementation
    //=================================================================//-----------------------------------------------------------------
    // Class LinkedListNode
    //-----------------------------------------------------------------
    // Author(s)
    //  Nicholas C. Zakas (NCZ), 9/5/02
    //
    // Description
    //  A linked list data node.
    //
    // Arguments
    //  vData (Variant) - the data to store in the node.
    //-----------------------------------------------------------------
    function LinkedListNode(vData) {
        this.data = vData;      //the data for this node
        this.next = null;       //pointer to next node in the list
    }//=================================================================
    // LinkedList Implementation
    //=================================================================/**
     * A linked list implementation in JavaScript.
     * @class LinkedList
     * @constructor
     */
    function LinkedList() {    /**
         * The number of items in the list.
         * @property _length
         * @type int
         * @private
         */
        this._length = 0;
        
        /**
         * Pointer to first item in the list.
         * @property _list
         * @type Object
         * @private
         */
        this._list = null;
    }LinkedList.prototype = {    //restore constructor
        constructor: LinkedList,
        
        /**
         * Appends some data to the end of the list. This method traverses
         * the existing list and places the value at the end in a new item.
         * @param {variant} data The data to add to the list.
         * @return {Void}
         * @method add
         */
        add: function (data){
        
            //create a new item object, place data in
            var item = { 
                    data: data, 
                    next: null 
                },
                
                //used to traverse the structure
                current,
                previous;
        
            //special case: no items in the list yet
            if (this._list === null){
                this._list = item;
            } else {
                previous = this._list;
                current = this._list.next;
                
                while(current){
                    previous = current;
                    current = current.next;
                }
               
                previous.next = item;            
            }
            
            //don't forget to update the count
            this._length++;
        
        },
        
        /**
         * Retrieves the data in the given position in the list.
         * @param {int} index The zero-based index of the item whose value 
         *      should be returned.
         * @return {variant} The value in the "data" portion of the given item
         *      or null if the item doesn't exist.
         * @method item
         */
        item: function(index){
        
            //check for out-of-bounds values
            if (index > -1 && index < this._length){
                var current = this._list,
                    i = 0;
                    
                while(i++ < index){
                    current = current.next;            
                }
            
                return current.data;
            } else {
                return null;
            }
        },
        
        /**
         * Removes the item from the given location in the list.
         * @param {int} index The zero-based index of the item to remove.
         * @return {variant} The data in the given position in the list or null if
         *      the item doesn't exist.
         * @method remove
         */
        remove: function(index){
        
            //check for out-of-bounds values
            if (index > -1 && index < this._length){
            
                var current = this._list,
                    previous,
                    i = 0;
                    
                //special case: removing first item
                if (index === 0){
                    this._list = current.next;
                } else {
            
                    //find the right location
                    while(i++ < index){
                        previous = current;
                        current = current.next;            
                    }
                
                    //skip over the item to remove
                    previous.next = current.next;
                }
            
                //decrement the length
                this._length--;
            
                //return the value
                return current.data;            
            
            } else {
                return null;
            }
        
        },
        
        /**
         * Returns the number of items in the list.
         * @return {int} The number of items in the list.
         * @method size
         */
        size: function(){
            return this._length;
        },
        
        /**
         * Converts the list into an array.
         * @return {Array} An array containing all of the data in the list.
         * @method toArray
         */
        toArray: function(){
            var result = [],
                current = this._list;
            
            while(current){
                result.push(current.data);
                current = current.next;
            }
            
            return result;
        },
        
        /**
         * Converts the list into a string representation.
         * @return {String} A string representation of the list.
         * @method toString
         */
        toString: function(){
            return this.toArray().toString();
        }
    };
      

  3.   

    to 1 2 3楼
    就是用js实现连表 连表不是 data  next的形式吗
    data是 "1"   "2"   "3"   问一下next该如何写to 4楼  good!~
      

  4.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>XX</title>
    <style type="text/css">
    div {
    height:30px ; width:auto;font-size:14px;
    padding-left:100px;
    }
    </style>
    <body >
    <script>
    function LinkedList(){
    this._length = 0;
    this._head   = null;  //永远保存第一个节点
    };LinkedList.prototype = {
    constructor : LinkedList,
    add : function(data){
    var item = {
    head : data,
    next : null
    };
    if(this._head == null){
    this._head = item;
    }else{
    this.find(this._length-1).next = item ;
    }
    this._length++;
    },
    find : function(index){
    if(index < 0||index > this._length-1)return;
    var item = this._head,i = 0;
        while(i++<index)
    {
    item = item.next;
    }
    return item;
    },
    insert : function(data,index){
    if(index < 0||index > this._length-1)return;
        var item = {
    head : data,
    next : null
    };
    if(index==0)
    {
    item.next  = this._head;
    this._head = item;
    }
    else if(index == this._length - 1)
    {
    this.find(this._length-1).next = item;
    }
    else
    {   
        item.next = this.find(index);
    this.find(index-1).next = item;
    }
    this._length++;
    return item;
    },
    remove :function(index){
    if(index < 0||index > this._length-1)return;
    var item = this.find(index);
    if(index==0)
    {
    this._head = this.find(1);
    }
    else if(index == this._length-1)
    {
        this.find(this._length-2).next = null;
    }
    else
    {
    this.find(index-1).next = item.next;
    }
    this._length--;
    return item;
    },
    toArray : function(){
    var arrs = [],item = this._head;
    while(item){
    arrs.push(item.head);
    item = item.next;
    }
    return arrs ; 
    }
    };
    var list = null,ss=null;
    window.onload = function(){
    ss=document.getElementById("ss");
    list = new LinkedList();
    list.add("111");
    list.add("222");
    list.add("333");
    list.add("444");
    list.add("555");
    list.add("666");
    }function showlength(){
    alert(list._length);
    };function show(){
        var str = "";
    for(var i=0,l=list._length;i<l;i++)
    {
    str = str+"第"+i+"组数据为:"+list.find(i).head+"<br>";
    }
    ss.innerHTML = str;
    }function insertdata(arg){
    switch(arg){
    case 1 :
    list.insert("toutoutou",0)
    show();
    break ;
    case 2 :
    list.insert("zhongzhongzhong",parseInt(list._length/2+1))
    show();
    break;
    case 3 :
    list.insert("weiweiwei",list._length-1);
    show();
    break;
    }
    }function removedata(){
    list.remove(5);
    show();
    }function toArraydata(){
    ss.innerHTML = list.toArray();
    }</script>
    <div id="ss" style="height:450px ; width:700px; border:1px solid #333333; margin-left:80px; font-size:14px; "></div>
    <div>
        显示链表的长度:<input value="sssssss" type="button"  onclick="showlength()"/>
    </div>
    <div>
        显示连表里面的数据:<input value="sssssss" type="button"  onclick="show()"/>
    </div>
    <div>
        插入新的数据:<input value="表头插入" type="button"  onclick="insertdata(1)"/> 
        <input value="中间插入" type="button"  onclick="insertdata(2)"/>
         
        <input value="尾部插入" type="button"  onclick="insertdata(3)"/>
    </div>
    <div>
        删除数据:<input value="sssssss" type="button"  onclick="removedata()"/>
    </div>
    <div>
        转成数组的形式:<input value="sssssss" type="button"  onclick="toArraydata()"/>
    </div>
    </body>
    </html>
      

  5.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>editor</title>
    <style type="text/css">
    div {
        height:30px ; width:auto;font-size:14px;
        padding-left:100px;
    }
    </style>
    </head><body>
    <body >
    <script>
    function LinkedList(){
        this._length = 0;
        this._head   = null;  //永远保存第一个节点
    };LinkedList.prototype = {
        constructor : LinkedList,
        add : function(data){
            var item = {
                head : data,
                next : null
            };        
            if(this._head == null){
                this._head = item;
            }else{
                this.find(this._length-1).next = item ;
            }
            this._length++;
        },
        find : function(index){
            if(index < 0||index > this._length-1)return;
            var item = this._head,i = 0;
            while(i++<index)
            {
                item = item.next;
            }
            return item;
        },
        insert : function(data,index){
            if(index < 0||index > this._length)return;
            var item = {
                head : data,
                next : null
            };
    this.find(index)&&(item.next=this.find(index));
    this.find(index-1)&&(this.find(index-1).next = item);
    index==0&&(this._head = item);
            this._length++;
            return item;
        },
        remove :function(index){
            if(index < 0||index > this._length-1)return;
            var item = this.find(index);
    index==0&&(this._head = this.find(1));
    this.find(index+1)&&this.find(index-1)&&(this.find(index-1).next = item.next);
            this._length--;
            return item;
        },
        toArray : function(){
            var arrs = [],item = this._head;
            while(item){
                arrs.push(item.head);
                item = item.next;
            }
            return arrs ; 
        }        
    };
    var list = null,ss=null;
    window.onload = function(){
        ss=document.getElementById("ss");
        list = new LinkedList();
        list.add("111");
        list.add("222");
        list.add("333");
        list.add("444");
        list.add("555");
        list.add("666");
    }function showlength(){
        alert(list._length);
    };function show(){
        var str = "";
        for(var i=0,l=list._length;i<l;i++)
        {
            str = str+"第"+i+"组数据为:"+list.find(i).head+"<br>";
        }
        ss.innerHTML = str;
    }function insertdata(arg){
        switch(arg){
            case 1 :
            list.insert("toutoutou",0)
            show();
            break ;
            case 2 :
            list.insert("zhongzhongzhong",parseInt(list._length/2+1))
            show();
            break;
            case 3 :
            list.insert("weiweiwei",list._length);
            show();
            break;
        }
    }function removedata(){
    var n = parseInt(document.getElementById('bb').value)
        list.remove(n);
        show();
    }function toArraydata(){
        ss.innerHTML = list.toArray();
    }</script>
    <div id="ss" style="height:450px ; width:700px; border:1px solid #333333; margin-left:80px; font-size:14px; "></div>
    <div>
        显示链表的长度:<input value="sssssss" type="button"  onclick="showlength()"/>
    </div>
    <div>
        显示连表里面的数据:<input value="sssssss" type="button"  onclick="show()"/>
    </div>
    <div>
        插入新的数据:<input value="表头插入" type="button"  onclick="insertdata(1)"/> 
        <input value="中间插入" type="button"  onclick="insertdata(2)"/>
         
        <input value="尾部插入" type="button"  onclick="insertdata(3)"/>
    </div>
    <div>
        删除数据:<input value="sssssss" type="button"  onclick="removedata()"/> 
        <input id="bb" type="text" />
    </div>
    <div>
        转成数组的形式:<input value="sssssss" type="button"  onclick="toArraydata()"/>
    </div>
    </body>
    </body>
    </html>