无法使用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 老师祝福语简短100字
16 婆婆生日祝福语简短的
17 娶嫂子的祝福语简短
18 过节文案成语祝福语简短