Monday, June 21, 2021
Home coding Write a program to implement stack using array

Write a program to implement stack using array

Given a array ,we have to implement stack using array.

Stack has two operation:

1-push operation.(push the element)

2-pop operation.(pop the element)

Logic:

STEP 1:implement push operation.
STEP 2:implement pop operation.

Code

void push(int x)
{
    
    a[++top]=x;
    cout<<x<<" is pushed into stack"<<endl; 
}
 
int Stack::pop()
{
    
        int x = a[top--];
        return x;
    
}

Ouput:

pushed
popped

Time Complexity:O(N)

Space Complexity:O(1)

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments