原文:

给定s,任务是,以使元素仍然存在于栈中,而不改变它们的顺序。

示例

输入s = {2, 3, 4, 5}

输出5 4 3 2

输入s = {3, 3, 2, 2}

输出2 2 3 3

:请按照以下步骤解决问题:

  1. 创建一个具有作为参数的递归函数。

  2. 添加基本​​条件,如果为空,则从函数返回。

  3. 否则,将顶部元素存储在某个变量x中,然后将其删除。

  4. 打印x,调用递归函数并在其中传递相同的栈。

  5. 将存储的x推回栈。

下面是上述方法的实现:

c

// c   program for the above approach
#include 
using namespace std;
// function to print stack elements
// from top to bottom with the
// order of elements unaltered
void printstack(stack s)
{
    // if stack is empty
    if (s.empty())
        return;
// extract top of the stack
    int x = s.top();
    // pop the top element
    s.pop();
    // print the current top
    // of the stack i.e., x
    cout << x << ' ';
    // proceed to print
// remaining stack
    printstack(s);
    // push the element back
    s.push(x);
}
// driver code
int main()
{
    stack s;
    // given stack s
    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);
    // function call
    printstack(s);
    return 0;
}

java

// java program for the above approach 
import java.util.*;
class gfg{
// function to print stack elements 
// from top to bottom with the 
// order of elements unaltered 
public static void printstack(stack s) 
{ 
    // if stack is empty 
    if (s.empty()) 
        return; 
    // extract top of the stack 
    int x = s.peek(); 
    // pop the top element 
    s.pop(); 
    // print the current top 
    // of the stack i.e., x 
    system.out.print(x   " ");
    // proceed to print 
    // remaining stack 
    printstack(s); 
    // push the element back 
    s.push(x); 
} 
// driver code
public static void main(string[] args) 
{
    stack s = new stack(); 
    // given stack s 
    s.push(1); 
    s.push(2); 
    s.push(3); 
    s.push(4); 
    // function call 
    printstack(s); 
}
}
// this code is contributed divyeshrabadiya07

python3

# python3 program for the 
# above approach
from queue import lifoqueue
# function to prstack elements
# from top to bottom with the
# order of elements unaltered
def printstack(s):
    # if stack is empty
    if (s.empty()):
        return;
    # extract top of the 
    # stack
    x = s.get();
    # pop the top element
    #s.pop();
    # print current top
    # of the stack i.e., x
    print(x, end = " ");
    # proceed to print
    # remaining stack
    printstack(s);
    # push the element 
    # back
    s.put(x);
# driver code
if __name__ == '__main__':
    s = lifoqueue();
    # given stack s
    s.put(1);
    s.put(2);
    s.put(3);
    s.put(4);
    # function call
    printstack(s);
# this code is contributed by amit katiyar

c

// c# program for 
// the above approach 
using system;
using system.collections.generic;
class gfg{
// function to print stack elements 
// from top to bottom with the 
// order of elements unaltered 
public static void printstack(stack s) 
{ 
  // if stack is empty 
  if (s.count == 0) 
    return; 
  // extract top of the stack 
  int x = s.peek(); 
  // pop the top element 
  s.pop(); 
  // print the current top 
  // of the stack i.e., x 
  console.write(x   " ");
  // proceed to print 
  // remaining stack 
  printstack(s); 
  // push the element back 
  s.push(x); 
} 
// driver code
public static void main(string[] args) 
{
  stack s = new stack(); 
  // given stack s 
  s.push(1); 
  s.push(2); 
  s.push(3); 
  s.push(4); 
  // function call 
  printstack(s); 
}
}
// this code is contributed by rajput-ji

输出

4 3 2 1 

时间复杂度o(n),其中n是给定栈中元素的数量。

辅助空间o(n)

:此方法讨论解决表示法的问题的pg电子试玩链接的解决方案。 步骤如下:

  1. 将给定栈中的顶部元素推入。

  2. 打印的顶部元素。

  3. 从给定的栈中弹出顶部元素。

  4. 按顺序重复上述步骤,直到给定的栈为空。

下面是上述方法的实现:

java

