您的位置:首页 > 理论基础 > 数据结构算法

JavaScript数据结构与算法(六) 链表的实现

2017-06-09 11:20 302 查看
1 // 链表存储有序的元素集合,但不同于数组,链表中的元素在内存中并不是连续放置的。每个
2 // 元素由一个存储元素本身的节点和一个指向下一个元素的引用(也称指针或链接)组成。下图展
3 // 示了一个链表的结构:
4


1 var Node = (function () {
2     function Node(element) {
3         this.element = element;
4         this.next = null;
5     }
6     return Node;
7 }());
8 var LinkedList = (function () {
9     function LinkedList() {
10         this.length = 0;
11         this.head = null;
12     }
13     /**
14      * 向列表尾部添加一个新的项
15      * @param element
16      */
17     LinkedList.prototype.append = function (element) {
18         var node = new Node(element), current;
19         if (this.head === null) {
20             this.head = node;
21         }
22         else {
23             current = this.head;
24             // 循环列表,知道找到最后一项
25             while (current.next) {
26                 current = current.next;
27             }
28             // 找到最后一项,将其next赋为node,建立链接
29             current.next = node;
30         }
31         this.length++; // 更新列表长度
32     };
33     /**
34      * 向列表的特定位置插入一个新的项
35      * @param position
36      * @param element
37      */
38     LinkedList.prototype.insert = function (position, element) {
39         // 检查越界值
40         if (position >= 0 && position <= length) {
41             var node = new Node(element), current = this.head, previous = void 0, index = 0;
42             if (position === 0) {
43                 node.next = current;
44                 this.head = node;
45             }
46             else {
47                 while (index++ < position) {
48                     previous = current;
49                     current = current.next;
50                 }
51                 node.next = current;
52                 previous.next = node;
53             }
54             this.length++; // 更新列表长度
55             return true;
56         }
57         else {
58             return false;
59         }
60     };
61     /**
62      * 从列表的特定位置移除一项
63      * @param position
64      */
65     LinkedList.prototype.removeAt = function (position) {
66         // 检查越界值
67         if (position > -1 && position < this.length) {
68             var current = this.head, previous = void 0, index = 0;
69             // 移除第一项
70             if (position === 0) {
71                 this.head = current.next;
72             }
73             else {
74                 while (index++ < position) {
75                     previous = current;
76                     current = current.next;
77                 }
78                 // 将previous与current的下一项链接起来:跳过current,从而移除它
79                 previous.next = current.next;
80             }
81             this.length--;
82             return current.element;
83         }
84         else {
85             return null;
86         }
87     };
88     /**
89      * 从列表中移除一项
90      * @param element
91      */
92     LinkedList.prototype.remove = function (element) {
93         var index = this.indexOf(element);
94         return this.removeAt(index);
95     };
96     /**
97      * :返回元素在列表中的索引。如果列表中没有该元素则返回-1
98      * @param element
99      */
100     LinkedList.prototype.indexOf = function (element) {
101         var current = this.head, index = -1;
102         while (current) {
103             if (element === current.element) {
104                 return index;
105             }
106             index++;
107             current = current.next;
108         }
109         return -1;
110     };
111     /**
112      * 如果链表中不包含任何元素, 返回true, 如果链表长度大于0则返回false
113      */
114     LinkedList.prototype.isEmpty = function () {
115         return this.length === 0;
116     };
117     /**
118      * 返回链表包含的元素个数。与数组的length属性类似
119      */
120     LinkedList.prototype.size = function () {
121         return this.length;
122     };
123     /**
124      * 由于列表项使用了Node类,就需要重写继承自JavaScript对象默认的toString方法,让其只输出元素的值
125      */
126     LinkedList.prototype.toString = function () {
127         var current = this.head, string = '';
128         while (current) {
129             string += current.element;
130             current = current.next;
131         }
132         return string;
133     };
134     LinkedList.prototype.getHead = function () {
135         return this.head;
136     };
137     LinkedList.prototype.print = function () {
138         console.log(this.toString());
139     };
140     return LinkedList;
141 }());


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: