-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.html
More file actions
152 lines (137 loc) · 4.14 KB
/
Copy pathStack.html
File metadata and controls
152 lines (137 loc) · 4.14 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
<!DOCTYPE html>
<html>
<head>
<title>Stack UI</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
padding: 20px;
}
h1 {
color: #333;
text-align: center;
}
.container {
max-width: 500px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
input[type="text"] {
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 4px;
width: 60%;
}
button {
padding: 10px 20px;
font-size: 16px;
background-color: #4CAF50;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
margin-left: 10px;
}
button:hover {
background-color: #45a049;
}
#stack-contents {
list-style-type: none;
padding: 0;
margin-top: 20px;
}
#stack-contents li {
background-color: #f2f2f2;
padding: 10px;
margin-bottom: 5px;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="container">
<h1>Stack UI</h1>
<input type="text" id="value" placeholder="Enter a value">
<button id="push">Push</button>
<button id="pop">Pop</button>
<button id="peek">Peek</button>
<p>Stack Contents:</p>
<ul id="stack-contents"></ul>
</div>
<script>
$(document).ready(function() {
var stack = [];
// Handle "Push" button click
$('#push').click(function() {
var value = $('#value').val();
stack = push(stack, value);
updateStackContents(stack);
$('#value').val('');
});
// Handle "Pop" button click
$('#pop').click(function() {
stack = pop(stack);
updateStackContents(stack);
});
// Handle "Peek" button click
$('#peek').click(function() {
var top = peek(stack);
if (top !== null) {
alert('Top of the stack: ' + top);
} else {
alert('Stack is empty');
}
});
// Function to update the stack contents in the UI
function updateStackContents(stack) {
var stackContents = $('#stack-contents');
stackContents.empty();
$.each(stack, function(index, value) {
stackContents.append('<li>' + value + '</li>');
});
}
// Implement your Python Stack class functions here
function push(lst, num) {
if (!Array.isArray(lst)) {
throw new TypeError("The first argument needs to be a list");
}
lst.push(num);
return lst;
}
function pop(lst) {
if (!Array.isArray(lst)) {
throw new TypeError("The first argument needs to be a list");
}
if (lst.length === 0) {
return "List is empty";
}
try {
lst.pop();
return lst;
} catch (e) {
return null;
}
}
function peek(lst) {
if (!Array.isArray(lst)) {
throw new TypeError("First argument needs to be a list");
}
if (lst.length === 0) {
return null;
}
try {
return lst[lst.length - 1];
} catch (e) {
return null;
}
}
});
</script>
</body>
</html>