您的位置:

首页 >

腾博会官方网 >

腾博会官方网:JavaScript数据结构之链表的实现 >

腾博会官方网:JavaScript数据结构之链表的实现

2016-10-10 06:15:07

分类:腾博会官方网

前面楼主分别讨论了数据结构栈与队列的实现,当时所用的数据结构都是用的数组来进行实现,但是数组有的时候并不是最佳的数据结构,比如在数组中新增删除元素的时候需要将其他元素进行移动,而在javascript中使用spit()方法不需要访问其他元素。如果你在使用数组的时候发现很慢,就可以考虑使用链表。链表的概念链表是一种常见的数据结构。它是动态地进行存储分配的一种结构。链表有一个“头指针”变量,以head表示,它存放一个地址,指向一个元素。每个结点都使用一个对象的引用指标它的后继,指向另一个结点的引用叫做链。数组元素依靠下标(位置)来进行引用,而链表元素则是靠相互之间的关系来进行引用。因此链表的插入效率很高,下图演示了链表结点d的插入过程:  删除过程:基于对象的链表我们定义2个类,Node类与LinkedList类,Node为结点数据,LinkedList保存操作链表的方法。首先看Node类:  function Node(element){ this.element = element; this.next = null; }element用来保存结点上的数据,next用来保存指向一下结点的的链接。  LinkedList类:function LinkedList(){ this.head = new Node('head'); this.find = find; this.insert = insert; this.remove = remove; this.show = show;}find()方法,从头结点开始,沿着链表结点一直查找,直到找到与item内容相等的element则返回该结点,没找到则返回空。function find(item){ var currentNode = this.head;//从头结点开始 while(currentNode.element!=item){ currentNode = currentNode.next; } return currentNode;//找到返回结点数据,没找到返回null}Insert方法。通过前面元素插入的演示可以看出,实现插入简单四步:1、创建结点2、找到目标结点3、修改目标结点的next指向链接4、将目标结点的next值赋值给要插入的结点的nextfunction insert(newElement,item){ var newNode = new Node(newElement); var currentNode = this.find(item); newNode.next = currentNode.next; currentNode.next = newNode; }Remove()方法。删除某一节点需要先找到被删除结点的前结点,为此我们定义方法frontNode():function frontNode(item){ var currentNode = this.head; while(currentNode.next.element!=item&¤tNode.next!=null){ currentNode = currentNode.next; } return currentNode;}简答三步:1、创建结点2、找到目标结点的前结点3、修改前结点的next指向被删除结点的n后一个结点function remove(item){ var frontNode = this.frontNode(item); //console.log(frontNode.element); frontNode.next = frontNode.next.next; }Show()方法:function show(){ var currentNode = this.head,result; while(currentNode.next!=null){ result += currentNode.next.element;//为了不显示head结点 currentNode = currentNode.next; }}测试程序:var list = new LinkedList();list.insert("a","head");list.insert("b","a");list.insert("c","b");console.log(list.show());list.remove("b");console.log(list.show());输出:双向链表从链表的头节点遍历到尾节点很简单,但有的时候,我们需要从后向前遍。此时我们可以通过给 Node 对象增加一个属性,该属性存储指向前驱节点的链接。楼主用下图来双向链表的工作原理。首先我们先给Node类增加front属性:  function Node(element){ this.element = element; this.next = null; this.front = null; }当然,对应的insert()方法和remove()方法我们也需要做相应的修改: function insert(newElement,item){ var newNode = new Node(newElement); var currentNode = this.find(item); newNode.next = currentNode.next; newNode.front = currentNode;//增加front指向前驱结点 currentNode.next = newNode;}function remove(item){ var currentNode = this.find(item);//找到需要删除的节点 if (currentNode.next != null) { currentNode.front.next = currentNode.next;//让前驱节点指向需要删除的节点的下一个节点 currentNode.next.front = currentNode.front;//让后继节点指向需要删除的节点的上一个节点 currentNode.next = null;//并设置前驱与后继的指向为空 currentNode.front = null; } }反序显示链表:需要给双向链表增加一个方法,用来查找最后的节点。 findLast() 方法找出了链表中的最后一个节点,可以免除从前往后遍历链。function findLast() {//查找链表的最后一个节点 var currentNode = this.head; while (currentNode.next != null) { currentNode = currentNode.next; } return currentNode;}实现反序输出:function showReverse() { var currentNode = this.head, result = ""; currentNode = this.findLast(); while(currentNode.front!=null){ result += currentNode.element + " "; currentNode = currentNode.front; } return result;}测试程序:var list = new LinkedList();list.insert("a","head");list.insert("b","a");list.insert("c","b");console.log(list);list.remove("b");console.log(list.show());console.log(list.showReverse());输出:循环链表循环链表是另一种形式的链式存贮结构。它的特点是表中最后一个结点的指针域指向头结点,整个链表形成一个环。循环链表和单向链表相似,节点类型都是一样的。唯一的区别是,在创建循环链表时,让其头节点的 next 属性指向它本身,即:head.next = head这种行为会传导至链表中的每个节点,使得每个节点的 next 属性都指向链表的头节点。楼主用下图来表示循环链表:修改构造方法:function LinkedList(){ this.head = new Node('head');//初始化 this.head.next = this.head;//直接将头节点的next指向头节点形成循环链表 this.find = find; this.frontNode = frontNode; this.insert = insert; this.remove = remove; this.show = show; }这时需要注意链表的输出方法show()与find()方法,原来的方式在循环链表里会陷入死循环,while循环的循环条件需要修改为当循环到头节点时退出循环。function find(item){ var currentNode = this.head;//从头结点开始 while(currentNode.element!=item&¤tNode.next.element!='head'){ currentNode = currentNode.next; } return currentNode;//找到返回结点数据,没找到返回null}function show(){ var currentNode = this.head,result = ""; while (currentNode.next != null && currentNode.next.element != "head") { result += currentNode.next.element + " "; currentNode = currentNode.next; } return result;}测试程序:var list = new LinkedList();list.insert("a","head");list.insert("b","a");list.insert("c","b");console.log(list.show());list.remove("b");console.log(list.show());测试结果:本文用到的示例代码地址:https://github.com/LJunChina/JavaScript以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

本文实例讲述了jQuery使用正则验证15/18身份证的方法。分享给大家供大家参考,具体如下:最好的jQuery正则验证身份证代码:按照身份证的算法定制而成,精确度完美支持15和18位. 错一个数字或字母都通不过,无需通过数据处理来验证//身份证检验var vcity={ 11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古", 21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏", 33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南", 42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆", 51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃", 63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"};function checkCard(card){ //是否为空 if(!card||!isCardNo(card)||!checkProvince(card)||!checkBirthday(card)||!checkParity(card)){ ZlyJs.divTip("请输入正确的身份证号码"); jQuery(".sf_list").focus(); return false; } return true;};//检查号码是否符合规范,包括长度,类型function isCardNo(card){ //身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/; if(reg.test(card) === false){ return false; } return true;};//取身份证前两位,校验省份function checkProvince(card){ var province = card.substr(0,2); if(vcity[province] == undefined) { return false; } return true;};//检查生日是否正确function checkBirthday(card){ var len = card.length; //身份证15位时,次序为省(3位)市(3位)年(2位)月(2位)日(2位)校验位(3位),皆为数字 if(len == '15'){ var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/; var arr_data = card.match(re_fifteen); var year = arr_data[2]; var month = arr_data[3]; var day = arr_data[4]; var birthday = new Date('19'+year+'/'+month+'/'+day); return verifyBirthday('19'+year,month,day,birthday); } //身份证18位时,次序为省(3位)市(3位)年(4位)月(2位)日(2位)校验位(4位),校验位末尾可能为X if(len == '18'){ var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/; var arr_data = card.match(re_eighteen); var year = arr_data[2]; var month = arr_data[3]; var day = arr_data[4]; var birthday = new Date(year+'/'+month+'/'+day); return verifyBirthday(year,month,day,birthday); } return false;};//校验日期function verifyBirthday(year,month,day,birthday){ var now = new Date(); var now_year = now.getFullYear(); //年月日是否合理 if(birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day){ //判断年份的范围(3岁到100岁之间) var time = now_year - year; if(time >= 3 && time <= 100) { return true; } return false; } return false;};//校验位的检测function checkParity(card){ //15位转18位 card = changeFivteenToEighteen(card); var len = card.length; if(len == '18'){ var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); var cardTemp = 0, i, valnum; for(i = 0; i < 17; i ++){ cardTemp += card.substr(i, 1) * arrInt[i]; } valnum = arrCh[cardTemp % 11]; if(valnum == card.substr(17, 1)){ return true; } return false; } return false;};//15位转18位身份证号function changeFivteenToEighteen(card){ if(card.length == '15'){ var arrInt = new array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); var arrCh = new array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); var cardTemp = 0, i; card = card.substr(0, 6) + '19' + card.substr(6, card.length - 6); for(i = 0; i < 17; i ++){ cardTemp += card.substr(i, 1) * arrInt[i]; } card += arrCh[cardTemp % 11]; return card; } return card;};if (!checkCard(code)) { alert("请输入正确的身份证号码"); return false;}PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:JavaScript正则表达式在线测试工具:http://tools.jb51.net/regex/javascript正则表达式在线生成工具:http://tools.jb51.net/regex/create_reg更多关于jQuery相关内容可查看本站专题:《jQuery正则表达式用法总结》、《jQuery字符串操作技巧总结》、《jQuery操作xml技巧总结》、《jQuery扩展技巧总结》、《jquery选择器用法总结》及《jQuery常用插件及用法总结》希望本文所述对大家jQuery程序设计有所帮助。

本文实例为大家分享了vue实现单选和多选功能的具体代码,供大家参考,具体内容如下复制代码<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta> <title>Document</title> <script src="../vue.js"></script> <style> ul, li { list-style-type: none; } * { margin: 0; padding: 0; } .border-1px { position: relative; } .border-1px:after { display: block; position: absolute; left: 0; bottom: 0; width: 100%; border-top: 1px solid rgba(7, 17, 27, .1); content: ' '; } @media (-webkit-min-device-pixel-ratio: 1.5),(min-device-pixel-ratio: 1.5) { .border-1px::after { -webkit-transform: scaleY(0.7); transform: scaleY(0.7); } } @media (-webkit-min-device-pixel-ratio: 2),(min-device-pixel-ratio: 2) { .border-1px ::after { -webkit-transform: scaleY(0.5); transform: scaleY(0.5); } } #example { margin: 20px; } h3 { font-size: 26px; margin-left: 20px; height: 60px; } .self-radio { display: none; } .self-radio + label { -webkit-appearance: none; background-color: #fff; border: 1px solid #aaa; border-radius: 50px; display: inline-block; position: relative; width: 30px; height: 30px; box-sizing: border-box; } .self-radio:checked + label { border: 1px #47d9bf solid; } .self-radio:checked + label:after { position: absolute; top: 9px; left: 9px; content: ' '; width: 10px; height: 10px; border-radius: 50px; background: #47d9bf; box-shadow: 0px 0px 5px 0px #47d9bf; } .check-area { display: inline-block; width: 400px; padding: 12px 20px; border: 1px solid #aaa; border-top-left-radius: 4px; border-top-right-radius: 4px; } li { height: 60px; } li .self-radio + label { vertical-align: middle; } li span { margin-left: 20px; display: inline-block; line-height: 60px; font-size: 22px; } p { height: 60px; line-height: 60px; margin-left: 20px; } p span { color: #f00; } .btn { margin: 20px auto; width: 100%; text-align: center; } .btn button { width: 120px; height: 40px; line-height: 30px; font-size: 16px; color: #fff; background: #47d9bf; border: 1px #23d5b6 solid; border-radius: 6px; text-align: center; outline: none; } .btn button:hover { background: #23d5b6; } </style></head><body><div id="example"> <h3>单选按钮</h3> <div class="check-area" v-show="items.length!=0"> <ul> <li class="border-1px" v-for="(item,index) in items"> <input class="self-radio" type="radio" :id="'radio-'+item.id" :data-id="'food-'+item.id" name="radio" :checked="index==0" :value="item.value" v-model="checkValue"> <label :for="'radio-'+item.id" @click="setCheckValue(item)"></label> <span>{{item.value}}</span> </li> </ul> <p>您选择了:<span>{{checkValue}}</span></p> <div class="btn"> <button @click="showCheck(checkId)">按钮</button> <span>{{checkId}}</span> </div> </div></div><script> var itemData = [{id: '20170811001', value: '香蕉'}, {id: '20170811002', value: '苹果'}, { id: '20170811003', value: '梨子' }, {id: '20170811004', value: '葡萄'}] //itemData = []; var vm = new Vue({ el: '#example', data: { items: '', checkValue: '', checkId: '' }, methods: { init: function () { }, initData: function () { var self = this; self.items = itemData; if (itemData.length != 0) { self.checkValue = self.items[0].value; self.checkId = 'food-' + self.items[0].id } }, setCheckValue: function (item) { this.checkId = 'food-' + item.id; } , showCheck: function () { console.log(this.checkId) } }, mounted: function () { this.initData(); } })</script></body></html>vue实现多选功能<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta> <title>Document</title> <script src="../vue.js"></script> <style> ul, li { list-style-type: none; } * { margin: 0; padding: 0; } .border-1px { position: relative; } .border-1px:after { display: block; position: absolute; left: 0; bottom: 0; width: 100%; border-top: 1px solid rgba(7, 17, 27, .1); content: ' '; } @media (-webkit-min-device-pixel-ratio: 1.5),(min-device-pixel-ratio: 1.5) { .border-1px::after { -webkit-transform: scaleY(0.7); transform: scaleY(0.7); } } @media (-webkit-min-device-pixel-ratio: 2),(min-device-pixel-ratio: 2) { .border-1px ::after { -webkit-transform: scaleY(0.5); transform: scaleY(0.5); } } #example { margin: 20px; } h3 { font-size: 26px; margin-left: 20px; height: 60px; } .self-checkbox { display: none; } .self-checkbox + label { margin-top: 16px; -webkit-appearance: none; background-color: #fff; border: 2px solid #aaa; border-radius: 5px; display: inline-block; position: relative; width: 30px; height: 30px; box-sizing: border-box; vertical-align: top; } .self-checkbox:checked + label { border: 2px #47d9bf solid; } .self-checkbox:checked + label:after { display: inline-block; text-align: center; content: '√'; width: 100%; height: 30px; line-height: 26px; color: #47d9bf; font-size: 18px; text-shadow: 0px 0px 5px #47d9bf; } .check-area { display: inline-block; width: 400px; padding: 12px 20px; border: 1px solid #aaa; border-top-left-radius: 4px; border-top-right-radius: 4px; } li { height: 60px; } li .self-radio + label { vertical-align: middle; } li span { margin-left: 20px; display: inline-block; line-height: 60px; font-size: 22px; } p { height: 60px; line-height: 60px; margin-left: 20px; } p span { color: #f00; } .btn { margin: 20px auto; width: 100%; text-align: center; } .btn button { width: 120px; height: 40px; line-height: 30px; font-size: 16px; color: #fff; background: #47d9bf; border: 1px #23d5b6 solid; border-radius: 6px; text-align: center; outline: none; } .btn button:hover { background: #23d5b6; } </style></head><body><div id="example"> <h3>多选按钮</h3> <div class="check-area" v-show="items.length!=0"> <ul> <li class="border-1px" v-for="(item,index) in items"> <input class="self-checkbox" type="checkbox" :id="'checkbox-'+item.id" :data-id="'food-'+item.id" name="radio" :value="item.value" v-model="checkValues" @click="setCheckValue($event,item)"> <label :for="'checkbox-'+item.id"></label> <span>{{item.value}}</span> </li> </ul> <p>您选择了:<span v-show="checkValues.length">{{filterCheckValues}}</span></p> <div class="btn"> <button @click="showCheck(checkIds)">按钮</button> <span v-show="checkIds.length">{{checkIds}}</span> </div> </div></div><script> var itemData = [{id: '20170811001', value: '香蕉'}, {id: '20170811002', value: '苹果'}, { id: '20170811003', value: '梨子' }, {id: '20170811004', value: '葡萄'}] //itemData = []; var vm = new Vue({ el: '#example', data: { items: '', checkValues: [], checkIds: [] }, computed: { filterCheckValues: function () { var value = this.checkValues; var reValue = ''; for (var i = 0; i < value.length; i++) { reValue += value[i] + '、' } reValue = reValue.substring(0, reValue.length - 1) return reValue; } }, methods: { initData: function () { var self = this; self.items = itemData; if (itemData.length != 0) {// self.checkValues[0] = self.items[0].value;// self.checkIds[0] = 'food-' + self.items[0].id; } }, setCheckValue: function (ev, item) { var id = 'food-' + item.id; if (ev.target.checked) { this.checkIds.push(id); } else if (this.checkIds.indexOf(id) > -1) { this.checkIds.remove(id); } } , showCheck: function () { console.log(this.checkIds) } }, filter: {}, mounted: function () { this.initData(); } }) Array.prototype.remove = function (val) { var index = this.indexOf(val); if (index > -1) { this.splice(index, 1); } };</script></body></html>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

废话不多说了,直接给大家贴代码了,具体代码如下所示:<pre name="code" class="javascript">angular.module('myApp.loginCtrl', []) .config(function($httpProvider) { $httpProvider.defaults.headers.post['Content-Type']='application/x-www-form-urlencoded;charset=utf-8'; //改变header中content-type的格式 }) .controller('loginCtrl',['$scope','$http',function($scope,$http){}]) //兼容ios动态改变title标题 .run(['$location','$rootScope',function($location, $rootScope){ $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) { $rootScope.title = toState.title }); $rootScope.$watch('title',function(title){ var body = document.getElementsByTagName('body')[0]; document.title = title; var iframe = document.createElement("iframe"); iframe.title = '登陆'; iframe.width = 0; iframe.height = 0; iframe.src = '//m.baidu.com/favicon.ico'; iframe.style.display = 'none'; iframe.addEventListener('load', function() { setTimeout(function() { // iframe.removeEventListener('load'); document.body.removeChild(iframe); }, 0); }); document.body.appendChild(iframe); }); }]); </pre><br> <br> <pre></pre> <pre code_snippet_id="2083087" snippet_file_name="blog_20161229_4_8475823" name="code" class="javascript">//路由<pre name="code" class="javascript"> .state('deliciousFood',{ title:'美食住宿', url:'/deliciousFood', templateUrl:'/hq/views/deliciousFood/deliciousFood.html', controller:'deliciousFoodCtrl', resolve:{ loadMyCtrl:['$ocLazyLoad',function($ocLazyLoad){ return $ocLazyLoad.load([ '/hq/views/deliciousFood/deliciousFoodCtrl.js', ]).then(function(){}) }] } })</pre><br> <pre></pre> <br> </pre> 以上所述是小编给大家介绍的Angularjs 动态改变title标题(兼容ios),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

需求:要求实现点击全选选中所有菜单,再次点击全选取消选中。此功能经常会用户,下面小编给大家分享下实现代码,一起看看吧!效果图如下:点击全选之前:点击全选之后:再次点击全选之后:代码如下:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <input type="checkbox" onclick="quanxuan()">全选 <input type="checkbox" name="nation">唱歌 <input type="checkbox" name="nation">跳舞 <input type="checkbox" name="nation">书法 </body> <script type="text/javascript"> var k=0; function quanxuan(){ var s=document.getElementsByName("nation"); if(k%2==0) { for(var i=0;i< s.length;i++) { s[i].checked=true; } k++; } else { for(var j=0;j< s.length;j++) { s[j].checked=false; } k++; } } </script> </html> 以上所述是小编给大家介绍的基于JS实现checkbox全选功能实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

焦点访谈

最新最热的文章

更多 >

COPYRIGHT (©) 2017 Copyright ©2017 腾博会官网 网站地图

联系我们

827570882

扫描二维码分享到微信