Stack implementation using array in C
Stack has following operations :
push // To add value at top of stack
pop // To remove value from top of stack
isstack_empty // To check whether stack is empty or not
isstack_full // To check whether stack is full or not
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/* Integer Stack implementation using array , Code by codingstreet.com */ #include<stdio.h> int push(int *stack,int val,int *top,int *size); int pop(int *stack,int *top); int isstack_empty(int *top); int isstack_full(int *top,int *size); int isstack_empty(int *top){ if((*top)==0) return 1; return 0; } int isstack_full(int *top,int *size){ if((*top)==(*size)-1) return 1; return 0; } int push(int *stack,int val,int *top,int *size){ if(isstack_full(top,size)){ return 0; } stack[(*top)++]=val; return 1; } int pop(int *stack,int *top){ if(isstack_empty(top)){ return -1; } else return stack[--(*top)]; } int main(){ int sstack[10],ssize=10,stop=0; push(sstack,10,&stop,&ssize); push(sstack,101,&stop,&ssize); printf(" %d ",pop(sstack,&stop)); printf(" %d ",pop(sstack,&stop)); printf("\n Thank you for using codingstreet.com 's Datastructure Solutions "); return 0; } |
Stack implementation using array in C