-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
305 lines (277 loc) · 8.77 KB
/
index.html
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<html>
<head>
<title>Sortable Linked List</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
<style>
.newLink {
margin-right: 5em;
margin-top: 2em;
padding: 5px;
}
.item span {
float: left;
font-size: 20px;
}
.item a {
float: right;
font-size: 20px;
text-decoration: none;
}
.item:after {
content: '\2193';
font-size: 24px;
display: block;
/*padding-top: 32px;*/
}
.item {
list-style: none;
font-size: 30px;
text-align: center;
background: white;
height: 50px;
}
.textright {
text-align: right;
float: right;
position: fixed;
right: 0;
top: 15px;
}
.textcenter {
text-align: center;
padding-top: 20px;
}
</style>
<script src='jquery-1.11.3.js'>
</script>
<script src='Sortable.min.js'>
</script>
<script>
var linkedList = {
initialize: function(initLL, head, tail) {
//initLL is an array of Node Objects --- [{id:"", next:"", data: {}}, {}, {}],
linkedList.nodes = initLL;
linkedList.head = head;
linkedList.tail = tail;
},
getListIDsFromTail: function() {},
getListIDsFromHead: function() {
var count = linkedList.nodes.length;
var retArr = [];
var pointer = linkedList.head.id;
while (count > 0) {
if (pointer === null) {
retArr[retArr.length] = "NULL";
break;
}
// var pointer = linkedList.getNode(pointer).next;
retArr[retArr.length] = linkedList.getNode(pointer).id;
if (linkedList.getNode(pointer).next) {
pointer = linkedList.getNode(pointer).next.id;
}
count--;
}
return retArr;
},
nodes: [],
head: null,
tail: null,
insertAt: function(index, data, relocID) {
var newID = new Date().getTime();
// var length = linkedList._length;
var beforeNodeID = beforeNodeID || linkedList.head.id;
var current = linkedList.head,
previous = null,
length = linkedList._length;
newNode = {
id: relocID,
data: data,
next: beforeNodeID,
prev: previous
};
var position = 0;
// check for index out-of-bounds
if (index < 0 || index > linkedList._length) {
return false;
}
// if index is 0, we just need to insert the first node
if (index === 0) {
linkedList.addNodeBefore(data);
}
while (position < index) {
previous = current;
current = linkedList.head.next;
position += 1;
}
current.prev.next = newNode;
newNode.prev = current.prev;
current.prev = newNode;
newNode.next = current;
// if (index === length) {
// newNode.next = current;
// }
linkedList.nodes.splice(index, 0, newNode);
linkedList._length++;
},
addNodeBefore: function(nodeData, beforeNodeID, nodeID) { //alter for A4
var newID = nodeID || new Date().getTime();
beforeNodeID = beforeNodeID;
var prevID = null;
var newNode = {
id: newID,
data: nodeData,
next: beforeNodeID,
prev: prevID
};
if (linkedList.head !== null) {
// linkedList.head = newID;
linkedList.tail.next = newNode;
newNode.prev = linkedList.tail;
linkedList.tail = newNode;
} else {
linkedList.head = newNode;
linkedList.tail = newNode;
}
if (beforeNodeID) {
var prevPos = linkedList.getParentPos(beforeNodeID);
if (prevPos >= 0)
linkedList.nodes[prevPos].next = newID;
}
if (beforeNodeID === linkedList.head) {
linkedList.head.id = newID;
// console.log('No node before');
}
linkedList.nodes[linkedList.nodes.length] = newNode;
},
isEmpty: function() {
if (linkedList.head === null) {
return true;
}
return false;
},
deleteNode: function(id) { //alter for A4
var pos = linkedList.getNodePos(id);
var prevPos = linkedList.getParentPos(id);
var next = linkedList.getNode(id).next;
if (prevPos >= 0)
linkedList.nodes[prevPos].next = next;
linkedList.nodes.splice(pos, 1);
if (id == linkedList.head) {
linkedList.head = next;
}
},
getNodeAt: function(pos) {
return linkedList.nodes[pos];
},
getNodeFromPos: function(pos) {
var ordList = linkedList.getListIDsFromHead();
return ordList[pos];
},
swapNodes: function(fromPos, toPos) { //alter for A4
var moveData = linkedList.getNodeAt(fromPos);
var deleteID = linkedList.getNodeAt(fromPos).id;
var insertBeforeID = linkedList.getNodeAt(toPos).id;
linkedList.deleteNode(deleteID);
linkedList.insertAt(toPos, moveData.data, moveData.id);
console.log(fromPos);
console.log(toPos);
},
getParent: function(id) {
var list = linkedList.nodes;
var ret = null;
for (var i = 0; i < list.length; i++) {
if (linkedList.getNodePos(list[i].id).next === id) {
ret = list[i];
break;
}
}
return ret;
},
getChild: function(id) { //complete for A4
},
getNode: function(id) {
var list = linkedList.nodes;
var ret = null;
for (var i = 0; i < list.length; i++) {
if (list[i].id === id) {
ret = list[i];
break;
}
}
return ret;
},
getParentPos: function(id) {
var pos = -1;
var list = linkedList.nodes;
for (var i = 0; i < list.length; i++) {
if (list[i].next === id) {
pos = i;
break;
}
}
return pos;
},
getNodePos: function(id) {
var pos = -1;
var list = linkedList.nodes;
for (var i = 0; i < list.length; i++) {
if (list[i].id === id) {
pos = i;
break;
}
}
return pos;
}
}
/*
var x = 5;
var y = 10;
// your code here
var swap;
swap = x;
x = y;
y = swap;
// end code
console.log(x);
console.log(y);
*/
var sortable;
function startUp() {
var el = document.getElementById('theList');
sortable = Sortable.create(el, {
onEnd: function(evt) {
linkedList.swapNodes(evt.oldIndex, evt.newIndex);
}
});
}
function addLink() {
var text = prompt("Add new node:");
if (text == null) return;
var id = new Date().getTime();
$("#theList").append("<li class='item " + id + "'>" + text +
"<span clas='idNum'>" + id + "</span>" +
"<a class='btn btn-danger' href='javascript:removeLink(" + id + ")'> X </a>" + "</li>"
);
linkedList.addNodeBefore(text, null, id);
}
function removeLink(id) {
if (confirm("Delete node ID: " + id)) {
$("#theList ." + id).remove();
linkedList.deleteNode(id);
}
}
</script>
</head>
<body onload='startUp()'>
<div class="container">
<h1 class="textcenter"> Sortable Linked List - Assignment 4 </h1>
<h2 class="textcenter">Aldo Mucino-Martinez</h2>
<hr>
<a class='textright newLink' href='javascript:addLink()'> + New Node</a>
<div class="item head"> HEAD </div>
<ul id="theList"> </ul>
<div class="textcenter"> NULL </div>
</div>
</body>
</html>