Skip to content

Commit a838f3e

Browse files
author
Nicholas C. Zakas
committed
Linked list implementation in JavaScript
1 parent 04a7cd4 commit a838f3e

File tree

3 files changed

+278
-0
lines changed

3 files changed

+278
-0
lines changed

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2009 Nicholas C. Zakas. All rights reserved.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

data-structures/linked-list.htm

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2+
3+
<html>
4+
<head>
5+
<title>Linked List Example</title>
6+
<script language="javascript" type="text/javascript" src="linked-list.js"></script>
7+
</head>
8+
<body>
9+
<h2>Linked List Example</h2>
10+
<hr />
11+
<p>This example shows the usage of the linked list implementation.</p>
12+
<script>
13+
14+
//test creating the linked list
15+
var list = new LinkedList;
16+
17+
//test the add method
18+
list.add("red");
19+
list.add("orange");
20+
list.add("yellow");
21+
list.add("green");
22+
list.add("blue");
23+
list.add("indigo");
24+
list.add("violet");
25+
26+
//test the toString() method
27+
document.write("<p><b>Original List: </b><br />" + list + "</p>");
28+
29+
//test the item() method
30+
document.write("<p><b>Value of node at position 2: </b><br />" + list.item(2) + "</p>");
31+
32+
//test the remove() method
33+
list.remove(2);
34+
document.write("<p><b>After removing node at position 2: </b><br />" + list + "</p>");
35+
36+
list.remove(0);
37+
document.write("<p><b>After removing node at position 0: </b><br />" + list + "</p>");
38+
39+
//test the toArray() method
40+
var array = list.toArray();
41+
42+
document.write("<p><b>New Array from toArray() method: </b><br />" + array + "</p>");
43+
44+
//prove that the Array is actually a JavaScript Array object by sorting it
45+
array.sort();
46+
47+
document.write("<p><b>Sorted Array from toArray() method: </b><br />" + array + "</p>");
48+
49+
//try emptying the list
50+
while (list.size()){
51+
list.remove(0);
52+
}
53+
54+
document.write("<p><b>List after removing all: </b><br />" + list + "</p>");
55+
</script>
56+
</body>
57+
</html>

data-structures/linked-list.js

+202
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/*
2+
* Linked List implementation in JavaScript
3+
* Copyright (c) 2009 Nicholas C. Zakas
4+
* See LICENSE for details on license.
5+
*/
6+
7+
//=================================================================
8+
// LinkedListNode Implementation
9+
//=================================================================
10+
11+
//-----------------------------------------------------------------
12+
// Class LinkedListNode
13+
//-----------------------------------------------------------------
14+
// Author(s)
15+
// Nicholas C. Zakas (NCZ), 9/5/02
16+
//
17+
// Description
18+
// A linked list data node.
19+
//
20+
// Arguments
21+
// vData (Variant) - the data to store in the node.
22+
//-----------------------------------------------------------------
23+
function LinkedListNode(vData) {
24+
this.data = vData; //the data for this node
25+
this.next = null; //pointer to next node in the list
26+
}
27+
28+
//=================================================================
29+
// LinkedList Implementation
30+
//=================================================================
31+
32+
/**
33+
* A linked list implementation in JavaScript.
34+
* @class LinkedList
35+
* @constructor
36+
*/
37+
function LinkedList() {
38+
39+
/**
40+
* The number of items in the list.
41+
* @property _length
42+
* @type int
43+
* @private
44+
*/
45+
this._length = 0;
46+
47+
/**
48+
* Pointer to first item in the list.
49+
* @property _list
50+
* @type Object
51+
* @private
52+
*/
53+
this._list = null;
54+
}
55+
56+
LinkedList.prototype = {
57+
58+
//restore constructor
59+
constructor: LinkedList,
60+
61+
/**
62+
* Appends some data to the end of the list. This method traverses
63+
* the existing list and places the value at the end in a new item.
64+
* @param {variant} data The data to add to the list.
65+
* @return {Void}
66+
* @method add
67+
*/
68+
add: function (data){
69+
70+
//create a new item object, place data in
71+
var item = {
72+
data: data,
73+
next: null
74+
},
75+
76+
//used to traverse the structure
77+
current,
78+
previous;
79+
80+
//special case: no items in the list yet
81+
if (this._list === null){
82+
this._list = item;
83+
} else {
84+
previous = this._list;
85+
current = this._list.next;
86+
87+
while(current){
88+
previous = current;
89+
current = current.next;
90+
}
91+
92+
previous.next = item;
93+
}
94+
95+
//don't forget to update the count
96+
this._length++;
97+
98+
},
99+
100+
/**
101+
* Retrieves the data in the given position in the list.
102+
* @param {int} index The zero-based index of the item whose value
103+
* should be returned.
104+
* @return {variant} The value in the "data" portion of the given item
105+
* or null if the item doesn't exist.
106+
* @method item
107+
*/
108+
item: function(index){
109+
110+
//check for out-of-bounds values
111+
if (index > -1 && index < this._length){
112+
var current = this._list,
113+
i = 0;
114+
115+
while(i++ < index){
116+
current = current.next;
117+
}
118+
119+
return current.data;
120+
} else {
121+
return null;
122+
}
123+
},
124+
125+
/**
126+
* Removes the item from the given location in the list.
127+
* @param {int} index The zero-based index of the item to remove.
128+
* @return {variant} The data in the given position in the list or null if
129+
* the item doesn't exist.
130+
* @method remove
131+
*/
132+
remove: function(index){
133+
134+
//check for out-of-bounds values
135+
if (index > -1 && index < this._length){
136+
137+
var current = this._list,
138+
previous,
139+
i = 0;
140+
141+
//special case: removing first item
142+
if (index === 0){
143+
this._list = current.next;
144+
} else {
145+
146+
//find the right location
147+
while(i++ < index){
148+
previous = current;
149+
current = current.next;
150+
}
151+
152+
//skip over the item to remove
153+
previous.next = current.next;
154+
}
155+
156+
//decrement the length
157+
this._length--;
158+
159+
//return the value
160+
return current.data;
161+
162+
} else {
163+
return null;
164+
}
165+
166+
},
167+
168+
/**
169+
* Returns the number of items in the list.
170+
* @return {int} The number of items in the list.
171+
* @method size
172+
*/
173+
size: function(){
174+
return this._length;
175+
},
176+
177+
/**
178+
* Converts the list into an array.
179+
* @return {Array} An array containing all of the data in the list.
180+
* @method toArray
181+
*/
182+
toArray: function(){
183+
var result = [],
184+
current = this._list;
185+
186+
while(current){
187+
result.push(current.data);
188+
current = current.next;
189+
}
190+
191+
return result;
192+
},
193+
194+
/**
195+
* Converts the list into a string representation.
196+
* @return {String} A string representation of the list.
197+
* @method toString
198+
*/
199+
toString: function(){
200+
return this.toArray().toString();
201+
}
202+
};

0 commit comments

Comments
 (0)