Interview questions 5:JS to print a single linked list from the end of the head

Source: Internet
Author: User

The single-linked list, in memory, occupies an address that is not contiguous. So when traversing a single linked list: You need to traverse from the beginning. The order in which the title requires output: From the end of the head. That is, the first node to traverse to the last output, and the last to traverse to the first output of the node. This is the typical "LIFO" and we can use stacks to implement this order.

The example contains four documents. Prerequisites for Running the program: Project installed Nodejs

1.stack_list.js: Implements a common stack.

/** * 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: For initializing nodes

(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: Implementing a single-linked list

(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) {//Inserts the Newelement node after the specified position of item and inserts to the end of the list if item is not found.            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 () {//returns the last and node var currnode = This.gethead ();            while (currnode.next) {currnode = Currnode.next;        } return Currnode;            }, Findpre:function (item) {//Returns the previous node of the specified element 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: Reverse output single-linked list

(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 (n);    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);    }}) ();

Note: The four files in my project are in the same directory. If you are not in the same directory, you need to modify the require path parameter.

Interview questions 5:JS to print a single linked list from the end of the head

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.