Stacktop and Stackbottam
Stacktop and Stackbottam
stackBottom:
This operation is responsible for returning the bottommost
element in a stack, which intuitively, is the element at index 0.
One thing one must observe here is that both these operations
happen to work in a constant runtime, that is O(1). Because we
are just accessing an element at an index, and that works in a
constant time in an array.
return sp->arr[sp->top];
return sp->arr[0];
#include<stdlib.h>
struct stack{
int size ;
int top;
int * arr;
};
if(ptr->top == -1){
return 1;
else{
return 0;
return 1;
}
else{
return 0;
if(isFull(ptr)){
else{
ptr->top++;
ptr->arr[ptr->top] = val;
if(isEmpty(ptr)){
return -1;
else{
ptr->top--;
return val;
return -1;
else{
return sp->arr[arrayInd];
return sp->arr[sp->top];
return sp->arr[0];
int main(){
sp->size = 50;
sp->top = -1;
push(sp, 1);
push(sp, 23);
push(sp, 99);
push(sp, 75);
push(sp, 3);
push(sp, 64);
push(sp, 57);
push(sp, 46);
push(sp, 89);
push(sp, 6);
push(sp, 5);
push(sp, 75);
// {
// }
return 0;