-
Notifications
You must be signed in to change notification settings - Fork 16
/
singly-linked-list.js
132 lines (116 loc) · 2.97 KB
/
singly-linked-list.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* Node */
var Node = function(data, next){
this.data = data || null;
this.next = next || null;
}
/* Singly Linked List */
function List() {
this.head = null;
this.tail = null;
}
/* List methods */
List.prototype = {
/*
Method: append
Adds a Node to the END of the List
*/
append: function(data) {
// if linkedList is empty
if(this.head === null) {
// create the head (start) node
this.head = new Node(data, null);
// tail (end) also becomes the head node
this.tail = this.head;
// else linkedList isn't empty
} else {
// give the current tail a new node as its next
this.tail.next = new Node(data, null);
// mark the new node as the end of the list
this.tail = this.tail.next;
}
},
/*
Method: print
Traverse the list. For each node, append the current node's data to
a master list of all data, including head and tail
*/
print: function() {
var listString = 'Head -> ';
// start tracking a 'current' Node, beginning with the head
var current = this.head;
// while the 'current' Node isn't null
while(current !== null) {
// print out the 'current' Node's data
listString += current.data + ' -> ';
// assign our 'current' Node's next to be 'current' (increment!)
current = current.next;
}
console.log(listString +'Tail');
},
/*
Method: prepend
Insert a new Node at the head of the list.
*/
prepend: function(data) {
// YOUR CODE HERE
},
/*
Method: length
Traverse the list. Return the amount of Nodes in the list.
*/
length: function() {
// YOUR CODE HERE
},
/*
Method: exists
Traverse the list. If a Node with the data passed in exists, then return
true. If not, return false
*/
exists: function(data) {
// YOUR CODE HERE
},
/*
Method: each
Traverse the list. For each Node, call the callback function on that Node.
Example: callback(current);
*/
each: function(callback) {
// YOUR CODE HERE
},
/*
Method: indexOf
Traverse the list. If a Node with the data passed is found, return an
index (integer) of that Node's location.
If there's no Node with the desired data, return -1.
*/
indexOf: function(data) {
// YOUR CODE HERE
},
/*
Method: dataFrom
Traverse the list to the ith position and return the corresponding data.
If there is no ith position, return undefined.
*/
dataFrom: function(i) {
// YOUR CODE HERE
},
/*
Method: insertAt
Traverse the List. Find the ith Node in the list and insert a new Node
after it. You must preserve the list structure!
*/
insertAt: function(i, data){
// YOUR CODE HERE
},
/*
Method: delete
Traverse the list, find the node with the corresponding data,
and remove that node. List must still be fully intact after
you remove the node!
*/
delete: function(data) {
// YOUR CODE HERE
}
}
// export for testing
module.exports = List;