无法使用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 出阁宴祝福语姐姐简短