forked from sumn2u/learn-javascript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_element
More file actions
56 lines (43 loc) · 868 Bytes
/
insert_element
File metadata and controls
56 lines (43 loc) · 868 Bytes
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
<script>
// JavaScript program for the above approach
// Function to insert an element
// at the bottom of a given stack
function insertToBottom(S, N)
{
// Temporary stack
var temp = [];
// Iterate until S becomes empty
while (S.length!=0) {
// Push the top element of S
// into the stack temp
temp.push(S[S.length-1]);
// Pop the top element of S
S.pop();
}
// Push N into the stack S
S.push(N);
// Iterate until temp becomes empty
while (temp.length!=0) {
// Push the top element of
// temp into the stack S
S.push(temp[temp.length-1]);
// Pop the top element of temp
temp.pop();
}
// Print the elements of S
while (S.length!=0) {
document.write( S[S.length-1] + " ");
S.pop();
}
}
// Driver Code
// Input
var S = [];
S.push(5);
S.push(4);
S.push(3);
S.push(2);
S.push(1);
var N = 7;
insertToBottom(S, N);
</script>