面试题5:JS实现从尾到头打印单链表
2016-09-17 10:29
176 查看
单链表,在内存中所占地址是不连续的。所以遍历单链表时:需要从头遍历。而题目要求输出的顺序:从尾到头。也就是说第一个遍历到的节点最后一个输出,而最后一个遍历到的节点第一个输出。这就是典型的“后进先出”,我们可以用栈来实现这种顺序。
例题一共包含四个文件。运行程序前提:项目安装了nodejs
1.stack_list.js:实现了一个普通的栈。
2.createNode.js:用于初始化节点
3.createList.js:实现一个单链表
4.desending.js:倒序输出单链表
注意:我的项目中这四个文件是在同一目录下的。如果不在同一目录下,需要修改require的路径参数。
例题一共包含四个文件。运行程序前提:项目安装了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的路径参数。
相关文章推荐
- 用js实现上传图片前的预览(TX的面试题)
- 用js实现上传图片前的预览(TX的面试题)
- js实现打印
- JS实现页面打印
- JS 实现打印功能
- 有关打印、收藏等的JS代码(打印等主要使用了一个IE组件来实现)
- JS实现局部打印和预览:
- [华赛面试题] C语言动态分配内存 用指针实现字符串的函数体外打印
- js 实现打印网页中定义的部分内容的代码
- js实现页面打印功能实例代码(附去页眉页脚功能代码)
- JS 实现简单的页面局部打印
- JS实现局部打印和预览
- JS实现局部打印和预览
- js实现网页局部打印
- 通过js实现弹出窗口进行打印
- JS+CSS 实现打印分页 每页都有表头和页尾
- JS实现打印网页内容
- JS 实现简单的页面局部打印
- 使用js实现 Asp.net下的局部打印
- JS利用ActiveX控件的方式,即 Excel.Application,实现对word或者excel的打印