Advertisement
ahamed210

stack

Sep 21st, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #define stack_max 10
  3.  
  4. //structure variable
  5. typedef struct{
  6.     int top;
  7.     int data[stack_max];
  8. }stack;
  9.  
  10. //push function
  11. void push(stack *s, int item)
  12. {
  13.     if(s->top < stack_max){
  14.         s->data[s->top] = item;
  15.         s->top++;//when, pushing will be done the value of top will be one more than the length of the array
  16.         //so in the pop function first decrease one from top to excess the array
  17.     }
  18.     else{
  19.         printf("stack is full!!\n");
  20.     }
  21.     //printf("%d\n", s->top);
  22. }
  23.  
  24. //pop function
  25. int pop(stack *s)
  26. {
  27.     int item;
  28.     if(s->top == 0){
  29.         printf("Stack is empty\n");
  30.         return -1;
  31.     }
  32.     else{
  33.     //decreasing top value one
  34.     s->top--;
  35.     item = s->data[s->top];
  36.     }
  37.     return item;
  38. }
  39.  
  40. int main()
  41. {
  42.     stack my_stack;
  43.     int item;
  44.     my_stack.top = 0;
  45.     //calling push function
  46.     push(&my_stack, 1);
  47.     //printf("%d\n", my_stack.top);
  48.     push(&my_stack, 2);
  49.     //printf("%d\n", my_stack.top);
  50.     push(&my_stack, 3);
  51.     //printf("%d\n", my_stack.top);
  52.  
  53.     //calling pop function
  54.     item = pop(&my_stack);
  55.     printf("%d\n", item);
  56.     item = pop(&my_stack);
  57.     printf("%d\n", item);
  58.     item = pop(&my_stack);
  59.     printf("%d\n", item);
  60.     /*item = pop(&my_stack);
  61.     printf("%d\n", item);*/
  62.     return 0;
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement