Stack designer | Practice | GeeksforGeeks

You are given an array arr of size N. You need to push the elements of the array into a stack and then print them while popping. 
Example 1: 
Input:
n = 5
arr = {1 2 3 4 5}
Output:
5 4 3 2 1

Example 2: 
Input: 
n = 7
arr = {1 6 43 1 2 0 5}
Output: 
5 0 2 1 43 6 1
 
Your Task:
Since this is a function problem, you don't need to take any input. Just complete the provided functions _push() and _pop().
Constraints:
1 <= Ai <= 107
0
mayank180919992 weeks ago
stack<int>_push(int arr[],int n)
{
   stack<int>s;
   for(int i=0;i<n;i++){
       s.push(arr[i]);
   }
   return s;
}
void _pop(stack<int> s)
{
    while(!s.empty()){
        cout<<s.top()<<" ";
        s.pop();
    }
}
0
0niharika22 months ago
stack<int>_push(int arr[],int n){  stack<int> s;  for(int i=0; i<n; i++)       s.push(arr[i]);   return s;}
void _pop(stack<int> s){   while(!s.empty())   {       cout << s.top() << " ";       s.pop();   }}
0
surabhichoubey552 months ago
stack<int>_push(int arr[],int n){  //return a stack with all elements of arr inserted in it  stack<int>s;  for(int i = 0;i<n;i++)  {      s.push(arr[i]);  }  return s;  }
/* _pop function to print elements of the stack  remove as well*/void _pop(stack<int> s){   //print top and pop for each element until it becomes empty   while(!s.empty())   {       int top = s.top();       s.pop();       cout<<top<<" ";   }}
0
princejee20192 months ago
c++ code !!
stack<int>_push(int arr[],int n){  //return a stack with all elements of arr inserted in it  stack<int>s;  for(int i =0;i<n;i++){      s.push(arr[i]);  }  return s;}
/* _pop function to print elements of the stack  remove as well*/void _pop(stack<int> s){   //print top and pop for each element until it becomes empty   while(!s.empty()){       cout<<s.top()<<" ";       s.pop();   }   } 
+1
ayushvats9522 months ago
stack<int>_push(int arr[],int n){  //return a stack with all elements of arr inserted in it  stack<int> s;   for(int i=0; i<n; i++)  s.push(arr[i]);   return s;}
 
void _pop(stack<int> s){   //print top and pop for each element until it becomes empty   while(!s.empty()){       cout<<s.top()<<" ";       s.pop();          }   }
0
madhukartemba3 months ago
SIMPLE JAVA SOLUTION:
 
public static Stack<Integer>_push(ArrayList<Integer> arr,int n)
{
    //Your code here
    Stack<Integer> st = new Stack<>();
    for(int i=0; i<n; i++)
    {
        st.push(arr.get(i));
    }
    
    return st;
}

public static void _pop(Stack<Integer>s)
{
    while(s.isEmpty()==false)
    {
        System.out.print(s.pop() + " ");
    }
}
0
prashspp3 months ago
from collections import dequedef _push(arr):   stack=deque()   for i in arr:       stack.append(i)   return stackdef _pop(stack):   #print top and pop for each element until it becomes empty     while(len(stack)>0):         print(stack.pop(),end=' ')
the code is correct but the test cases are not passing i dont know what is th error in the code
 
0
ayushnautiyal11103 months ago
stack<int>_push(int arr[],int n){   stack<int>s;  for(int i=0;i<n;i++){      s.push(arr[i]);  }  return s;}
/* _pop function to print elements of the stack  remove as well*/void _pop(stack<int> s){   while(s.empty()==false){       cout<<s.top()<<" ";       s.pop();   }   }
0
s87646 months ago
@mod  @geeksforgeeks  please active python for this problem...
0
imranwahid6 months ago
Easy C++ solution
https://ide.geeksforgeeks.org/RFnAhbMjkK
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.