Advertisement
Rakibul_Ahasan

Stack_Array_Pointer_With_Function

Aug 21st, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. #define STACK_MAX 100
  4.  
  5. typedef struct{
  6.     int top;
  7.     int data[STACK_MAX];
  8. }Stack;
  9.  
  10. void push(Stack *s,int item){
  11.  
  12.     if(s->top < STACK_MAX){
  13.         s->data[s->top]=item;
  14.         s->top = s->top+1;
  15.     }else {
  16.         printf("Stack is full!!\n");
  17.     }
  18. }
  19.  
  20. int pop(Stack *s){
  21.     int item;
  22.  
  23.     if(s->top==0){
  24.             printf("stack is Empty!!\n");
  25.             return -1;
  26.     } else {
  27.         s->top = s->top - 1;
  28.         item= s->data[s->top];
  29.     }
  30.     return item;
  31. }
  32.  
  33. int main()
  34. {
  35.     Stack my_stack;
  36.     int item;
  37.  
  38.     my_stack.top = 0;
  39.  
  40.     push(&my_stack,1);
  41.     push(&my_stack,2);
  42.     push(&my_stack,3);
  43.  
  44.     item=pop(&my_stack);
  45.     printf("%d\n",item);
  46.  
  47.     item=pop(&my_stack);
  48.     printf("%d\n",item);
  49.  
  50.     item=pop(&my_stack);
  51.     printf("%d\n",item);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement