原文:

给定一个堆栈,任务是从底部到顶部打印堆栈的元素,这样元素仍然存在于堆栈中,而它们在堆栈中的顺序不会改变。

示例:

input : 
|   4    |
|   3    |
|   2    |
|   1    |
|________|
output :1 2 3 4

方法 1(递归):思路是弹出栈的元素,调用递归函数 printstack。一旦堆栈变空,开始打印最后弹出的元素,最后弹出的元素是最底部的元素。因此,元素将从下往上打印。现在推回被打印的元素,这将保持元素在堆栈中的顺序。

下面是上述方法的实现:

c

// c   program to print the elements of a
// stack from bottom to top
#include 
using namespace std;
// recursive function to print stack elements
// from bottom to top without changing
// their order
void printstack(stack s)
{
    // if stack is empty then return
    if (s.empty())
        return;
    int x = s.top();
    // pop the top element of the stack
    s.pop();
    // recursively call the function printstack
    printstack(s);
    // print the stack element starting
    // from the bottom
    cout << x << " ";
    // push the same element onto the stack
    // to preserve the order
    s.push(x);
}
// driver code
int main()
{
    // stack s
    stack s;
    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);
    printstack(s);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print the elements of a
// stack from bottom to top
import java.util.*;
class gfg
{
// recursive function to print stack elements
// from bottom to top without changing
// their order
static void printstack(stack s)
{
    // if stack is empty then return
    if (s.isempty())
        return;
    int x = s.peek();
    // pop the top element of the stack
    s.pop();
    // recursively call the function printstack
    printstack(s);
    // print the stack element starting
    // from the bottom
    system.out.print(x   " ");
    // push the same element onto the stack
    // to preserve the order
    s.push(x);
}
// driver code
public static void main(string[] args)
{
    // stack s
    stack s = new stack ();
    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);
    printstack(s);
}
}
// this code is contributed by prerna saini.

python 3

# python3 program to print the elements of a
# stack from bottom to top
# stack class with all functionality of a stack
import sys
class stack:
    def __init__(self):
        self.s = []
    def push(self, data):
        self.s.append(data)
    def pop(self):
        return self.s.pop()
    def peek(self):
        return self.s[-1]
    def count(self):
        return len(self.s)
# recursive function to print stack elements
# from bottom to top without changing
# their order
def printstack(s):
    # if stack is empty then simply return
    if s.count() == 0:
        return
    x = s.peek()
    # pop top most element of the stack
    s.pop()
    # recursively call the function printstack
    printstack(s)
    # print the stack element starting
    # from the bottom
    print("{} ".format(x), end = "")
    # push the same element onto the stack
    # to preserve the order
    s.push(x)
# driver code
if __name__=='__main__':
    s=stack()
    s.push(1)
    s.push(2)
    s.push(3)
    s.push(4)
    printstack(s)
# this code is contributed by vikas kumar

c

// c# program to print the elements of a
// stack from bottom to top
using system;
using system.collections.generic;
class gfg
{
// recursive function to print stack elements
// from bottom to top without changing
// their order
static void printstack(stack s)
{
    // if stack is empty then return
    if (s.count == 0)
        return;
    int x = s.peek();
    // pop the top element of the stack
    s.pop();
    // recursively call the function printstack
    printstack(s);
    // print the stack element starting
    // from the bottom
    console.write(x   " ");
    // push the same element onto the stack
    // to preserve the order
    s.push(x);
}
// driver code
public static void main()
{
    // stack s
    stack s = new stack ();
    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);
    printstack(s);
}
}
/* this code contributed by princiraj1992 */

java 描述语言


output: 

1 2 3 4

时间复杂度:o(n) t3】辅助空间: o(n)

方法 2(使用另一个堆栈):想法是将每个元素推入另一个临时堆栈,然后打印临时堆栈的元素。

c

// c   program to print the elements of a
// stack from bottom to top
#include 
using namespace std;
// recursive function to print stack elements
// from bottom to top without changing
// their order
void printstack(stack s)
{
    stack temp;
    while (s.empty() == false)
    {
        temp.push(s.top());
        s.pop();
    }  
    while (temp.empty() == false)
    {
        int t = temp.top();
        cout << t << " ";
        temp.pop();
        // to restore contents of
        // the original stack.
        s.push(t); 
    }
}
// driver code
int main()
{
    // stack s
    stack s;
    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);
    printstack(s);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print the
// elements of a stack from
// bottom to top
import java.util.*;
class main{
// recursive function to print
// stack elements from bottom
// to top without changing
// their order
public static void printstack(stack s)
{
  stack temp = new stack();
  while (s.empty() == false)
  {
    temp.push(s.peek());
    s.pop();
  }  
  while (temp.empty() == false)
  {
    int t = temp.peek();
    system.out.print(t   " ");
    temp.pop();
    // to restore contents of
    // the original stack.
    s.push(t); 
  }
}
// driver code
public static void main(string[] args)
{
  // stack s
  stack s = new stack();
  s.push(1);
  s.push(2);
  s.push(3);
  s.push(4);
  printstack(s);
}
}
// this code is contributed by divyeshrabadiya07

python 3

# python3 program to print the elements of a
# stack from bottom to top
# stack class with all functionality of a stack
import sys
class stack:
    def __init__(self):
        self.s = []
    def push(self, data):
        self.s.append(data)
    def pop(self):
        return self.s.pop()
    def peek(self):
        return self.s[-1]
    def count(self):
        return len(self.s)
# recursive function to print stack elements
# from bottom to top without changing
# their order
def printstack(s):
    temp = stack()
    while(s.count() > 0):
        temp.push(s.peek())
        s.pop()
    while(temp.count() > 0):
        t = temp.peek()
        print("{} " . format(temp.peek()), end = "")
        temp.pop()
        # restore the contents of original stack
        s.push(t)
# driver code
if __name__=='__main__':
    s = stack()
    s.push(1)
    s.push(2)
    s.push(3)
    s.push(4)
    printstack(s)
# this code is contributed by vikash kumar 37

c

// c# program to print the elements of
// a stack from bottom to top
using system;
using system.collections;
class gfg{
// recursive function to print stack
// elements from bottom to top without
// changing their order
static void printstack(stack s)
{
    stack temp = new stack();
    while (s.count != 0)
    {
        temp.push(s.peek());
        s.pop();
    }  
    while (temp.count != 0)
    {
        int t = (int)temp.peek();
        console.write(t   " ");
        temp.pop();
        // to restore contents of
        // the original stack.
        s.push(t); 
    }
}
// driver code
public static void main(string[] args)
{
    // stack s
    stack s = new stack();
    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);
    printstack(s);
}
}
// this code is contributed by rutvik_56

java 描述语言


output: 

1 2 3 4

时间复杂度:o(n) t3】辅助空间: o(n)