Implement stack using array | Practice | GeeksforGeeks

Write a program to implement a Stack using Array. Your task is to use the class as shown in the comments in the code editor and complete the functions push() and pop() to implement a stack. 
Example 1:
Input: 
push(2)
push(3)
pop()
push(4) 
pop()
Output: 3, 4
Explanation: 
push(2)    the stack will be {2}
push(3)    the stack will be {2 3}
pop()      poped element will be 3,
           the stack will be {2}
push(4)    the stack will be {2 4}
pop()      poped element will be 4
Example 2:
Input: 
pop()
push(4)
push(5)
pop()
Output: -1, 5

Your Task:
You are required to complete two methods push() and pop(). The push() method takes one argument, an integer 'x' to be pushed into the stack and pop() which returns an integer present at the top and popped out from the stack. If the stack is empty then return -1 from the pop() method.
Expected Time Complexity : O(1) for both push() and pop().
Expected Auixilliary Space : O(1) for both push() and pop().
Constraints:
1 <= Q <= 100
1 <= x <= 100
-2
harshscode5 days ago
void MyStack :: push(int x){  top++;   arr[top]=x;   }int MyStack :: pop(){      if(top==-1)   return -1;      int x=arr[top];   top--;   return x;  }
0
hharshit81182 weeks ago
void MyStack :: push(int x){   if(top<999){       top++;       arr[top] = x;   }}
//Function to remove an item from top of the stack.int MyStack :: pop(){   if(top == -1){       return -1;   }     int x = arr[top];   top--;   return x;}
-3
tasyamyadav422 weeks ago
System.out.println ("Hello World suuuuuuuuuuuuuuuu");
-2
zerefkhan2 weeks ago
C++ code
Total Time taken : 0.01/1.12
 
void MyStack :: push(int x)
{
    arr[++top] = x;
}

//Function to remove an item from top of the stack.
int MyStack :: pop()
{
    if(top == -1) return -1;
    return arr[top--];
}
+1
tarunkanade4 weeks ago
0.2/1.3 
//Function to push an integer into the stack.
    void push(int a)
	{
	    // Your code here
	    top++;
	    arr[top] = a;
	} 
	
    //Function to remove an item from top of the stack.
	int pop()
	{
        // Your code here
        if(top == -1){
            return -1;
        }
        
        return arr[top--];
	}
0
atif836141 month ago
stack using array
java solution :
void push(int elem){
if(top==arr.length){
return;
}
top++;
arr[top]=elem;
}
//  pop opreation :
int pop(){
if(top==-1){
return -1;
}
int tempdata=arr[top];
top --;
return tempdata;
}
0
ruchitchudasama1231 month ago
void MyStack :: push(int x)
{
    top++;
    arr[top]=x;
}

//Function to remove an item from top of the stack.
int MyStack :: pop()
{
    if(top==-1)return top;
    int x=arr[top];
    top--;
    return x;
}
0
sayantankuila1 month ago
C solution
void push(struct Stack* stack, int item){   //code here   if(stack->top == stack->capacity-1){       return;   }   stack->top++;   stack->array[stack->top] = item;}
//Function to remove an item from top of the stack.int pop(struct Stack* stack){   //code here   if(stack->top == -1){       return -1;          }   int val = stack->array[stack->top];   stack->top--;   return val;   }
0
adarsh471111 month ago
C solution:
 
void push(struct Stack* stack, int item){   if(stack->top == stack->capacity-1)   return;      stack->top++;   stack->array[stack->top]=item;}
//Function to remove an item from top of the stack.int pop(struct Stack* stack){   if(stack->top == -1)   return -1;      stack->top--;   return stack->array[stack->top+1];}
0
0niharika21 month ago
void MyStack :: push(int x){   arr[top+1] = x;   top = top + 1;}
int MyStack :: pop(){   if(top==-1)       return top;   int x = arr[top];   top = top - 1;   return x;}
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.