使用 JavaScript 实现链表

代码:

  1 <!doctype html>
  2 <html lang="en">
  3 <head>
  4     <meta charset="UTF-8">
  5     <title>Document</title>
  6 </head>
  7 <body></body>
  8 <script>
  9     function LinkedList() {
 10         //需要插入链表的元素
 11         var Node = function(element) {
 12             this.element = element;//元素的值
 13             this.next = null;//指向下一个节点项的指针
 14         };
 15 
 16         var length = 0;//链表的长度
 17         var head = null;//链表中第一个节点(的引用)
 18 
 19         //向链表尾部追加元素
 20         this.append = function(element) {
 21             
 22             var node = new Node(element), current;
 23 
 24             if(head === null) {
 25                 //当链表为空时
 26                 head = node;
 27             } else {
 28                 //要从第一个元素找起
 29                 current = head;
 30                 
 31                 //循环链表,直到找到最后一项
 32                 while(current.next) {
 33                     current = current.next;
 34                 }
 35 
 36                 //把元素插入到链表的末尾
 37                 current.next = node;
 38             }
 39 
 40             length++;
 41         };
 42 
 43         //从链表中根据位置移除元素并返回该元素
 44         this.removeAt = function(position) {
 45             if (position > -1 && position < length) {
 46                 var current = head,
 47                     previous,
 48                     index = 0;
 49 
 50                 //移除第一项
 51                 if(position == 0) {
 52                     head = current.next;
 53                     return current.element;
 54                 }else{
 55                     while(index++ < position){
 56                         previous = current;//删除指定位置前的一个元素
 57                         current = current.next;
 58                     }
 59                     previous.next = current.next;
 60                     length--;
 61                 }
 62                 return current.element;
 63             }else{
 64                 return null;
 65             };
 66         }
 67 
 68         //从链表中根据值移除元素
 69         this.remove = function(element){
 70             var index = this.indexOf(element);
 71             return this.removeAt(index);
 72         };
 73 
 74         //在任意位置插入一个元素
 75         this.insert = function(position, element) {
 76             if(position > -1 && position <= length) {
 77                 var node = new Node(element),
 78                     current = head,
 79                     previous,
 80                     index = 0;
 81 
 82                 if(position === 0){ //在第一个位置添加
 83                     node.next = current;
 84                     head = node;
 85                 }else{
 86                     while(index++ < position) {
 87                         previous = current;
 88                         current = current.next;
 89                     }
 90                     node.next = current;
 91                     previous.next = node;
 92                 }
 93                 length++;
 94                 return true;
 95             }else{
 96                 return false;
 97             }
 98         };
 99 
100         //找到并返回一个元素的位置,如果元素不存在,返回-1
101         this.indexOf = function(element) {
102             var current = head,
103                 index = 0;
104 
105             while(current) {
106                 if(element === current.element) {
107                     return index;
108                 }
109                 index++;
110                 current = current.next;
111             }
112 
113             return -1;
114         };
115 
116         //判断链表是否为空
117         this.isEmpty = function() {
118             return length === 0;
119         };
120 
121         //返回链表的长度
122         this.size = function() {
123             return length;
124         };
125 
126         //查看链表中元素的值(转换为字符串)
127         this.toString = function() {
128             var current = head,
129                 string = '';
130 
131             while(current) {
132                 string += "," + current.element;
133                 current = current.next;
134             }
135             return string.slice(1);
136         };
137 
138         //返回链表中第一个元素
139         this.getHead = function() {
140             return head;
141         };
142 
143         //查看链表(中的元素和指针,以数组形式输出)
144         this.print = function() {
145             var current = head,
146                 list = [];
147 
148             while(current) {
149                 list.push(current);
150                 current = current.next;
151             }
152             return list;
153         };        
154     }
155 
156     var list = new LinkedList();
157     list.append(5);
158     list.append(10);
159     list.append(7);
160     list.append(9);
161     list.append(100);
162     list.append(-2);
163     console.log(list.toString());
164     console.log(list.print());
165     console.log(list.indexOf(115));
166     console.log(list.indexOf(5));
167     console.log(list.indexOf(7));
168     console.log(list.isEmpty());
169     console.log(list.size());
170     console.log(list.getHead());
171 
172     console.log(list.removeAt(0));
173     console.log(list.toString());    
174     console.log(list.removeAt(1));
175     console.log(list.toString());
176 
177     list.insert(0, 500);
178     console.log(list.toString());
179 </script>
180 </html>

输出:

原文地址:https://www.cnblogs.com/dee0912/p/4989479.html