>  기사  >  웹 프론트엔드  >  js 단방향 연결 목록_javascript 기술의 구체적인 구현 예

js 단방향 연결 목록_javascript 기술의 구체적인 구현 예

WBOY
WBOY원래의
2016-05-16 17:31:531066검색

코드 복사 코드는 다음과 같습니다.

function linkNode(_key, _value)
{
///
/// 연결 리스트 클래스의 노드 클래스
///

this.Key = _key;
this.Value = _value;
this.next = null;
}
function Link()
{
/// < summary> ;
/// 연결 리스트 클래스 생성
///
this.root = new linkNode(null, null) //루트는 항상 빈 노드입니다
this.end = this.root;
}
Link.prototype =
{
개수: 0,
값: 함수(_key)
{
/// < ;요약>
                                                                   ~ | s>
var i = this.root;
while (Boolean(i = i.next))
{
if (i.Key == _key)
return i.Value;
}
},
추가: function (_key, _value)
{
/// < summary>
/// 연결 리스트 끝에 노드 추가
///
param>
///
/// value
///
/// < ;returns type = "Object"> i = i.next))
                                                                                                            ~ > node;
this.count ;
return _value;
},
    insert: function (_key, node)
    {
        ///
        /// 从链表类的某节点之后插入新节点node.
        ///

        ///
        /// 在键值等于_key的元素之后插入
        ///
        ///
        /// 要插入的元素
        ///
        var i = this.root;
        while (Boolean(i = i.next))
        {
            if (i.Key == _key)
            {
                var tmp = i.next;
                i.next = node;
                node.next = tmp;
                break;
            }
        }
    },
    insertBefore: function (_key, node)
    {
        ///
        /// 从链表类的某节点之后插入新节点node.
        ///

        ///
        /// 在键值等于_key的元素之后插入
        ///
        ///
        /// 要插入的元素 www.jb51.net
        ///
        var i = this.root;
        while (Boolean(i = i.next))
        {
            if (i.next.Key == _key)
            {
                var tmp = i.next;
                i.next = node;
                node.next = tmp;
                break;
            }
        }
    },
    remove: function (_key)
    {
        ///
        /// 从链表类中移除一个key
        ///

        ///
        /// key的值
        ///
        var i = this.root;
        do
        {
            if (i.next.Key == _key)
            {
                if (i.next.next == null)
                    this.end = i;
                i.next = i.next.next;

this.count--;
return;
}
} while (Boolean(i = i.next))
},
존재함: 함수(_ key)
{
///


/// 연결리스트에 키
가 있는지 확인 class type="String">
/// 키의 값
/// ;/returns>
var i = this.root;
while (Boolean(i = i.next))
if (i.Key == _key)
return true ;
return false;
},
RemoveAll: function ()
{
🎜> this.root = new linkNode(null, null);
this.end = this. root;
this.count = 0;
},
Obj2str: 함수 (o)
{
if (o == 정의되지 않음)
{
return "";
}
var r = [];
if (typeof o == "string")
              return """ o.replace(/(["\])/g, "\$1 ").replace(/(n)/g, "\n").replace(/(r)/g, "\r").replace(/(t)/g, "\t") """ ;
                                                                                      )
         > ]))
                                                                                      | ;
},
    getJSON: function ()
    {
        ///
        /// 转换成JSON字符串
        ///

        ///
        ///

        //内部方法,用于递归
        var me = this;
        var getChild = function (node)
        {
            var str = "";
            str += "{\"Key\":\"" + node.Key + "\",\"Value\":" + me.Obj2str(node.Value);
            if (node.next != null)
                str += ",\"next\":" + getChild(node.next);
            else
                str += ",\"next\":\"null\"";
            str += "}";
            return str;
        };
        var link = "{\"root\":{\"Key\":\"null\",\"Value\":\"null\",\"next\":";
        if (this.count == 0)//如果空表
        {
            return "{\"root\":{\"Key\":\"null\",\"Value\":\"null\",\"next\":\"null\"},\"end\":{\"Key\":\"null\",\"Value\":\"null\",\"next\":\"null\"},\"count\":\"0\"}";
        }
        link += getChild(this.root.next) + "}";
        //加上end
        link += ",\"end\":{\"Key\":\"" + this.end.Key + "\",\"Value\":" + me.Obj2str(this.end.Value) + ",\"next\":\"null\"";
        link += "},\"count\":\"" + this.count + "\"}";
        return link;
    },
    getArrayJSON: function ()
    {
        ///
        /// 转所有节点的value换成JSON字符串,数组格式
        ///

        ///
        ///

        var link = "{\"link\":[";
        var i = this.root;
        while (Boolean(i = i.next))
        {
            link += this.Obj2str(i.Value) + ",";
        }
        link = link.substr(0, link.length - 1);
        link += "]}";
        return link;
    },
    sort: function (fn)
    {
        ///
        /// 对链表进行排序
        ///

        ///
        /// 比较两个链表元素大小的方法,当返回真时,此方法的参数所指的节点将往下沉
        ///
        if (fn != null)
        {

            var i = this.root;
            while (Boolean(i = i.next))
           {
              var j = this.root;
                while (Boolean(j = j.next ))
                {
                  if (j.next != null)
                  {
                       if (fn.call(fn.call) this, j))
                       {
                         var Key = j.Key;
                          var Value = j.Value;
                        j.Key = j.next.Key;
                          j.Value = j .next.Value;
                           j.next.Key = 키;
j.next.Value = 값;
                       }
                 }
              }
               i;
            }

        }
        else
        {

        }
    }
};

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.