由於自身資料結構的基礎薄弱,買了一本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. 基于百度语音识别API的Python语音识别小程序

    一.功能概述 实现语音为文字,可以扩展到多种场景进行工作,这里只实现其基本的语言接收及转换功能. 在语言录入时,根据语言内容的多少与停顿时间,自动截取音频进行转换. 工作示例: 二.软件环境 操作系统 ...

  2. CTF -攻防世界-crypto新手区(5~11)

    easy_RSA 首先如果你没有密码学基础是得去恶补一下的 然后步骤是先算出欧拉函数 之后提交注意是cyberpeace{********}这样的 ,博主以为是flag{}耽误了很长时间  明明没算错 ...

  3. Java时间格式化年-月-日-时间

    Date d = new Date(); System.out.println(d); //Sat Mar 16 20:58:56 CST 2019 System.out.println(d.toLo ...

  4. rewrite例子集合

    在 httpd 中将一个域名转发到另一个域名 虚拟主机世界近期更换了域名,新域名为 www.wbhw.com, 更加简短好记.这时需要将原来的域名 webhosting-world.com, 以及论坛 ...

  5. CodeForces (字符串从字母a开始删除k个字母)

    You are given a string s consisting of n lowercase Latin letters. Polycarp wants to remove exactly k ...

  6. java实现接口导出csv文件

    Tomxin7 Simple, Interesting | 简单,有趣 业务介绍 项目要求从数据库中查询出相关数据后,通过表格展示给用户,如果用户需要,可以点击导出按钮,导出数据为csv格式. 开发环 ...

  7. ftp主动和被动模式区别

    转载自:http://www.west999.com/cms/wiki/server/2018-11-16/49417.html FTP是基于TCP的服务的,FTP不同之处在于FTP使用两个端口,一个 ...

  8. jupyter notebook 安装配置使用,+目录插件安装

    1.安装 pip3 install jupyter 2.配置 2.1. 生成一个 notebook 配置文件 jupyter notebook --generate-config /root/.jup ...

  9. linux-线程同步之信号量

    1.任务:用户从终端输入任意字符然后统计字符个数显示,输入end则结束 2.使用多线程实现:主线程获取用户输入并判断是否退出,子线程计数 #include <stdio.h> #inclu ...

  10. java this的用法以及原理

    /** * this存在方法中,在方法中被调用. * 且是非static方法中被调用.(this 表示这个类的当前实例,而静态方法不依赖于该类的任何实例,随着类产生而装载,因此方法内不能引用 this ...