由於自身資料結構的基礎薄弱,買了一本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. ArchLinux安装Gnome桌面

    给Arch安装Gnome桌面美化及常用软件配置 一.创建普通用户 1.安装zsh 个人比较喜欢的一个shell,你们可以和我不同 # pacman -S zsh 2.创建用户 kain是我创建用户的名 ...

  2. 在h5页面上添加音乐播放

    接到需求说要做一个h5轮播图,同时配上背景音乐. Html部分: <!--音乐开始--> <div id="music"> <div id=" ...

  3. 【php】Swoole之php高性能通信框架

    Swoole介绍 swoole是由c语言开发的异步网络通信引擎,被编译为so文件(swoole.so)作为php的extesion扩展. 与其他普通扩展不同: 与普通的扩展不同的是普通的扩展只是提供一 ...

  4. Ubuntu16.04安装配置Caffe教程(GPU版)

    推荐博客:https://www.linuxidc.com/Linux/2017-11/148629.htmhttps://blog.csdn.net/yggaoeecs/article/detail ...

  5. [SDOI2016]游戏(树剖+李超树)

    趁着我把李超树忘个一干二净的时候来复习一下吧,毕竟马上NOI了. 题解:看着那个dis就很不爽,直接把它转换成深度问题,然后一条直线x->y,假设其lca为z,可以拆分成x->z和z-&g ...

  6. 十五、Numpy-科学计算基础库

    Numpy:          NumPy(Numerical Python) 是科学计算基础库,提供大量科学计算相关功能,比如数据统计,随机数生成等.其提供最核心类型为多维数组类型(ndarray) ...

  7. 实验吧web-易-what a fuck!这是什么鬼东西?

    打开链接是一大串符号,是js编码的一种,全部复制下来,粘贴在控制台中回车就拿到flag了.

  8. pycharm使用常见设置

    字体修改: file——setting——Editor——font(快捷键:ctrl+alt+s) 修改项目编译器:a,b方法二选一 a.ctrl+alt+s,调出设置窗口——选中想要换的版本即可,如 ...

  9. 浅copy

    person=['aaa',['a',bbb'] p1=copy.copy(person) p2=person[:] p3=list(person) p4=person.copy() print(ty ...

  10. 计算机网络(6): http cookie

    Cookie作用: 1)帮助管理用户会话信息(用户需要记录的信息:登陆状态等) 2)跟踪浏览器的行为 3)用户自定义设置 实现方式: 当用户浏览带有Cookie的网站时,网站自动为其生成一个唯一的标志 ...