无法使用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 教师闺蜜祝福语简短