SAADQUAMER

Stack Average

Oct 29th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define CAPACITY 100
  5. typedef struct node
  6. {
  7.     int data;
  8.     struct node *next;
  9. }node ;
  10. node*top;
  11.  
  12. int size = 0;
  13.  
  14. void push(int element)
  15.  
  16. {
  17.     if (size >= CAPACITY)
  18.     {
  19.         printf("\tStack Overflow\n");
  20.         return;
  21.     }
  22.     node *N = (node *) malloc(sizeof(node));
  23.     N->data = element;
  24.  
  25.     N->next = top;
  26.  
  27.  
  28.     top = N;
  29.  
  30.     ++size;
  31.  
  32. }
  33.  
  34. int pop()
  35. {
  36.     int data = 0,n;
  37.     node *N;
  38.  
  39.     N = top;
  40.     data = top->data;
  41.     top = top->next;
  42.     free(N);
  43.     size--;
  44.     return data;
  45. }
  46.  
  47.  
  48. int main()
  49. {
  50.  
  51.     int t,data,i ,sum=0;
  52.     int m,l,x;
  53.  
  54.     printf("TOTAL INPUT :");
  55.     scanf("%d",&t);
  56.     for(i=1; i<=t; i++)
  57.     {
  58.         scanf("%d",&data);
  59.         push(data);
  60.     }
  61.     printf("TOTAL OUTPUT NUMBER :");
  62.     scanf("%d",&m);
  63.     for(l=0; l<m; l++)
  64.     {
  65.         x=pop();
  66.         printf("%d\n",x);
  67.  
  68.         sum=sum+x;
  69.  
  70.     }
  71.  
  72.     printf("%.2f\n",(float)sum/m);
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment