-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
2206 lines (1093 loc) · 177 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
<link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />
<meta name="keywords" content="Hexo, NexT" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />
<meta name="description" content="ziyun‘s blog | 前端 | JavaScript | webpack | CSS">
<meta property="og:type" content="website">
<meta property="og:title" content="ziyun">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="ziyun">
<meta property="og:description" content="ziyun‘s blog | 前端 | JavaScript | webpack | CSS">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="ziyun">
<meta name="twitter:description" content="ziyun‘s blog | 前端 | JavaScript | webpack | CSS">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Pisces',
sidebar: {"position":"right","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
fancybox: true,
motion: true,
duoshuo: {
userId: '0',
author: '博主'
},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
</script>
<link rel="canonical" href="http://yoursite.com/"/>
<title> ziyun </title>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<script type="text/javascript">
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?63e4ec0ae2955c8a2087a6194757b136";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<div class="container one-collumn sidebar-position-right
page-home
">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-wrapper">
<div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">ziyun</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle">ziyun‘s study blog</p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br />
首页
</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories" rel="section">
<i class="menu-item-icon fa fa-fw fa-th"></i> <br />
分类
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
归档
</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags" rel="section">
<i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
标签
</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/04/06/数据结构-字典/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Zi Yun">
<meta itemprop="description" content="">
<meta itemprop="image" content="/uploads/photo.jpeg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="ziyun">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2017/04/06/数据结构-字典/" itemprop="url">
数据结构-字典
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2017-04-06T15:25:30+08:00">
2017-04-06
</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/数据结构与算法JavaScript描述/" itemprop="url" rel="index">
<span itemprop="name">数据结构与算法JavaScript描述</span>
</a>
</span>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<a href="/2017/04/06/数据结构-字典/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2017/04/06/数据结构-字典/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="chapter-7-字典"><a href="#chapter-7-字典" class="headerlink" title="chapter-7 字典"></a>chapter-7 字典</h2><ul>
<li>键值对形式存储数据的数据结构</li>
<li>JavaScript的Object类就是以字典的形式设计的</li>
<li>基础是Array类,可以对字典的键排序,JavaScript中是不能对对象的属性进行排序的。</li>
<li>JavaScript中一切皆对象,数组也是对象。</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * 字典实现</div><div class="line"> */</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Dictionary</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="keyword">this</span>.datastore = <span class="keyword">new</span> <span class="built_in">Array</span>();</div><div class="line">}</div><div class="line">Dictionary.prototype = {</div><div class="line"> <span class="attr">constructor</span>: Dictionary,</div><div class="line"> <span class="comment">// 添加</span></div><div class="line"> add: <span class="function"><span class="keyword">function</span> (<span class="params">key, value</span>) </span>{</div><div class="line"> <span class="keyword">this</span>.datastore[key] = value;</div><div class="line"> },</div><div class="line"> <span class="comment">// 查找</span></div><div class="line"> find: <span class="function"><span class="keyword">function</span> (<span class="params">key</span>) </span>{</div><div class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.datastore[key];</div><div class="line"> },</div><div class="line"> <span class="comment">// 删除</span></div><div class="line"> remove: <span class="function"><span class="keyword">function</span> (<span class="params">key</span>) </span>{</div><div class="line"> <span class="keyword">delete</span> <span class="keyword">this</span>.datastore[key];</div><div class="line"> },</div><div class="line"> <span class="comment">// 显示字典中所有的键值对</span></div><div class="line"> showAll: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="comment">// 对键名进行重排序</span></div><div class="line"> <span class="keyword">var</span> arr = <span class="built_in">Object</span>.keys(<span class="keyword">this</span>.datastore).sort();</div><div class="line"> <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i < arr.length; i++) {</div><div class="line"> <span class="keyword">var</span> key = arr[i];</div><div class="line"> <span class="built_in">console</span>.log(key + <span class="string">' -> '</span> + <span class="keyword">this</span>.datastore[key])</div><div class="line"> }</div><div class="line"> },</div><div class="line"> <span class="comment">// 字典元素个数</span></div><div class="line"> count: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="comment">// 当数组的键的类型为字符串的时候,length属性就不管用了</span></div><div class="line"> <span class="keyword">return</span> <span class="built_in">Object</span>.keys(<span class="keyword">this</span>.datastore).length;</div><div class="line"> },</div><div class="line"> <span class="comment">// 清除</span></div><div class="line"> clear: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="keyword">for</span> (<span class="keyword">var</span> key <span class="keyword">in</span> <span class="keyword">this</span>.datastore) {</div><div class="line"> <span class="keyword">delete</span> <span class="keyword">this</span>.datastore[key];</div><div class="line"> } </div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// 主程序</span></div><div class="line"><span class="keyword">var</span> pbook = <span class="keyword">new</span> Dictionary();</div><div class="line"><span class="comment">// pbook.add('Mike', '123');</span></div><div class="line"><span class="comment">// pbook.add('David', '345');</span></div><div class="line"><span class="comment">// pbook.add('Cynthia', '456');</span></div><div class="line"><span class="comment">// console.log(pbook.find('David'));</span></div><div class="line"><span class="comment">// pbook.remove('David');</span></div><div class="line"><span class="comment">// pbook.showAll();</span></div><div class="line"><span class="comment">// console.log(pbook.count());</span></div><div class="line"><span class="comment">// pbook.clear();</span></div><div class="line"><span class="comment">// console.log(pbook.count());</span></div><div class="line"><span class="comment">// debugger</span></div><div class="line">pbook.add(<span class="string">'Raymond'</span>, <span class="string">'123'</span>);</div><div class="line">pbook.add(<span class="string">'David'</span>, <span class="string">'345'</span>);</div><div class="line">pbook.add(<span class="string">'Cynthia'</span>, <span class="string">'456'</span>);</div><div class="line">pbook.add(<span class="string">'Mike'</span>, <span class="string">'723'</span>);</div><div class="line">pbook.add(<span class="string">'Jennifer'</span>, <span class="string">'987'</span>);</div><div class="line">pbook.add(<span class="string">'Danny'</span>, <span class="string">'012'</span>);</div><div class="line">pbook.add(<span class="string">'Jonathan'</span>, <span class="string">'666'</span>);</div><div class="line">pbook.showAll();</div></pre></td></tr></table></figure>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/04/06/数据结构-链表/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Zi Yun">
<meta itemprop="description" content="">
<meta itemprop="image" content="/uploads/photo.jpeg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="ziyun">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2017/04/06/数据结构-链表/" itemprop="url">
数据结构-链表
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2017-04-06T15:25:17+08:00">
2017-04-06
</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/数据结构与算法JavaScript描述/" itemprop="url" rel="index">
<span itemprop="name">数据结构与算法JavaScript描述</span>
</a>
</span>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<a href="/2017/04/06/数据结构-链表/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2017/04/06/数据结构-链表/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="chapter-6-链表"><a href="#chapter-6-链表" class="headerlink" title="chapter-6 链表"></a>chapter-6 链表</h2><h3 id="数组的缺点"><a href="#数组的缺点" class="headerlink" title="数组的缺点"></a>数组的缺点</h3><ul>
<li>很多语言里,数组的长度固定。填满后,加入新元素很困难。</li>
<li>添加、删除元素也很麻烦,因为需要将数组中其他元素向前或向后平移,以反映刚刚进行了添加或删除操作。</li>
<li>JavaScript数组不存在上述问题,split方法不需要再访问数组中其他元素了,主要问题是:它们被实现成了对象,与其他语言相比,效率很低。</li>
<li>如果数组使用中很慢,可以考虑用链表代替它,除了对数据的随机访问,链表几乎可以用在任何可以使用一维数组的情况。需要随机访问,数组是更好的选择。</li>
</ul>
<h3 id="定义链表"><a href="#定义链表" class="headerlink" title="定义链表"></a>定义链表</h3><ul>
<li>一组节点组成的集合。</li>
<li>每个节点使用一个对象的引用指向它的后继。指向另一个节点的引用叫做链。</li>
<li>数组靠位置进行引用,链表靠相互之间的关系进行引用。</li>
<li>遍历链表是跟着链接,从首元素走到尾元素(不包含头节点,作为链表的接入点),尾元素指向一个null节点。</li>
<li>头节点是一个特殊固定节点。</li>
<li>插入一个节点的效率很高,修改它前面的节点(前驱),使其指向新加入的节点,新加入的节点则指向原来前驱指向的节点。</li>
<li>删除一个节点,将待删除元素的前驱节点指向待删除元素的后继节点,同时将待删除元素指向null</li>
</ul>
<h3 id="设计一个基于对象的链表"><a href="#设计一个基于对象的链表" class="headerlink" title="设计一个基于对象的链表"></a>设计一个基于对象的链表</h3><ul>
<li>包含两个类:Node类用来表示节点,LinkedList类提供插入、删除、显示列表元素、及一些辅助方法。</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 节点类</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Node</span> (<span class="params">element</span>) </span>{</div><div class="line"> <span class="comment">// 保存节点上的数据</span></div><div class="line"> <span class="keyword">this</span>.element = element;</div><div class="line"> <span class="comment">// 保存指向下一个节点的链接</span></div><div class="line"> <span class="keyword">this</span>.next = <span class="literal">null</span>; </div><div class="line">}</div><div class="line"><span class="comment">// 提供对链表进行操作的方法</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">LList</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="comment">// 属性:使用一个Node对象保存该链表的头节点</span></div><div class="line"> <span class="keyword">this</span>.head = <span class="keyword">new</span> Node(<span class="string">'head'</span>);</div><div class="line">}</div><div class="line">LList.prototype = {</div><div class="line"> <span class="attr">constructor</span>: LList,</div><div class="line"> <span class="comment">// 查找给定的节点</span></div><div class="line"> find: <span class="function"><span class="keyword">function</span> (<span class="params">item</span>) </span>{</div><div class="line"> <span class="keyword">var</span> currNode = <span class="keyword">this</span>.head;</div><div class="line"> <span class="keyword">while</span> (currNode.element !== item) {</div><div class="line"> currNode = currNode.next;</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> currNode;</div><div class="line"> },</div><div class="line"> <span class="comment">// 查找给定的节点前面的节点</span></div><div class="line"> findPrevious (item) {</div><div class="line"> <span class="keyword">var</span> currNode = <span class="keyword">this</span>.head;</div><div class="line"> <span class="keyword">while</span> (currNode.next !== <span class="literal">null</span> && currNode.next.element !== item) {</div><div class="line"> currNode = currNode.next;</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> currNode;</div><div class="line"> },</div><div class="line"> <span class="comment">// 指定节点后插入节点</span></div><div class="line"> insert: <span class="function"><span class="keyword">function</span> (<span class="params">newElement, item</span>) </span>{</div><div class="line"> <span class="comment">// 创建新节点</span></div><div class="line"> <span class="keyword">var</span> newNode = <span class="keyword">new</span> Node(newElement);</div><div class="line"> <span class="comment">// 查找需要插入的位置</span></div><div class="line"> <span class="keyword">var</span> current = <span class="keyword">this</span>.find(item);</div><div class="line"> <span class="comment">// 新节点指向current的下一个节点</span></div><div class="line"> newNode.next = current.next;</div><div class="line"> <span class="comment">// current节点的下一个节点指向新节点</span></div><div class="line"> current.next = newNode;</div><div class="line"> },</div><div class="line"> <span class="comment">// 删除节点</span></div><div class="line"> remove: <span class="function"><span class="keyword">function</span> (<span class="params">item</span>) </span>{</div><div class="line"> <span class="comment">// 找到待删除节点前面的节点</span></div><div class="line"> <span class="keyword">var</span> prevNode = <span class="keyword">this</span>.findPrevious(item);</div><div class="line"> <span class="keyword">if</span> (prevNode.next !== <span class="literal">null</span>) {</div><div class="line"> prevNode.next = prevNode.next.next;</div><div class="line"> }</div><div class="line"> },</div><div class="line"> <span class="comment">// 显示列表中元素</span></div><div class="line"> display: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="comment">// 从头节点开始循环,不显示头节点</span></div><div class="line"> <span class="keyword">var</span> currNode = <span class="keyword">this</span>.head;</div><div class="line"> <span class="keyword">while</span> (currNode.next !== <span class="literal">null</span>) {</div><div class="line"> <span class="built_in">console</span>.log(currNode.next.element);</div><div class="line"> currNode = currNode.next;</div><div class="line"> }</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// 主程序</span></div><div class="line"><span class="keyword">var</span> cities = <span class="keyword">new</span> LList();</div><div class="line">cities.insert(<span class="string">'Conway'</span>, <span class="string">'head'</span>);</div><div class="line">cities.insert(<span class="string">'Russellville'</span>, <span class="string">'Conway'</span>);</div><div class="line">cities.insert(<span class="string">'Carlisle'</span>, <span class="string">'Russellville'</span>);</div><div class="line">cities.insert(<span class="string">'Alma'</span>, <span class="string">'Carlisle'</span>);</div><div class="line"><span class="built_in">console</span>.log(<span class="string">'链表整体数据:============'</span>);</div><div class="line">cities.display();</div><div class="line">cities.remove(<span class="string">'Russellville'</span>);</div><div class="line"><span class="built_in">console</span>.log(<span class="string">'删除数据Russellville后数据:============'</span>);</div><div class="line">cities.display();</div></pre></td></tr></table></figure>
<img src="/2017/04/06/数据结构-链表/1.jpeg" alt="1.jpeg" title="">
<h3 id="双向链表"><a href="#双向链表" class="headerlink" title="双向链表"></a>双向链表</h3><ul>
<li>从链表的头节点遍历到尾节点很简单,但是反过来,从后向前遍历则没那么简单。给Node增加一个属性,存储指向前驱节点的链接。</li>
<li>插入时,需要更多的工作,指出节点正确的前驱和后继。</li>
<li>删除时,效率提高了,不需要再查找待删除节点的前驱节点了。</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * 双向链表实现</div><div class="line"> */</div><div class="line"></div><div class="line"><span class="comment">// 节点类</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Node</span> (<span class="params">element</span>) </span>{</div><div class="line"> <span class="comment">// 保存节点上的数据</span></div><div class="line"> <span class="keyword">this</span>.element = element;</div><div class="line"> <span class="comment">// 保存指向下一个节点的链接</span></div><div class="line"> <span class="keyword">this</span>.next = <span class="literal">null</span>; </div><div class="line"> <span class="comment">// 保存前驱节点指向</span></div><div class="line"> <span class="keyword">this</span>.previous = <span class="literal">null</span>;</div><div class="line">}</div><div class="line"><span class="comment">// 提供对链表进行操作的方法</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">LList</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="comment">// 属性:使用一个Node对象保存该链表的头节点</span></div><div class="line"> <span class="keyword">this</span>.head = <span class="keyword">new</span> Node(<span class="string">'head'</span>);</div><div class="line">}</div><div class="line">LList.prototype = {</div><div class="line"> <span class="attr">constructor</span>: LList,</div><div class="line"> <span class="comment">// 查找给定的节点</span></div><div class="line"> find: <span class="function"><span class="keyword">function</span> (<span class="params">item</span>) </span>{</div><div class="line"> <span class="keyword">var</span> currNode = <span class="keyword">this</span>.head;</div><div class="line"> <span class="keyword">while</span> (currNode.element !== item) {</div><div class="line"> currNode = currNode.next;</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> currNode;</div><div class="line"> },</div><div class="line"> <span class="comment">// 查找最后的节点</span></div><div class="line"> findLast () {</div><div class="line"> <span class="keyword">var</span> currNode = <span class="keyword">this</span>.head;</div><div class="line"> <span class="keyword">while</span> (currNode.next !== <span class="literal">null</span>) {</div><div class="line"> currNode = currNode.next;</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> currNode;</div><div class="line"> },</div><div class="line"> <span class="comment">// 指定节点后插入节点</span></div><div class="line"> insert: <span class="function"><span class="keyword">function</span> (<span class="params">newElement, item</span>) </span>{</div><div class="line"> <span class="comment">// 创建新节点</span></div><div class="line"> <span class="keyword">var</span> newNode = <span class="keyword">new</span> Node(newElement);</div><div class="line"> <span class="comment">// 查找需要插入的位置</span></div><div class="line"> <span class="keyword">var</span> current = <span class="keyword">this</span>.find(item);</div><div class="line"> <span class="comment">// 新节点指向current的下一个节点</span></div><div class="line"> newNode.next = current.next;</div><div class="line"> <span class="comment">// 新节点的前驱属性</span></div><div class="line"> newNode.previous = current;</div><div class="line"> <span class="comment">// current节点的下一个节点指向新节点</span></div><div class="line"> current.next = newNode;</div><div class="line"> },</div><div class="line"> <span class="comment">// 删除节点</span></div><div class="line"> remove: <span class="function"><span class="keyword">function</span> (<span class="params">item</span>) </span>{</div><div class="line"> <span class="keyword">var</span> currNode = <span class="keyword">this</span>.find(item);</div><div class="line"> <span class="keyword">if</span> (currNode.next !== <span class="literal">null</span>) {</div><div class="line"> currNode.previous.next = currNode.next;</div><div class="line"> currNode.next.previous = currNode.previous;</div><div class="line"> currNode.next = <span class="literal">null</span>;</div><div class="line"> currNode.previous = <span class="literal">null</span>;</div><div class="line"> }</div><div class="line"> },</div><div class="line"> <span class="comment">// 显示列表中元素</span></div><div class="line"> display: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="comment">// 从头节点开始循环,不显示头节点</span></div><div class="line"> <span class="keyword">var</span> currNode = <span class="keyword">this</span>.head;</div><div class="line"> <span class="keyword">while</span> (currNode.next !== <span class="literal">null</span>) {</div><div class="line"> <span class="built_in">console</span>.log(currNode.next.element);</div><div class="line"> currNode = currNode.next;</div><div class="line"> }</div><div class="line"> },</div><div class="line"> <span class="comment">// 反序显示双向链表中的元素</span></div><div class="line"> dispReverse: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="keyword">var</span> currNode = <span class="keyword">this</span>.head;</div><div class="line"> currNode = <span class="keyword">this</span>.findLast();</div><div class="line"> <span class="keyword">while</span> (currNode.previous !== <span class="literal">null</span>) {</div><div class="line"> <span class="built_in">console</span>.log(currNode.element);</div><div class="line"> currNode = currNode.previous;</div><div class="line"> }</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// 主程序</span></div><div class="line"><span class="keyword">var</span> cities = <span class="keyword">new</span> LList();</div><div class="line">cities.insert(<span class="string">'Conway'</span>, <span class="string">'head'</span>);</div><div class="line">cities.insert(<span class="string">'Russellville'</span>, <span class="string">'Conway'</span>);</div><div class="line">cities.insert(<span class="string">'Carlisle'</span>, <span class="string">'Russellville'</span>);</div><div class="line">cities.insert(<span class="string">'Alma'</span>, <span class="string">'Carlisle'</span>);</div><div class="line"><span class="built_in">console</span>.log(<span class="string">'链表整体数据:============'</span>);</div><div class="line">cities.display();</div><div class="line">cities.remove(<span class="string">'Russellville'</span>);</div><div class="line"><span class="built_in">console</span>.log(<span class="string">'删除数据Russellville后数据:============'</span>);</div><div class="line">cities.display();</div><div class="line"><span class="built_in">console</span>.log(<span class="string">'反转展示数据:============'</span>);</div><div class="line">cities.dispReverse();</div></pre></td></tr></table></figure>
<p><strong>计算了一下时间:</strong></p>
<ul>
<li>插入10000个Node的时间<ul>
<li>普通链表和双向链表差不多,普通链表240ms,双向链表250ms</li>
</ul>
</li>
<li>依次删除这全部10000个节点<ul>
<li>普通链表在150ms左右</li>
<li>双向链表时间则较长,需要230ms左右</li>
</ul>
</li>
<li>随机删除一个元素(Math.random())<ul>
<li>普通链表平均:0.283ms</li>
<li>双向链表平均:0.196ms</li>
</ul>
</li>
</ul>
<p><code>所以书上写的双向链表更高效应该是指随机删除任意一个元素的平均时间</code></p>
<h3 id="循环链表"><a href="#循环链表" class="headerlink" title="循环链表"></a>循环链表</h3><ul>
<li>节点类型和单向链表类似,区别是,创建循环链表时,让其头节点的next属性指向它本身。</li>
<li>希望可以从后向前遍历链表,但是又不想付出额外代价来创建一个双向链表,就需要使用循环链表。</li>
<li>从循环链表的尾节点向后移动,就等于从后向前遍历链表</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * 循环链表实现</div><div class="line"> */</div><div class="line"></div><div class="line"><span class="comment">// 节点类</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Node</span> (<span class="params">element</span>) </span>{</div><div class="line"> <span class="comment">// 保存节点上的数据</span></div><div class="line"> <span class="keyword">this</span>.element = element;</div><div class="line"> <span class="comment">// 保存指向下一个节点的链接</span></div><div class="line"> <span class="keyword">this</span>.next = <span class="literal">null</span>; </div><div class="line">}</div><div class="line"><span class="comment">// 提供对链表进行操作的方法</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">LList</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="comment">// 属性:使用一个Node对象保存该链表的头节点</span></div><div class="line"> <span class="keyword">this</span>.head = <span class="keyword">new</span> Node(<span class="string">'head'</span>);</div><div class="line"> <span class="keyword">this</span>.head.next = <span class="keyword">this</span>.head;</div><div class="line">}</div><div class="line">LList.prototype = {</div><div class="line"> <span class="attr">constructor</span>: LList,</div><div class="line"> <span class="comment">// 查找给定的节点</span></div><div class="line"> find: <span class="function"><span class="keyword">function</span> (<span class="params">item</span>) </span>{</div><div class="line"> <span class="keyword">var</span> currNode = <span class="keyword">this</span>.head;</div><div class="line"> <span class="keyword">while</span> (currNode.element !== item) {</div><div class="line"> currNode = currNode.next;</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> currNode;</div><div class="line"> },</div><div class="line"> <span class="comment">// 查找给定的节点前面的节点</span></div><div class="line"> findPrevious (item) {</div><div class="line"> <span class="keyword">var</span> currNode = <span class="keyword">this</span>.head;</div><div class="line"> <span class="keyword">while</span> (currNode.next !== <span class="literal">null</span> && currNode.next.element !== <span class="string">'head'</span> && currNode.next.element !== item) {</div><div class="line"> currNode = currNode.next;</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> currNode;</div><div class="line"> },</div><div class="line"> <span class="comment">// 指定节点后插入节点</span></div><div class="line"> insert: <span class="function"><span class="keyword">function</span> (<span class="params">newElement, item</span>) </span>{</div><div class="line"> <span class="comment">// 创建新节点</span></div><div class="line"> <span class="keyword">var</span> newNode = <span class="keyword">new</span> Node(newElement);</div><div class="line"> <span class="comment">// 查找需要插入的位置</span></div><div class="line"> <span class="keyword">var</span> current = <span class="keyword">this</span>.find(item);</div><div class="line"> <span class="comment">// 新节点指向current的下一个节点</span></div><div class="line"> newNode.next = current.next;</div><div class="line"> <span class="comment">// current节点的下一个节点指向新节点</span></div><div class="line"> current.next = newNode;</div><div class="line"> },</div><div class="line"> <span class="comment">// 删除节点</span></div><div class="line"> remove: <span class="function"><span class="keyword">function</span> (<span class="params">item</span>) </span>{</div><div class="line"> <span class="comment">// 找到待删除节点前面的节点</span></div><div class="line"> <span class="keyword">var</span> prevNode = <span class="keyword">this</span>.findPrevious(item);</div><div class="line"> <span class="keyword">if</span> (prevNode.next !== <span class="literal">null</span>) {</div><div class="line"> prevNode.next = prevNode.next.next;</div><div class="line"> }</div><div class="line"> },</div><div class="line"> <span class="comment">// 显示列表中元素</span></div><div class="line"> display: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="comment">// 从头节点开始循环,不显示头节点</span></div><div class="line"> <span class="keyword">var</span> currNode = <span class="keyword">this</span>.head;</div><div class="line"> <span class="comment">// 检查头节点</span></div><div class="line"> <span class="keyword">while</span> (currNode.next !== <span class="literal">null</span> && currNode.next.element !== <span class="string">'head'</span>) {</div><div class="line"> <span class="built_in">console</span>.log(currNode.next.element);</div><div class="line"> currNode = currNode.next;</div><div class="line"> }</div><div class="line"> },</div><div class="line"> <span class="comment">// 反序显示循环链表中的元素</span></div><div class="line"> dispReverse: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="keyword">var</span> currNode = <span class="keyword">this</span>.head;</div><div class="line"> <span class="keyword">var</span> prevNode = <span class="keyword">this</span>.findPrevious(currNode.element);</div><div class="line"> <span class="keyword">while</span> (prevNode.element !== currNode.element && prevNode.element !== <span class="string">'head'</span>) {</div><div class="line"> <span class="built_in">console</span>.log(prevNode.element);</div><div class="line"> currNode = prevNode;</div><div class="line"> prevNode = <span class="keyword">this</span>.findPrevious(currNode.element);</div><div class="line"> }</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">var</span> cities = <span class="keyword">new</span> LList();</div><div class="line">cities.insert(<span class="string">'Conway'</span>, <span class="string">'head'</span>);</div><div class="line">cities.insert(<span class="string">'Russellville'</span>, <span class="string">'Conway'</span>);</div><div class="line">cities.insert(<span class="string">'Carlisle'</span>, <span class="string">'Russellville'</span>);</div><div class="line">cities.insert(<span class="string">'Alma'</span>, <span class="string">'Carlisle'</span>);</div><div class="line"><span class="built_in">console</span>.log(<span class="string">'链表整体数据:============'</span>);</div><div class="line">cities.display();</div><div class="line">cities.remove(<span class="string">'Russellville'</span>);</div><div class="line"><span class="built_in">console</span>.log(<span class="string">'删除数据Russellville后数据:============'</span>);</div><div class="line">cities.display();</div><div class="line"><span class="comment">// debugger</span></div><div class="line"><span class="built_in">console</span>.log(<span class="string">'反转展示数据:============'</span>);</div><div class="line"><span class="built_in">console</span>.time();</div><div class="line">cities.dispReverse();</div><div class="line"><span class="built_in">console</span>.timeEnd();<span class="comment">// 1.213ms</span></div></pre></td></tr></table></figure>
<p><code>一般正常情况所说的链表都是双向链表,比较快</code></p>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/04/06/数据结构-队列/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Zi Yun">
<meta itemprop="description" content="">
<meta itemprop="image" content="/uploads/photo.jpeg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="ziyun">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2017/04/06/数据结构-队列/" itemprop="url">
数据结构-队列
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2017-04-06T15:18:58+08:00">
2017-04-06
</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/数据结构与算法JavaScript描述/" itemprop="url" rel="index">
<span itemprop="name">数据结构与算法JavaScript描述</span>
</a>
</span>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<a href="/2017/04/06/数据结构-队列/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2017/04/06/数据结构-队列/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="chapter-5-队列"><a href="#chapter-5-队列" class="headerlink" title="chapter-5 队列"></a>chapter-5 队列</h2><h3 id="特性"><a href="#特性" class="headerlink" title="特性"></a>特性</h3><ul>
<li>一种列表</li>
<li>只能在队尾插入元素,队首删除元素</li>
<li>存储按顺序排列的数据,先进先出(FIFO)</li>
<li>用于提交操作系统执行的一系列进程</li>
</ul>
<h3 id="主要操作"><a href="#主要操作" class="headerlink" title="主要操作"></a>主要操作</h3><ul>
<li>插入(入队)- 队尾插入新元素</li>
<li>删除(出队)- 删除队头元素</li>
<li>读取队头元素 - peek</li>
<li>存储多少元素 - length</li>
<li>清空所有元素 - clear</li>
</ul>
<h3 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Queue</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="keyword">this</span>.dataStore = [];</div><div class="line">}</div><div class="line">Queue.prototype = {</div><div class="line"> <span class="attr">constructor</span>: Queue,</div><div class="line"> <span class="comment">// 队列尾部添加一个元素</span></div><div class="line"> enqueue: <span class="function"><span class="keyword">function</span> (<span class="params">element</span>) </span>{</div><div class="line"> <span class="keyword">this</span>.dataStore.push(element);</div><div class="line"> },</div><div class="line"> <span class="comment">// 删除队首元素</span></div><div class="line"> dequeue: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.dataStore.shift();</div><div class="line"> },</div><div class="line"> <span class="comment">// 读取队首元素</span></div><div class="line"> front: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.dataStore[<span class="number">0</span>];</div><div class="line"> },</div><div class="line"> <span class="comment">// 读取队尾元素</span></div><div class="line"> back: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.dataStore[<span class="keyword">this</span>.dataStore.length - <span class="number">1</span>];</div><div class="line"> },</div><div class="line"> <span class="comment">// 显示队列内所有元素</span></div><div class="line"> toString: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.dataStore;</div><div class="line"> },</div><div class="line"> <span class="comment">// 判断是否为空</span></div><div class="line"> empty: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.dataStore.length === <span class="number">0</span>;</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<p>测试结果:</p>
<h3 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h3><h4 id="方块舞的舞伴分配问题"><a href="#方块舞的舞伴分配问题" class="headerlink" title="方块舞的舞伴分配问题"></a>方块舞的舞伴分配问题</h4><h4 id="基数排序"><a href="#基数排序" class="headerlink" title="基数排序"></a>基数排序</h4><ul>
<li>9个队列,每个对应1-9每个数字,所有队列保存在一个数组里,使用取余和除法操作决定个位和十位。先根据个位数值对其排序,在根据十位数字排序呢,结果即为排序好的数字<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * 分配函数</div><div class="line"> * @param {array[num]} nums 排序数字数组</div><div class="line"> * @param {array} queues 队列数组,每一项代表分配的基数数字</div><div class="line"> * @param {int} n 进制 </div><div class="line"> * @param {int} digit 当前位数,1:个位、10:十</div><div class="line"> */</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">distribute</span> (<span class="params">nums, queues, n, digit</span>) </span>{</div><div class="line"> <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i < n; i++) {</div><div class="line"> <span class="keyword">if</span> (digit === <span class="number">1</span>) {</div><div class="line"> queues[nums[i] % <span class="number">10</span>].enqueue(nums[i]);</div><div class="line"> } <span class="keyword">else</span> {</div><div class="line"> queues[<span class="built_in">Math</span>.floor(nums[i] / <span class="number">10</span>)].enqueue(nums[i]);</div><div class="line"> }</div><div class="line"> } </div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * 出队收集</div><div class="line"> * @param {array} queues 队列数组</div><div class="line"> * @param {array} nums 排序数组</div><div class="line"> */</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">collect</span>(<span class="params">queues, nums</span>) </span>{</div><div class="line"> <span class="comment">// debugger</span></div><div class="line"> <span class="keyword">var</span> i = <span class="number">0</span>;</div><div class="line"> <span class="keyword">for</span> (<span class="keyword">var</span> digit = <span class="number">0</span>; digit < <span class="number">10</span>; digit++) {</div><div class="line"> <span class="keyword">while</span> (!queues[digit].empty()) {</div><div class="line"> nums[i++] = queues[digit].dequeue();</div><div class="line"> }</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// 执行程序</span></div><div class="line"><span class="keyword">var</span> queues = [];</div><div class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i < <span class="number">10</span>; i++) {</div><div class="line"> queues[i] = <span class="keyword">new</span> Queue();</div><div class="line">}</div><div class="line"><span class="keyword">var</span> nums = [];</div><div class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i < <span class="number">10</span>; i++) {</div><div class="line"> nums[i] = <span class="built_in">Math</span>.floor(<span class="built_in">Math</span>.random() * <span class="number">100</span>);</div><div class="line">}</div><div class="line"><span class="built_in">console</span>.log(<span class="string">'原始数据:'</span> + nums);</div><div class="line"><span class="comment">// 个位分布排序</span></div><div class="line">distribute(nums, queues, <span class="number">10</span>, <span class="number">1</span>);</div><div class="line">collect(queues, nums);</div><div class="line"><span class="built_in">console</span>.log(<span class="string">'个位排序后数据:'</span> + nums);</div><div class="line"><span class="comment">// 十位分布排序</span></div><div class="line">distribute(nums, queues, <span class="number">10</span>, <span class="number">10</span>);</div><div class="line">collect(queues, nums);</div><div class="line"><span class="built_in">console</span>.log(<span class="string">'十位排序后数据:'</span> + nums);</div></pre></td></tr></table></figure>
</li>
</ul>
<h5 id="test-结果"><a href="#test-结果" class="headerlink" title="test 结果"></a>test 结果</h5>
<h3 id="优先队列"><a href="#优先队列" class="headerlink" title="优先队列"></a>优先队列</h3><ul>
<li>一般情况下,队列中删除的元素遵循先入先出的原则。</li>
<li>可以实现一个按优先权重限制的队列,高优先级先出,同优先级按先入先出处理。<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * 存储队列元素</div><div class="line"> * @param {*} name </div><div class="line"> * @param {int} code 表示优先级</div><div class="line"> */</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Patient</span> (<span class="params">name, code</span>) </span>{</div><div class="line"> <span class="keyword">this</span>.name = name;</div><div class="line"> <span class="keyword">this</span>.code = code;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// 重新定义dequeue函数</span></div><div class="line"><span class="comment">// 顺序查找方法寻找优先级最高的元素(code越小,优先级越高)</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">dequeue</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="keyword">var</span> entry = <span class="number">0</span>;</div><div class="line"> <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i < <span class="keyword">this</span>.dataStore.length;i++) {</div><div class="line"> <span class="keyword">if</span> (<span class="keyword">this</span>.dataStore[i].code < <span class="keyword">this</span>.dataStore[entry].code) {</div><div class="line"> entry = i;</div><div class="line"> }</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.dataStore.splice(entry, <span class="number">1</span>);</div><div class="line">}</div><div class="line"></div><div class="line">Queue.prototype.dequeue = dequeue;</div><div class="line"><span class="comment">// 主程序</span></div><div class="line"><span class="keyword">var</span> ed = <span class="keyword">new</span> Queue();</div><div class="line"><span class="comment">// 数据</span></div><div class="line"><span class="keyword">var</span> data = [</div><div class="line"> [<span class="string">'Smith'</span>, <span class="number">5</span>],</div><div class="line"> [<span class="string">'Jones'</span>, <span class="number">4</span>],</div><div class="line"> [<span class="string">'Fehrenbach'</span>, <span class="number">6</span>],</div><div class="line"> [<span class="string">'Brown'</span>, <span class="number">1</span>],</div><div class="line"> [<span class="string">'Ingram'</span>, <span class="number">1</span>]</div><div class="line">];</div><div class="line"><span class="comment">// 入队</span></div><div class="line"><span class="keyword">for</span> (<span class="keyword">var</span> dd = <span class="number">0</span>; dd < data.length; dd++) {</div><div class="line"> <span class="keyword">var</span> p = <span class="keyword">new</span> Patient(data[dd][<span class="number">0</span>], data[dd][<span class="number">1</span>]);</div><div class="line"> ed.enqueue(p);</div><div class="line">}</div><div class="line"><span class="built_in">console</span>.log(<span class="string">'初始数据:'</span> + <span class="built_in">JSON</span>.stringify(ed.dataStore));</div><div class="line"><span class="comment">// 出队</span></div><div class="line"><span class="keyword">var</span> index = <span class="number">0</span>;</div><div class="line"><span class="keyword">while</span> (!ed.empty()) {</div><div class="line"> <span class="keyword">var</span> seen = ed.dequeue();</div><div class="line"> <span class="comment">// debugger</span></div><div class="line"> data[index++] = seen[<span class="number">0</span>];</div><div class="line">}</div><div class="line"><span class="built_in">console</span>.log(<span class="string">'排序后数据:'</span> + <span class="built_in">JSON</span>.stringify(data));</div></pre></td></tr></table></figure>
</li>
</ul>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/03/10/数据结构-栈/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Zi Yun">
<meta itemprop="description" content="">
<meta itemprop="image" content="/uploads/photo.jpeg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="ziyun">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2017/03/10/数据结构-栈/" itemprop="url">
数据结构-栈
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2017-03-10T00:12:42+08:00">
2017-03-10
</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/数据结构与算法JavaScript描述/" itemprop="url" rel="index">
<span itemprop="name">数据结构与算法JavaScript描述</span>
</a>
</span>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<a href="/2017/03/10/数据结构-栈/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2017/03/10/数据结构-栈/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="chapter-4-栈"><a href="#chapter-4-栈" class="headerlink" title="chapter-4 栈"></a>chapter-4 栈</h2><h3 id="特点"><a href="#特点" class="headerlink" title="特点"></a>特点</h3><ul>
<li>高效的数据结构</li>
<li>数据只能在栈顶添加或删除</li>
<li>特殊的列表,栈内元素只能通过列表的一端访问,这一段称为栈顶。</li>
<li>后入先出(LIFO)的数据结构</li>
<li>任何不在栈顶的元素都无法访问,为了得到栈底的元素,必须先拿掉上面的元素</li>
<li>入栈(push),出栈(pop)、预览栈顶元素(peek)</li>
</ul>
<h3 id="栈的实现"><a href="#栈的实现" class="headerlink" title="栈的实现"></a>栈的实现</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Stack</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="comment">// 保存栈内元素</span></div><div class="line"> <span class="keyword">this</span>.dataStore = [];</div><div class="line"> <span class="comment">// 记录栈顶位置</span></div><div class="line"> <span class="keyword">this</span>.top = <span class="number">0</span>;</div><div class="line">}</div><div class="line"></div><div class="line">Stack.prototype = {</div><div class="line"> <span class="attr">constructor</span>: Stack,</div><div class="line"> <span class="comment">// 向栈中压入元素</span></div><div class="line"> push: <span class="function"><span class="keyword">function</span> (<span class="params">element</span>) </span>{</div><div class="line"> <span class="keyword">this</span>.dataStore[<span class="keyword">this</span>.top++] = element;</div><div class="line"> },</div><div class="line"> <span class="comment">// 推出最后一个元素</span></div><div class="line"> pop: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="comment">// 为什么不删除这个元素呢</span></div><div class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.dataStore[--<span class="keyword">this</span>.top];</div><div class="line"> },</div><div class="line"> <span class="comment">// 返回第top - 1个元素</span></div><div class="line"> peek: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.dataStore[<span class="keyword">this</span>.top - <span class="number">1</span>];</div><div class="line"> },</div><div class="line"> <span class="comment">// 返回栈内元素个数</span></div><div class="line"> length: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.top;</div><div class="line"> },</div><div class="line"> <span class="comment">// 清空一个栈</span></div><div class="line"> clear: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="comment">// 为什么不把dataStore也清空呢</span></div><div class="line"> <span class="keyword">this</span>.top = <span class="number">0</span>;</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<h4 id="测试结果"><a href="#测试结果" class="headerlink" title="测试结果"></a>测试结果</h4><img src="/2017/03/10/数据结构-栈/2.jpeg" alt="2.jpeg" title="">
<h3 id="实际问题"><a href="#实际问题" class="headerlink" title="实际问题"></a>实际问题</h3><p>将数字n转换为以b为基数的数字,实现转换</p>
<ul>
<li>最高位为n%b,压入栈</li>
<li>使用n/b代替n</li>
<li>重复前两步,知道n = 0,没有余数</li>
<li>弹出栈内元素,排列</li>
</ul>
<p>只针对基数为2-9的情况(why?)</p>
<img src="/2017/03/10/数据结构-栈/1.jpeg" alt="1.jpeg" title="">
<p>操作符的参考资料:<a href="https://developer.mozilla.org/en-US/docs/Web/JavaS" target="_blank" rel="external">https://developer.mozilla.org/en-US/docs/Web/JavaS</a><br>cript/Reference/Operators/Assignment_Operators</p>
<h4 id="数制间转换"><a href="#数制间转换" class="headerlink" title="数制间转换"></a>数制间转换</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 数制间的相互转换</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">mulBase</span> (<span class="params">num, base</span>) </span>{</div><div class="line"> <span class="keyword">var</span> s = <span class="keyword">new</span> Stack();</div><div class="line"> <span class="keyword">do</span> {</div><div class="line"> s.push(num % base);</div><div class="line"> num = <span class="built_in">Math</span>.floor(num /= base);</div><div class="line"> } <span class="keyword">while</span> (num > <span class="number">0</span>);</div><div class="line"> <span class="keyword">var</span> converted = <span class="string">''</span>;</div><div class="line"> <span class="keyword">while</span> (s.length() > <span class="number">0</span>) {</div><div class="line"> converted += s.pop();</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> converted;</div><div class="line">}</div><div class="line"><span class="comment">// 转换为二进制和八进制</span></div><div class="line">mulBase(<span class="number">32</span>, <span class="number">2</span>);<span class="comment">// 100000</span></div><div class="line">mulBase(<span class="number">125</span>, <span class="number">8</span>);<span class="comment">// 175</span></div></pre></td></tr></table></figure>
<h4 id="回文"><a href="#回文" class="headerlink" title="回文"></a>回文</h4><ul>
<li>一个单词、短语或数字,从前往后写和从后往前写都是一样的<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">isPalindrome</span> (<span class="params">word</span>) </span>{</div><div class="line"> <span class="keyword">var</span> s = <span class="keyword">new</span> Stack();</div><div class="line"> <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i < word.length;i++){</div><div class="line"> s.push(word[i]);</div><div class="line"> }</div><div class="line"> <span class="keyword">var</span> rword = <span class="string">''</span>;</div><div class="line"> <span class="keyword">while</span> (s.length() > <span class="number">0</span>) {</div><div class="line"> rword += s.pop();</div><div class="line"> }</div><div class="line"> <span class="keyword">if</span> (word === rword) {</div><div class="line"> <span class="keyword">return</span> <span class="literal">true</span>;</div><div class="line"> } <span class="keyword">else</span> {</div><div class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</div><div class="line"> }</div><div class="line"> <span class="built_in">console</span>.log(s.dataStore)</div><div class="line">}</div><div class="line">isPalindrome(<span class="string">'hello'</span>);<span class="comment">// false</span></div><div class="line">isPalindrome(<span class="string">'racecar'</span>);<span class="comment">// true</span></div></pre></td></tr></table></figure>
</li>
</ul>
<h4 id="递归演示"><a href="#递归演示" class="headerlink" title="递归演示"></a>递归演示</h4><ul>
<li>模拟递归过程<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 使用栈模拟递归过程</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">factorial</span> (<span class="params">n</span>) </span>{</div><div class="line"> <span class="keyword">if</span> (n === <span class="number">0</span>) {</div><div class="line"> <span class="keyword">return</span> <span class="number">1</span>;</div><div class="line"> } <span class="keyword">else</span> {</div><div class="line"> <span class="keyword">return</span> n * factorial(n - <span class="number">1</span>);</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">fact</span> (<span class="params">n</span>) </span>{</div><div class="line"> <span class="keyword">var</span> s = <span class="keyword">new</span> Stack();</div><div class="line"> <span class="keyword">while</span> (n > <span class="number">1</span>) {</div><div class="line"> s.push(n--);</div><div class="line"> }</div><div class="line"> <span class="keyword">var</span> product = <span class="number">1</span>;</div><div class="line"> <span class="keyword">while</span> (s.length() > <span class="number">0</span>) {</div><div class="line"> product *= s.pop();</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> product;</div><div class="line">}</div><div class="line">factorial(<span class="number">5</span>);<span class="comment">// 120</span></div><div class="line">fact(<span class="number">5</span>);<span class="comment">// 120</span></div></pre></td></tr></table></figure></li>
</ul>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/03/08/数据结构-列表/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Zi Yun">
<meta itemprop="description" content="">
<meta itemprop="image" content="/uploads/photo.jpeg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="ziyun">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">