JS数据结构代码

2018-07-19  本文已影响0人  尼古拉苏

队列

function Queue(){
    var self=this;
    self.dataStore = [];

    var front = self.dataStore[0];
    var back= self.dataStore[self.dataStore.length-1];
    var count=self.dataStore.length;

    var update=function(){
        front = self.dataStore[0];
        self.front=front;
        back= self.dataStore[self.dataStore.length-1];
        self.back=back;
        count=self.dataStore.length;
        self.count=count;
    }

    var push=function(value){
        self.dataStore.push(value);
        update();
    }

    var shift=function(){
        var obj=self.dataStore.shift();
        update();
        return obj;
    }

    var toString=function(){
        var retStr = "";
        for (var i=0; i<self.dataStore.length; ++i) {
            retStr += self.dataStore[i] + " "
        }
        return retStr;
    }

    var isEmpty=function(){
        if(self.dataStore.length == 0){
            return true;
        }else{
            return false;
        }
    }

    self.push = push;
    self.shift = shift;
    self.front = front;
    self.back = back;
    self.toString = toString;
    self.isEmpty = isEmpty;
    self.count = count;
}

单链表

function List(){
    var self=this;
    var head=null;
    var length=0;

    function Node(element){
        this.element=element;
        this.next=null;
    }

    this.show_all=function(){
        console.log(head);
    }

    this.size=function(){
        return length;
    }

    this.find_last=function(){
        var last,next;
        if (head!=null) {
            next=head;
            while (next.next) {
                next=next.next;
            }
            last=next;
        }else{
            last=null;
        }
        return last;
    }

    this.find_ele=function(element){
        var last,next;
        var result=[];
        if (head!=null) {
            next=head;
            if (next.element==element) {
                last=next;
                result.push(last);
            }
            while (next.next) {
                next=next.next;
                if (next.element==element) {
                    last=next;
                    result.push(last);
                }
            }
        }else{
            last=null;
        }
        return result;
    }

    this.find_id=function(index){
        var last,next;
        var number=0;
        if (index>length-1) {
            return null;
        }
        if (head!=null) {
            next=head;
            while (next.next) {
                if (index===number) {
                    last=next;
                    break;
                }
                number=number+1
                next=next.next;
            }
            last=next;
        }else{
            last=null;
        }
        return last;
    }

    this.delete=function(element){
        var current;
        if (length==0) {
            self.show_all();;
            return;
        }
        if (head.element==element) {
            head=head.next;
            length=length-1;
            self.show_all();;
            return;
        }
        current=head;
        while (current) {
            if (current.next&&current.next.element==element) {
                current.next=current.next.next;
                length=length-1;
            }
            current=current.next;
        }
        self.show_all();;
        return;
    }

    this.add=function(element){
        var node=new Node(element);
        var next;
        if (head==null) {
            head=node;
            length=1;
        }else{
            next=head;
            while (next.next) {
                next=next.next;
            }
            next.next=node;
            length=length+1;
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读