无法使用JavaScript中的for循环将堆栈的所有元素推入另一个堆栈?
众所周知,堆栈工作在后进先出的原则上。首先,要插入另一个堆栈,您需要pop()第一个堆栈中的所有元素并推入第二个堆栈中。
示例
var myFirstStack=[10,20,30,40,50,60,70];
var mySecondStack=[];
for(;myFirstStack.length;){
mySecondStack.push(myFirstStack.pop());
}
console.log("After popping the all elements from the first stack=");
console.log(myFirstStack);
console.log("After pushing (inserting) all the elements into the second
stack=");
console.log(mySecondStack);要运行上述程序,您需要使用以下命令-
node fileName.js.
在这里,我的文件名为demo189.js。
输出结果
这将产生以下输出-
PS C:\Users\Amit\javascript-code> node demo189.js After popping the all elements from the first stack= [] After pushing (inserting) all the elements into the second stack= [ 70, 60, 50, 40, 30, 20, 10 ]
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短