Repository navigation
Expand file tree
/
Copy pathsq.js
More file actions
96 lines (76 loc) · 2.26 KB
/
Copy pathsq.js
File metadata and controls
96 lines (76 loc) · 2.26 KB
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
$(function() {
var dataStructureType = "";
$("#dataEntry").hide();
//runs if the user wants to visualize a stack
$("#stackButton").on("click", function() {
dataStructureType = "stack";
$("#queueButton").hide();
$("#stackButton").hide();
$("#title").html("<center>Visualization of a stack data structure<center>");
$("#dataEntry").show();
stack();
});
//runs if the user wants to visualize a queue
$("#queueButton").on("click", function() {
dataStructureType = "queue";
$("#stackButton").hide();
$("#queueButton").hide();
$("#title").html("<center>Visualization of a queue data structure<center>");
$("#dataEntry").show();
queue();
});
function stack() {
$("#selection").html("<button id='push'>push (add)</button><button id='pop'>pop (remove)</button>");
//listens for a click on the push button
$("#push").on("click", function(){
//reads in entry from textbox
$dataEntry = $("#textBox").val();
if ($dataEntry === "") {
$("#error").html("<em>Don't you want to type something?</em>");
}
else {
$("#error").html("");
pushStack($dataEntry);
$("#textBox").val("");
}
});
$("#pop").on("click", function(){
popStack();
});
}
function queue() {
//adds enqueue and dequeue buttons
$("#selection").html("<button id='enqueue'>enqueue (add)</button><button id='dequeue'>dequeue (remove)</button>");
//listens for a click on the enqueue button
$("#enqueue").on("click", function(){
console.log("hello");
//reads in entry from textbox
$dataEntry = $("#textBox").val();
if ($dataEntry === "") {
$("#error").html("<em>Don't you want to type something?</em>");
}
else {
$("#error").html("");
enqueue($dataEntry);
$("#textBox").val("");
}
});
$("#dequeue").on("click", function(){
dequeue();
});
}
function pushStack(text) {
$("ul").prepend("<li id='dataValue'>"+text+"</li>");
}
function popStack(text) {
$removeItem = $("li").filter(":first-child");
$removeItem.slideUp("normal", function(){$(this).remove();});
}
function enqueue(text) {
$("ul").prepend("<li id='dataValue'>"+text+"</li>");
}
function dequeue() {
$removeItem = $("li").filter(":last-child");
$removeItem.slideUp("normal", function(){$(this).remove();});
}
});