Topic: Stack
Questions Successfully Completed: 1
1) Implement Stack using Arrays | Easy |
Implementing Stack using Arrays
class MyStack
{
int top;
int arr[] = new int[1000];
MyStack()
{
top = -1;
}
//Function to push an integer into the stack.
void push(int a)
{
if(top!=arr.length-1){
arr[++top] = a;
}
}
//Function to remove an item from top of the stack.
int pop()
{
if(top!=-1){
return arr[top--];
}
return -1;
}
}
/*OUTPUT
3 4
*/
Thank you for reading :)