由於自身資料結構的基礎薄弱,買了一本JavaScript資料結構與演算法實作的書來看,重新把LinkedList鏈結串列學習了一遍,並用JS實作出來。

LinkedList鏈結串列

要存放多個元素,最常用的資料結構可能是陣列,但是在大多數語言中,陣列的大小是固定的,要從陣列中插入項目或移除項目的成本很高,因為必須移動其他元素。
LinkedList 就解決了這個情況,它存放有順序的元素集合,但不同於陣列,鏈結串列的元素在記憶體中並不是連續放置,每個元素由一個存放元素本身的節點和一個指向下一個元素的指位器組成。

實作

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
function (){

  var Node = function(element){
this.element = element;
this.next = null;
}
var length = 0;
var head = null;
//尾部追加元素
this.append = function(element){
var node = new Node(element),
current;
if(head === null){
head = node;
}else{
current = head;
while(current.next){
current = current.next;
}
current.next = node;
}
length++;
}
//移除特定位置元素
this.removeAt = function(position){大专栏  [JS]實作LinkedList鏈結串列n>
//檢查有無越界
if(position > -1 && position < length){
var current = head,
previous,
index = 0;
//移除第一項
if(position === 0){
head = current.next;
}else{
while(index++ < position ){
previous = current;
current = current.next;
}
}
length--;
return current.element;
}else{
return null;
}
}
//插入特定位置元素
this.insert = function(position, element){
if(position >=0 && position <= length){
var node = new Node(element),
current = head,
previous,
index = 0;
//在第一個位置添加
if(position === 0){
node.next = current;
head = node;
}else{
while(index++ < position){
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;
}
length++;
return true;
}else{
return false;
}
}
//將list輸出成String
this.toString = function(){
var current = head,
string = '';
while(current){
string += current.element + (current.next ? ', ' : '');
current = current.next;
}
return string;
}
//查詢元素在第幾項
this.indexOf = function(element){
var current = head,
index = 0;
while(current){
if(element === current.element){
return index;
}
index++;
current = current.next;
}
return -1;
}
//移除指定元素
this.remove = function(element){
var index = this.indexOf(element);
return this.removeAt(index);
}
this.isEmpty = function(){
return length === 0;
}
this.size = function(){
return length;
}
this.getHead = function(){
return head;
}
}

[JS]實作LinkedList鏈結串列的更多相关文章

  1. ASP.NET MVC 5 實作 GridView 分頁

    本文用 ASP.NET MVC 5 實作一個 GridView,功能包括: 分頁(paging).關鍵字過濾(filtering).排序(sorting).AJAX 非同步執行,外觀上亦支援 Resp ...

  2. D. Kilani and the Game 解析(裸BFS、實作)

    Codeforce 1105 D. Kilani and the Game 解析(裸BFS.實作) 今天我們來看看CF1105D 題目連結 題目 給一個\(n\times m\)的地圖,地圖上有幾種格 ...

  3. VMware虛擬化技術實作問答

    http://www.netadmin.com.tw/article_content.aspx?sn=1202130002&ns=1203280001&jump=3 Q4:啟用VMwa ...

  4. [Xamarin] 簡單實作ListActivity (转帖)

    但是文中案例因為是用事先設好的Layout 但是如果需要被選擇的東西很多時該怎麼辦 我們討論一下,如何製作很簡單的List . 首先我們得先參考一下再android 思維下要製作一個List 需要的架 ...

  5. js實現

    js的代碼寫在<script></script>中: <script></script>可以放在body中或者head中,如果放在body中,一般放在b ...

  6. js實現彈窗

    strSucc += "<br/><font color=\"red\">提醒您!在預設狀態下,Google Chrome 會阻止彈出式視窗自動在 ...

  7. 已知起始点,获取每段等距离途经点的经纬度(用百度js api作)

    已知两个中文地址,自动规划路径,获取路径上每个3公里的点的经纬度 <html> <head> <meta http-equiv="Content-Type&qu ...

  8. GOOGLE搜索從入門到精通V4.0

    1,前言2,摘要3,如何使用本文4,Google簡介5,搜索入門6,初階搜索 6.1,搜索結果要求包含兩個及兩個以上關鍵字 6.2,搜索結果要求不包含某些特定資訊 6.3,搜索結果至少包含多個關鍵字中 ...

  9. MQTT教學(一):認識MQTT

    http://swf.com.tw/?p=1002 本系列文章旨在補充<超圖解物聯網IoT實作入門>,採用Arduino.ESP8266和Node.js實作MQTT物聯網通訊實驗. MQT ...

随机推荐

  1. 2020/1/31 PHP代码审计之目录穿越漏洞

    0x00 目录穿越 目录穿越(Directory Traversal)攻击是黑客能够在Web应用程序所在的根目录以外的文件夹上,任意的存取被限制的文件夹,执行命令或查找数据.目录穿越攻击,也与人称为P ...

  2. kill -HUP 什么意思?

    参考 74.在DNS系统测试时,设named进程号是53,命令 D 通知进程重读配置文件.A kill –USR2 53 B kill –USR1 53 C kill -INT 63 D kill – ...

  3. java.lang.SecurityException: Permission denied (missing INTERNET permission?)

    ndroid app里试图用HttpUrlConnection获取网络连接,忘记在AndroidManifest清单文件里声明需要用到Internet的权限,运行时报此错误. 解决方法 在Androi ...

  4. hook键盘钩子_非dll

    unit Unit1; // download by http://www.codefans.net interface uses Windows, Messages, SysUtils, Class ...

  5. D语言-变量、输入、输出、注释

    Part 1:变量 D语言的变量有很多类型,这里只讨论几个基本类型 §1.1变量的定义方法 在D语言中,变量的定义方法是这样子的: [typename] [var1,var2,var3...] 其中, ...

  6. 机器学习分布式框架horovod安装 (Linux环境)

    1.openmi 下载安装 下载连接: https://download.open-mpi.org/release/open-mpi/v4.0/openmpi-4.0.1.tar.gz 安装命令 1 ...

  7. Python—程序设计:观察者模式

    观察者模式 内容:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新.观察者模式又称“发布-订阅”模式. 角色: 抽象主题(Subject) 具体 ...

  8. Linux(CENTOS7) Nginx负载均衡简单配置

    负载均衡的作用 1.转发功能 按照一定的算法[权重.轮询],将客户端请求转发到不同应用服务器上,减轻单个服务器压力,提高系统并发量. 2.故障移除 通过心跳检测的方式,判断应用服务器当前是否可以正常工 ...

  9. 4. 现代 javascript class 专题 和 异步专题

    class 专题 定义 class //es5 类的定义  属性定义在 function 上, 方法定义在原型链上 function foobar(){ this.foo_ = 'foo'; this ...

  10. 吴裕雄--天生自然 JAVA开发学习:switch case 语句

    public class Test { public static void main(String args[]){ //char grade = args[0].charAt(0); char g ...