Peek Operation
ABRAHAM
c_cpp
a year ago
1.5 kB
2
Indexable
Never
#include<stdio.h> #include<stdlib.h> struct stack{ int size ; int top; int * arr; }; int isEmpty(struct stack* ptr){ if(ptr->top == -1){ return 1; } else{ return 0; } } int isFull(struct stack* ptr){ if(ptr->top == ptr->size - 1){ return 1; } else{ return 0; } } void push(struct stack* ptr, int val){ if(isFull(ptr)){ printf("Stack Overflow! Cannot push %d to the stack\n", val); } else{ ptr->top++; ptr->arr[ptr->top] = val; } } int pop(struct stack* ptr){ if(isEmpty(ptr)){ printf("Stack Underflow! Cannot pop from the stack\n"); return -1; } else{ int val = ptr->arr[ptr->top]; ptr->top--; return val; } } int peek(struct stack* sp, int i){ int arrayInd = sp->top -i + 1; if(arrayInd < 0){ printf("Not a valid position for the stack\n"); return -1; } else{ return sp->arr[arrayInd]; } } int main(){ struct stack *sp = (struct stack *) malloc(sizeof(struct stack)); sp->size = 50; sp->top = -1; sp->arr = (int *) malloc(sp->size * sizeof(int)); printf("Stack has been created successfully\n"); printf("Before pushing, Full: %d\n", isFull(sp)); printf("Before pushing, Empty: %d\n", isEmpty(sp)); return 0; }