Monday, February 1, 2021

Implement Stack using array

 In this post we have to complete the push(), pop() methods using an array.

Hint: 

1. For push increase the top and assign arr[top]= element to be pushed

Before pushing an element, check the overflow condition. For pushing element, the array

void push(int element)

{ //check overflow condition

if(top>=size_of_array) cout<<"overflow condition"<<endl;

else 

{ top++; stack[top]=element;}

 

For pop, return top element and decrement top. Before popping an element, check for underflow conditions. 

void pop(int element)

{ //check underflow condition

if(top<=-1) {cout<<"underflow condtion"<<endl;}

else {cout<<stack[top]<<endl; top--;}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home