// java program for the above approach
import static java.lang.system.exit;
// create stack using linked list
class stackusinglinkedlist {
    // a linked list node
    private class node {
        int data;
        node link;
    }
    // reference variable
    node top;
    // constructor
    stackusinglinkedlist()
    {
        this.top = null;
    }
    // function to add an element x
    // in the stack by inserting
    // at the beginning of ll
    public void push(int x)
    {
        // create new node temp
        node temp = new node();
        // if stack is full
        if (temp == null) {
            system.out.print("\nheap overflow");
            return;
        }
        // initialize data into temp
        temp.data = x;
        // reference into temp link
        temp.link = top;
        // update top reference
        top = temp;
    }
    // function to check if the stack
    // is empty or not
    public boolean isempty()
    {
        return top == null;
    }
    // function to return top element
    // in a stack
    public int peek()
    {
        // check for empty stack
        if (!isempty()) {
            // return the top data
            return top.data;
        }
        // otherwise stack is empty
        else {
            system.out.println("stack is empty");
            return -1;
        }
    }
    // function to pop top element from
    // the stack by removing element
    // at the beginning
    public void pop()
    {
        // check for stack underflow
        if (top == null) {
            system.out.print("\nstack underflow");
            return;
        }
        // update the top pointer to
        // point to the next node
        top = (top).link;
    }
    // function to print the elements and
    // restore the stack
    public static void
    displaystack(stackusinglinkedlist s)
    {
        // create another stack
        stackusinglinkedlist s1
            = new stackusinglinkedlist();
        // until stack is empty
        while (!s.isempty()) {
            s1.push(s.peek());
            // print the element
            system.out.print(s1.peek()
                               " ");
            s.pop();
        }
    }
}
// driver code
public class gfg {
    // driver code
    public static void main(string[] args)
    {
        // create object of class
        stackusinglinkedlist obj
            = new stackusinglinkedlist();
        // insert stack value
        obj.push(1);
        obj.push(2);
        obj.push(3);
        obj.push(4);
        // function call
        obj.displaystack(obj);
    }
}

c

// c# program for the above approach
using system;
// create stack using linked list
class stackusinglinkedlist{
// a linked list node
public class node 
{
    public int data;
    public node link;
}
// reference variable
node top;
// constructor
public stackusinglinkedlist()
{
    this.top = null;
}
// function to add an element x
// in the stack by inserting
// at the beginning of ll
public void push(int x)
{
    // create new node temp
    node temp = new node();
    // if stack is full
    if (temp == null)
    {
        console.write("\nheap overflow");
        return;
    }
    // initialize data into temp
    temp.data = x;
    // reference into temp link
    temp.link = top;
    // update top reference
    top = temp;
}
// function to check if the stack
// is empty or not
public bool isempty()
{
    return top == null;
}
// function to return top element
// in a stack
public int peek()
{
    // check for empty stack
    if (isempty() != true)
    {
        // return the top data
        return top.data;
    }
    // otherwise stack is empty
    else
    {
        console.writeline("stack is empty");
        return -1;
    }
}
// function to pop top element from
// the stack by removing element
// at the beginning
public void pop()
{
    // check for stack underflow
    if (top == null)
    {
        console.write("\nstack underflow");
        return;
    }
    // update the top pointer to
    // point to the next node
    top = (top).link;
}
// function to print the elements and
// restore the stack
public void displaystack(stackusinglinkedlist s)
{
    // create another stack
    stackusinglinkedlist s1 = new stackusinglinkedlist();
    // until stack is empty
    while (s.isempty() != true) 
    {
        s1.push(s.peek());
        // print the element
        console.write(s1.peek()   " ");
        s.pop();
    }
}
}
class gfg{
// driver code
public static void main(string[] args)
{
    // create object of class
    stackusinglinkedlist obj = new stackusinglinkedlist();
    // insert stack value
    obj.push(1);
    obj.push(2);
    obj.push(3);
    obj.push(4);
    // function call
    obj.displaystack(obj);
}
}
// this code is contributed by amit katiyar

输出

4 3 2 1 

时间复杂度o(n),其中n是给定栈中元素的数量。

辅助空间o(n)

