js数据结构与算法(列表和栈)

列表

function List(){
            this.listSize=0;//列表元素个数
            this.pos=0;//列表当前位置
            this.dataStore=[];//初始化一个空数组用来保存列表元素
            this.clear=clear;//清空列表中的所有元素
            this.find=find;//查找元素
            this.toString=toString;//返回列表字符串形式
            this.insert=insert;//在现有元素后插入新的元素
            this.append=append;//在列表元素末尾增加新元素
            this.remove=remove;//从列表中删除元素
            this.front=front;//从列表的当前位置移动到第一个元素
            this.end=end;//从列表的当前位置移动到最后的一个位置
            this.prev=prev;//将当前位置后移一位
            this.next=next;//将当前位置前移一位
            this.length=length;//列表包含元素的个数
            this.currPos=currPos;//返回列表当前位置
            this.moveTo=moveTo;//将当前位置移动到指定位置
            this.getElement=getElement//显示当前的元素
            this.contains=contains;//是否包含该元素
        }
        function append(element){
            this.dataStore[this.listSize++]=element;
        }
        function find(element){
            //此处++应该在前以便返回正确索引值
            for(var i=0;i<this.dataStore.length;++i){
                if(this.dataStore[i]==element){
                    return i;
                }
            }
            return -1; //没有找到返回-1
        }
        function remove(element){
            var foundAt=this.find(element);
            if(foundAt>-1){
                this.dataStore.slice(foundAt,1);//删除当前位置这个元素
                --this.listSize;
                return
            }
            return false;
        }
        function length(){
            return this.listSize;
        }
        function toString(){
            return this.dataStore;
        }
        function insert(element,after){
            var insertPos=this.find(after);
            if(insertPos>-1){
                this.dataStore.splice(insertPos+1,0,element);
                ++this.listSize;
                return true;
            }
            return false;
        }
        function clear(){
            delete this.dataStore;
            this.dataStore.length=0;//创建一个空数组
            this.listSize=this.pos=0;
            }        
        function contains(element){
            for(var i=0;i<this.dataStore.length;++i){
                if(this.dataStore[i]==element){
                    return true;
                }
            }
            return false;
        }
        //遍历数组
        function front(){
            this.pos=0;
        }
        function end(){
            this.pos=this.listSize-1;
        }
        function prev(){
            if(this.pos>0){
                --this.pos;
            }
        }
        function next(){
            if(this.pos<this.listSize){
                ++this.pos;
            }
        }
        function currPos(){
            return this.pos;
        }
        function moveTo(position){
            this.pos=position;
        }
        function getElement(){
            return this.dataStore[this.pos];
        }
        var names=new List();
        names.append("小红");
        names.append("小绿");
        names.append("小黄");
        // names.next();
        // names.next();
        // console.log(names.getElement());
        for(names.front();names.currPos()<names.length();names.next()){
            console.log(names.getElement());
        }

 function Stack(){
            this.dataStore=[];//保存栈元素
            this.top=0;//标记新元素插入的位置 栈内压入元素该变量变大 弹出元素 变量减小
            this.push=push;//入栈操作
            this.pop=pop;//出栈操作
            this.peek=peek;//返回栈顶元素
            this.clear=clear;//清空栈
            this.length=length;//栈的长度
        }
        //向栈中压入元素 同时让指针top+1 一定注意++
        function push(element){
            this.dataStore[this.top++]=element;
            console.log(this.dataStore);
        }
        //出栈操作
        function pop(){
            return this.dataStore[--this.top];
            //出栈需要马上减所以先--
        }
        //返回栈顶元素,变量top值减1返回不删除
        function peek(){
            //console.log(this.top);
            return this.dataStore[this.top-1];
        }
        //返回栈内元素个数
        function length(){
            return this.top;
        }
        //清空一个栈
        function clear(){
            this.top=0;
        }
        var s=new Stack();
        s.push("小红第一");
        s.push("小黄第二");
        s.push("小绿第三");
        s.push("小紫第四");
        console.log("栈的长度为",s.length());
        console.log("栈顶",s.peek());
        console.log("出栈",s.pop());
        // console.log("出栈",s.pop());
        // console.log("出栈",s.pop());
        // console.log("出栈",s.pop());
        // console.log(s);
        function isPalindrome(word){
            var s=new Stack();
            for(var i=0;i<word.length;i++){
                s.push(word[i]);
            }
            var rword="";
            console.log(s);
            while(s.length()>0){
                rword+=s.pop();
            }
            if(rword==word){
                return true;
            }else{
                return false;
            }
        }
        var word="racecar";
        console.log(isPalindrome(word));
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值