fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct stack{
  5. int size;
  6. int top;
  7.  
  8. int *arr;
  9. };
  10. int isempty(struct stack*ptr){
  11. if(ptr->top==-1)
  12. {
  13. return 1;
  14. }
  15. else{
  16. return 0;
  17. }
  18. };
  19. int isfull(struct stack*ptr){
  20. if(ptr->top==ptr->size-1)
  21. {
  22. return 1;
  23. }
  24. else{
  25. return 0;
  26. }
  27. };
  28. void push(struct stack*ptr,int n){
  29. if(ptr->top==ptr->size-1){
  30. printf("stack is overflow");
  31. }
  32. else{
  33. ptr->top++;
  34. ptr->arr[ptr->top]=n;
  35. }
  36. };
  37. int pop(struct stack*ptr)
  38. {
  39. if(ptr->top==-1){
  40. printf("stack is underflow");
  41. }
  42. else{
  43.  
  44. int n= ptr->arr[ptr->top];
  45. ptr->top--;
  46. return n;
  47. }
  48. };
  49.  
  50. int main(){
  51. struct stack*sp=(struct stack*)malloc(sizeof(struct stack));
  52. sp->size=8;
  53. sp->top=-1;
  54.  
  55. struct stack *arr=(struct stack*)malloc(sp->size*sizeof(int));
  56. printf(" before pushing stack is empty:%d\n",isempty(sp));
  57. printf("before pushing stack is full:%d\n",isfull(sp));
  58. push(sp,8);
  59. push(sp,7);
  60. push(sp,89);
  61. push(sp,9);
  62. push(sp,5);
  63. printf(" after pushing stack is empty:%d\n",isempty(sp));
  64. printf("after pushing stack is full:%d\n",isfull(sp));
  65. printf("the element is pop up in this stack:%d\n",pop(sp));
  66. return 0;
  67. }
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
 before pushing stack is  empty:1
before pushing stack is  full:0
 after pushing stack is  empty:0
after pushing stack is  full:0
the element is pop up in this stack:5