-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
247 lines (161 loc) · 4.32 KB
/
index.html
File metadata and controls
247 lines (161 loc) · 4.32 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Stack Implent Using Array</title>
</head>
<body>
<h1>Stack Implementation using an Array<hr></h1>
<div id="demo">
<p id="instruct">Enter value in box and click on any button below to perform whatever action you want to implement on stack</p>
<ul>
<li>push</li>
<li>pop</li>
<li>peek</li>
<li>isEmpty</li>
<li>size</li>
<li>clear</li>
<li>display</li>
</ul>
</div>
<div class="choices">
<button id="but1">push</button>
<button id="but2">pop</button>
<button id="but3">display</button>
<button id="but4">peek</button>
<button id="but5">isEmpty</button>
<button id="but6">size</button>
<button id="but7" >clear</button>
</div>
<div class="visual">
<p id="toread"></p>
<div id="showstack">
</div>
</div>
<div class="show">
<input id="toadd" type="text" placeholder="enter value to push">
</div>
<script>
let choices=document.querySelector(".choices")
let visual=document.querySelector(".visual")
var toread=document.getElementById("toread")
var show=document.querySelector(".show")
var button=document.querySelector("button")
var showstack=document.getElementById("showstack")
//var toadd=document.getElementById ("toadd")
var toadd=document.getElementById("toadd").value
class Stack {
constructor() {
this.items = [];
this.top = -1;
}
}
//instantiate
let stackup = new Stack()
/*code for push*/
stackup.push = function (toadd) {
this.items[this.top] =toadd
this.top += 1
}
/*code for display */
stackup.display = function () {
if (this.top < 0) {
return "Stack is empty"
}
else{
showstack.innerHTML="items on stack are:<br/>"
for (let i =this.top-1; i>-2; i--) {
showstack.innerHTML += this.items[i] + " <br />"
}
}
}
/*code for pop*/
stackup.pop = function () {
if (this.top < 0) {
return "no elements left"
}
else{
var temp= this.items[this.top-1]
//--this.items.length
//return temp
this.top-=1
return temp
}
}
/*code for peek*/
stackup.peek=function(){
if(this.top<0){
toread.innerHTML="stack is empty"
}
else{
var temp=this.items[this.top-1]
return temp
}
}
/*code for isEmpty*/
stackup.isEmpty=function(){
if(this.top<0){
showstack.innerHTML=true
}
else{
showstack.innerHTML=false
}
}
//code for size
stackup.size=function(){
var trav=0
while(trav<=this.top){
trav+=1
}
toread.innerHTML=trav +" "+ "is the size of stack"
}
//for clear
stackup.clear=function(){
this.top=-1
toread.innerHTML="stack is cleared"
}
/*event listeners via delegation*/
choices.addEventListener("click",clicked)
function clicked(e){
if(e.target.matches("button")){
var button=e.target
switch(button){
//for push
case but1:
var toadd=document.getElementById("toadd").value
toread.innerHTML = toadd +" " +" is added to the stack"
stackup.push(toadd)
break;
//for pop
case but2:
toread.innerHTML = stackup.pop() + " is popped from stack"
break;
//for display
case but3:
stackup.display()
break;
//for peek
case but4:
toread.innerHTML=stackup.peek()+" "+"is top element in stack"
break;
//check if empty
case but5:
stackup.isEmpty()
break;
//for size
case but6:
stackup.size()
break;
//for clear
case but7:
stackup.clear()
break;
}
}
}
</script>
</body>
</html>