Advertisement
rana1704

Stack Maximum

Oct 19th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int s[100];
  4. int stack_size=0, i = 0, max_stack_size = 10;
  5.  
  6. void print_stack()
  7. {
  8.     printf("current stack : ");
  9.     for(i=0;i<stack_size;i++)
  10.     {
  11.            printf("%d ",s[i]);
  12.     }
  13.     printf("\n");
  14. }
  15.  
  16. void push(int number)
  17. {
  18.     printf("-->push(%d)\n",number);
  19.  
  20.     //do push
  21.     if( stack_size <= max_stack_size ){
  22.         s[stack_size] = number;
  23.         stack_size++;
  24.  
  25.         print_stack();
  26.     } else{
  27.         printf("Stack Overflow\n");
  28.     }
  29.  
  30. }
  31.  
  32. int pop()
  33. {
  34.     printf("-->pop()\n");
  35.     int num;
  36.  
  37.     //do pop
  38.     if( stack_size > 0 ){
  39.         s[stack_size - 1] = 0;
  40.         stack_size--;
  41.  
  42.         print_stack();
  43.     } else{
  44.         printf("Stack Empty\n");
  45.     }
  46.  
  47.     return num;
  48. }
  49.  
  50.  
  51. int main(){
  52.     push(5);
  53.     push(6);
  54.     push(7);
  55.     pop();
  56.     push(4);
  57.     pop();
  58.     pop();
  59.     push(5);
  60.     push(6);
  61.     push(7);
  62.     push(5);
  63.     push(6);
  64.     push(7);
  65.     push(5);
  66.     push(6);
  67.     push(7);
  68.     push(5);
  69.     push(6);
  70.     push(7);
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement