-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathQueueLL
275 lines (200 loc) · 6.64 KB
/
QueueLL
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
var LINKED_LIST_START_X = 100;
var LINKED_LIST_START_Y = 200;
var LINKED_LIST_ELEM_WIDTH = 70;
var LINKED_LIST_ELEM_HEIGHT = 30;
var LINKED_LIST_INSERT_X = 250;
var LINKED_LIST_INSERT_Y = 50;
var LINKED_LIST_ELEMS_PER_LINE = 8;
var LINKED_LIST_ELEM_SPACING = 100;
var LINKED_LIST_LINE_SPACING = 100;
var TOP_POS_X = 180;
var TOP_POS_Y = 100;
var TOP_LABEL_X = 130;
var TOP_LABEL_Y = 100;
var TOP_ELEM_WIDTH = 30;
var TOP_ELEM_HEIGHT = 30;
var PUSH_LABEL_X = 50;
var PUSH_LABEL_Y = 30;
var PUSH_ELEMENT_X = 120;
var PUSH_ELEMENT_Y = 30;
var SIZE = 32;
function StackLL(am, w, h)
{
this.init(am, w, h);
}
StackLL.prototype = new Algorithm();
StackLL.prototype.constructor = StackLL;
StackLL.superclass = Algorithm.prototype;
StackLL.prototype.init = function(am, w, h)
{
StackLL.superclass.init.call(this, am, w, h);
this.addControls();
this.nextIndex = 0;
this.commands = [];
this.setup();
this.initialIndex = this.nextIndex;
}
StackLL.prototype.addControls = function()
{
this.controls = [];
this.pushField = addControlToAlgorithmBar("Text", "");
this.pushField.onkeydown = this.returnSubmit(this.pushField, this.pushCallback.bind(this), 6);
this.pushButton = addControlToAlgorithmBar("Button", "Push");
this.pushButton.onclick = this.pushCallback.bind(this);
this.controls.push(this.pushField);
this.controls.push(this.pushButton);
this.popButton = addControlToAlgorithmBar("Button", "Pop");
this.popButton.onclick = this.popCallback.bind(this);
this.controls.push(this.popButton);
this.clearButton = addControlToAlgorithmBar("Button", "Clear Stack");
this.clearButton.onclick = this.clearCallback.bind(this);
this.controls.push(this.clearButton);
}
StackLL.prototype.enableUI = function(event)
{
for (var i = 0; i < this.controls.length; i++)
{
this.controls[i].disabled = false;
}
}
StackLL.prototype.disableUI = function(event)
{
for (var i = 0; i < this.controls.length; i++)
{
this.controls[i].disabled = true;
}
}
StackLL.prototype.setup = function()
{
this.linkedListElemID = new Array(SIZE);
for (var i = 0; i < SIZE; i++)
{
this.linkedListElemID[i]= this.nextIndex++;
}
this.topID = this.nextIndex++;
this.topLabelID = this.nextIndex++;
this.arrayData = new Array(SIZE);
this.top = 0;
this.leftoverLabelID = this.nextIndex++;
this.cmd("CreateLabel", this.topLabelID, "Top", TOP_LABEL_X, TOP_LABEL_Y);
this.cmd("CreateRectangle", this.topID, "", TOP_ELEM_WIDTH, TOP_ELEM_HEIGHT, TOP_POS_X, TOP_POS_Y);
this.cmd("SetNull", this.topID, 1);
this.cmd("CreateLabel", this.leftoverLabelID, "", PUSH_LABEL_X, PUSH_LABEL_Y);
this.animationManager.StartNewAnimation(this.commands);
this.animationManager.skipForward();
this.animationManager.clearHistory();
}
StackLL.prototype.resetLinkedListPositions = function()
{
for (var i = this.top - 1; i >= 0; i--)
{
var nextX = (this.top - 1 - i) % LINKED_LIST_ELEMS_PER_LINE * LINKED_LIST_ELEM_SPACING + LINKED_LIST_START_X;
var nextY = Math.floor((this.top - 1 - i) / LINKED_LIST_ELEMS_PER_LINE) * LINKED_LIST_LINE_SPACING + LINKED_LIST_START_Y;
this.cmd("Move", this.linkedListElemID[i], nextX, nextY);
}
}
StackLL.prototype.reset = function()
{
this.top = 0;
this.nextIndex = this.initialIndex;
}
StackLL.prototype.pushCallback = function(event)
{
if (this.top < SIZE && this.pushField.value != "")
{
var pushVal = this.pushField.value;
this.pushField.value = ""
this.implementAction(this.push.bind(this), pushVal);
}
}
StackLL.prototype.popCallback = function(event)
{
if (this.top > 0)
{
this.implementAction(this.pop.bind(this), "");
}
}
StackLL.prototype.clearCallback = function(event)
{
implementAction(this.clearData.bind(this), "");
}
StackLL.prototype.push = function(elemToPush)
{
this.commands = new Array();
var labPushID = this.nextIndex++;
var labPushValID = this.nextIndex++;
this.arrayData[this.top] = elemToPush;
this.cmd("SetText", this.leftoverLabelID, "");
this.cmd("CreateLinkedList",this.linkedListElemID[this.top], "" ,LINKED_LIST_ELEM_WIDTH, LINKED_LIST_ELEM_HEIGHT,
LINKED_LIST_INSERT_X, LINKED_LIST_INSERT_Y, 0.25, 0, 1, 1);
this.cmd("CreateLabel", labPushID, "Pushing Value: ", PUSH_LABEL_X, PUSH_LABEL_Y);
this.cmd("CreateLabel", labPushValID,elemToPush, PUSH_ELEMENT_X, PUSH_ELEMENT_Y);
this.cmd("Step");
this.cmd("Move", labPushValID, LINKED_LIST_INSERT_X, LINKED_LIST_INSERT_Y);
this.cmd("Step");
this.cmd("SetText", this.linkedListElemID[this.top], elemToPush);
this.cmd("Delete", labPushValID);
if (this.top == 0)
{
this.cmd("SetNull", this.topID, 0);
this.cmd("SetNull", this.linkedListElemID[this.top], 1);
}
else
{
this.cmd("Connect", this.linkedListElemID[this.top], this.linkedListElemID[this.top - 1]);
this.cmd("Step");
this.cmd("Disconnect", this.topID, this.linkedListElemID[this.top-1]);
}
this.cmd("Connect", this.topID, this.linkedListElemID[this.top]);
this.cmd("Step");
this.top = this.top + 1;
this.resetLinkedListPositions();
this.cmd("Delete", labPushID);
this.cmd("Step");
return this.commands;
}
StackLL.prototype.pop = function(ignored)
{
this.commands = new Array();
var labPopID = this.nextIndex++;
var labPopValID = this.nextIndex++;
this.cmd("SetText", this.leftoverLabelID, "");
this.cmd("CreateLabel", labPopID, "Popped Value: ", PUSH_LABEL_X, PUSH_LABEL_Y);
this.cmd("CreateLabel", labPopValID,this.arrayData[this.top - 1], LINKED_LIST_START_X, LINKED_LIST_START_Y);
this.cmd("Move", labPopValID, PUSH_ELEMENT_X, PUSH_ELEMENT_Y);
this.cmd("Step");
this.cmd("Disconnect", this.topID, this.linkedListElemID[this.top - 1]);
if (this.top == 1)
{
this.cmd("SetNull", this.topID, 1);
}
else
{
this.cmd("Connect", this.topID, this.linkedListElemID[this.top-2]);
}
this.cmd("Step");
this.cmd("Delete", this.linkedListElemID[this.top - 1]);
this.top = this.top - 1;
this.resetLinkedListPositions();
this.cmd("Delete", labPopValID)
this.cmd("Delete", labPopID);
this.cmd("SetText", this.leftoverLabelID, "Popped Value: " + this.arrayData[this.top]);
return this.commands;
}
StackLL.prototype.clearAll = function()
{
this.commands = new Array();
for (var i = 0; i < this.top; i++)
{
cmd("Delete", this.linkedListElemID[i]);
}
this.top = 0;
this.cmd("SetNull", this.topID, 1);
return this.commands;
}
var currentAlg;
function init()
{
var animManag = initCanvas();
currentAlg = new StackLL(animManag, canvas.width, canvas.height);
}