面試題5:JS實現從尾到頭列印單鏈表

來源:互聯網
上載者:User

標籤:

單鏈表,在記憶體中所佔地址是不連續的。所以遍曆單鏈表時:需要從頭遍曆。而題目要求輸出的順序:從尾到頭。也就是說第一個遍曆到的節點最後一個輸出,而最後一個遍曆到的節點第一個輸出。這就是典型的“後進先出”,我們可以用棧來實現這種順序。

例題一共包含四個檔案。運行程式前提:項目安裝了nodejs

1.stack_list.js:實現了一個普通的棧。

/** * Created by ym-Wang on 2016/8/16. */function Stack(){    this.top = null;    this.size = 0;}Stack.prototype = {    initStack:function(){        return new Stack();    },    push:function(data){        var node = {            data:data,            next:null        };        node.next = this.top;        this.top = node;        this.size++;    },    pop:function(){        if(this.isStackEmpty()){            console.log("stack is Empty");            return false;        }        var out = this.top;        this.top = this.top.next;        if(this.size > 0){            this.size--;        }        return out.data;    },    clearStack:function(){        this.top = null;        this.size = 0;    },    isStackEmpty:function(){        return this.top == null?true:false;    }};function stackConstructor(){    return new Stack();};exports.stackConstructor = stackConstructor;

2.createNode.js:用於初始化節點

(function(){    "use strict";    function Node(element){        this.element = element;        this.next = null;    }    function nodeConstructor(element){        return new Node(element);    };    exports.nodeConstructor = nodeConstructor;})();

3.createList.js:實現一個單鏈表

(function(){    "use strict";    var node = require("./createNode.js");    function LinkedList(){        this._head = node.nodeConstructor("This is Head Node");        this._size = 0;    }    LinkedList.prototype = {        isEmpty:function(){            if(this._size == 0){                return true;            }else{                return false;            }        },        size:function(){            return this._size;        },        getHead:function(){            return this._head;        },        display:function(){            var currNode = this.getHead().next;            while(currNode){                console.log(currNode.element);                currNode = currNode.next;            }        },        remove:function(item){            if(item){                var preNode = this.findPre(item);                if(preNode == null){                    return;                }                if(preNode.next != null){                    preNode.next = preNode.next.next;                    this._size--;                }            }        },        add:function(item){            this.insert(item);        },        insert:function(newElement,item){            //在指定位置item後插入newElement節點,如果未找到item,則插入到鏈表結尾。            var newNode = node.nodeConstructor(newElement);            var finder = item?this.find(item):null;            if(!finder){                var last = this.findLast();                last.next = newNode;            }else{                newNode.next = finder.next;                finder.next = newNode;            }            this._size++;        },        findLast:function(){            //返回最後一個及節點            var currNode = this.getHead();            while(currNode.next){                currNode = currNode.next;            }            return currNode;        },        findPre:function(item){            //返回指定元素的上一個節點            var currNode = this.getHead();            while(currNode.next != null&&currNode.next.element !=item){                currNode = currNode.next;            }            return currNode;        },        find:function(item){            if(item == null){                return null;            }            var currNode = this.getHead();            while(currNode && currNode.element != item){                currNode = currNode.next;            }            return currNode;        }    };    exports.linkedList= new LinkedList();})();

4.desending.js:倒序輸出單鏈表

(function(){    var singleList = require("./createList.js");    var stack_list = require("./stack_list.js");    var list = singleList.linkedList;    var stack = stack_list.stackConstructor();    list.add(1);    list.add(12);    list.add(123);    list.add(1234);    var curNode = list.getHead();    while(curNode.next !== null){        stack.push(curNode.next.element);        curNode = curNode.next;    }    while(stack.size!=0){        var ele = stack.pop();        console.log(ele);    }})();

 注意:我的項目中這四個檔案是在同一目錄下的。如果不在同一目錄下,需要修改require的路徑參數。

 

面試題5:JS實現從尾到頭列印單鏈表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.