Advertisement
afrinahoque

Queue

Dec 8th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1. #include<stdio.h>
  2. int head=-1, tail=-1, arr[100];
  3.  
  4. void enqueue(int n)
  5. {
  6.     if(tail>=100)
  7.     {
  8.         printf("It's overflown.\n");
  9.         return;
  10.     }
  11.     else if(head==-1 && tail==-1)
  12.     {
  13.         head=0;
  14.         arr[++tail]=n;
  15.         return;
  16.     }
  17.     else
  18.         arr[++tail]=n;
  19. }
  20.  
  21. int dequeue()
  22. {
  23.     if(head==-1)
  24.     {
  25.         printf("It's empty.\n");
  26.         return -1;
  27.     }
  28.     else
  29.     {
  30.         int temp=arr[head];
  31.         head++;
  32.         if(head>tail)
  33.         {
  34.             head=-1;
  35.             tail=-1;
  36.         }
  37.         return temp;
  38.     }
  39. }
  40.  
  41. int main()
  42. {
  43.     int i,n,x,y,a,b,sum=0;
  44.     scanf("%d", &n);
  45.     for(i=0; i<n; i++)
  46.     {
  47.         scanf("%d", &x);
  48.         enqueue(x);
  49.     }
  50.     scanf("%d", &y);
  51.     for(i=0; i<y; i++)
  52.     {
  53.         a=dequeue();
  54.         sum=sum+a;
  55.         printf("%d ", a);
  56.     }
  57.     printf("sum: %d\n", sum);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement