-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_que.html
More file actions
91 lines (71 loc) · 1.87 KB
/
Copy pathstack_que.html
File metadata and controls
91 lines (71 loc) · 1.87 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Stack and que</title>
</head>
<body>
<h1>Stack and Queue</h1>
<script>
// let name = "Prakash Sahu";
// let stack = "";
// for (let i = name.length - 1; i >= 0; i--) {
// stack += name[i];
// }
let name = "Prakash";
let stack = [];
// Push characters onto the stack
for (let i = 0; i < name.length; i++) {
stack.push(name[i]); // Stack stores characters in order
}
// Pop characters off the stack to reverse the order
name = "";
while (stack.length > 0) {
name += stack.pop(); // LIFO: Last character out first
}
let array = [];
let currentSize = array.length;
let currentValue;
function addValue(value) {
array[currentSize] = value;
currentSize++;
}
addValue(40);
addValue(60);
addValue(70);
function removeValue() {
if (currentSize === 0) {
return;
}
currentSize--;
currentValue = array[currentSize];
array.length = currentSize;
return currentValue;
}
removeValue();
let users = ["Prakash", "Sahu", "Raj", "Kumar"];
let userCount = users.length;
// let userValue;
function pushMethod(value) {
users[userCount] = value;
userCount++;
}
pushMethod("Rajesh");
function popMethod() {
if (userCount === 0) {
return;
}
userCount--;
let userValue = users[userCount];
users.length = userCount;
return userValue;
}
// removeValue();
// console.log(array);
popMethod();
// console.log(users);
// Reverse String
</script>
</body>
</html>