forked from den-wdi-2/linked-lists
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathchallenges.js
139 lines (124 loc) · 3.42 KB
/
challenges.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
133
134
135
136
137
138
139
/* Linked List */
function List() {
this.start = null;
this.end = null;
}
/* List methods */
List.prototype = {
/*
Method: makeNode
Make a simple Node object
*/
makeNode : function() {
return {data: null, next: null};
},
/*
Method: insertAtTail
Adds a Node to the END of the List
*/
insertAtTail: function(data) {
// if linkedList is empty
if(this.start === null) {
// start becomes a node
this.start = this.makeNode();
// end becomes the start node
this.end = this.start;
// else linkedList isn't empty
} else {
// create a next property for end and assign it a new Node
this.end.next = this.makeNode();
// move the end node to end's next node
this.end = this.end.next;
}
// finally, add the data to the end Node
this.end.data = data;
},
/*
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 -> ';
// set our 'current' Node to the starting node
var current = this.start;
// 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: insertAtHead
Insert a new Node at the head of the list.
*/
insertAtHead: function(data) {
// Enter code here!
},
/*
Method: length
Traverse the list. Return the amount of Nodes in the list.
*/
length: function() {
// Enter 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) {
// Enter code here!
},
/*
Method: each
Traverse the list. For each Node, call the function f on that Node.
Example: f(current);
*/
each: function(f) {
// Enter 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.
*/
indexOf: function(data) {
// Enter code here!
},
/*
Method: dataFrom
Traverse the list to the ith position and return the corresponding data.
*/
dataFrom: function(i) {
// Enter 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){
// Enter 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) {
// Enter code here!
}
}
/* LinkedList initialization */
var LinkedList = new List();
var i = 2;
while(i <= 20) {
LinkedList.insertAtTail(i);
i+=2;
}
LinkedList.print();