:此方法讨论实现中问题的pg电子试玩链接的解决方案。 步骤如下:

  1. 将给定栈中的顶部元素推入。

  2. 打印的顶部元素。

  3. 从给定的栈中弹出顶部元素。

  4. 按顺序重复上述步骤,直到给定的栈为空。

下面是上述方法的实现:

java

// java program for the above approach
class stack {
    static final int max = 1000;
    // stores the index where element
    // needs to be inserted
    int top;
    // array to store the stack elements
    int a[] = new int[max];
    // function that check whether stack
    // is empty or not
    boolean isempty()
    {
        return (top < 0);
    }
    // constructor
    stack() { top = -1; }
    // function that pushes the element
    // to the top of the stack
    boolean push(int x)
    {
        // if stack is full
        if (top >= (max - 1)) {
            system.out.println(
                "stack overflow");
            return false;
        }
        // otherwise insert element x
        else {
            a[  top] = x;
            return true;
        }
    }
    // function that removes the top
    // element from the stack
    int pop()
    {
        // if stack is empty
        if (top < 0) {
            system.out.println(
                "stack underflow");
            return 0;
        }
        // otherwise remove element
        else {
            int x = a[top--];
            return x;
        }
    }
    // function to get the top element
    int peek()
    {
        // if stack is empty
        if (top < 0) {
            system.out.println(
                "stack underflow");
            return 0;
        }
        // otherwise remove element
        else {
            int x = a[top];
            return x;
        }
    }
    // function to print the elements
    // and restore the stack
    static void displaystack(stack s)
    {
        // create another stack
        stack s1 = new stack();
        // until stack is empty
        while (!s.isempty()) {
            s1.push(s.peek());
            // print the element
            system.out.print(s1.peek()
                               " ");
            s.pop();
        }
    }
}
// driver code
class main {
    // driver code
    public static void main(string args[])
    {
        stack s = new stack();
        // given stack
        s.push(1);
        s.push(2);
        s.push(3);
        s.push(4);
        // function call
        s.displaystack(s);
    }
}

c

// c# program for 
// the above approach
using system;
class stack{
static int max = 1000;
// stores the index where 
// element needs to be inserted
int top;
// array to store the 
// stack elements
int []a = new int[max];
// function that check 
// whether stack
// is empty or not
bool isempty()
{
  return (top < 0);
}
// constructor
public stack() 
{ 
  top = -1; 
}
// function that pushes 
// the element to the 
// top of the stack
public bool push(int x)
{
  // if stack is full
  if (top >= (max - 1)) 
  {
    console.writeline("stack overflow");
    return false;
  }
  // otherwise insert element x
  else
  {
    a[  top] = x;
    return true;
  }
}
// function that removes the top
// element from the stack
public int pop()
{
  // if stack is empty
  if (top < 0) 
  {
    console.writeline("stack underflow");
    return 0;
  }
  // otherwise remove element
  else
  {
    int x = a[top--];
    return x;
  }
}
// function to get the top element
public int peek()
{
  // if stack is empty
  if (top < 0) 
  {
    console.writeline("stack underflow");
    return 0;
  }
  // otherwise remove element
  else
  {
    int x = a[top];
    return x;
  }
}
// function to print the elements
// and restore the stack
public void displaystack(stack s)
{
  // create another stack
  stack s1 = new stack();
  // until stack is empty
  while (!s.isempty()) 
  {
    s1.push(s.peek());
    // print the element
    console.write(s1.peek()   " ");
    s.pop();
  }
}
}
class gfg{
// driver code
public static void main(string []args)
{
  stack s = new stack();
  // given stack
  s.push(1);
  s.push(2);
  s.push(3);
  s.push(4);
  // function call
  s.displaystack(s);
}
}
// this code is contributed by 29ajaykumar

输出

4 3 2 1 

时间复杂度o(n),其中n是给定栈中元素的数量。

辅助空间o(n)



如果您喜欢 geeksforgeeks 并希望做出贡献,则还可以使用 tribution.geeksforgeeks.org 撰写文章,或将您的文章邮寄至 tribution@geeksforgeeks.org。 查看您的文章出现在 geeksforgeeks pg电子试玩链接主页上,并帮助其他 geeks。

如果您发现任何不正确的地方,请单击下面的“改进文章”按钮,以改进本文。