Advertisement
Guest User

Algoritmi i strukture 2

a guest
Feb 18th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define SIZE 10  /* Size of Stack */
  4. int CQ[SIZE],f=-1,r=-1;       /* Global declarations */
  5. int s[SIZE],top=-1;       /* Global declarations */
  6.  
  7. void push(int elem)
  8. {                       /* Function for PUSH operation */
  9.     if( Sfull()) printf("\n\n Overflow!!!!\n\n");
  10.     else
  11.     {
  12.         ++top;
  13.         s[top]=elem;
  14.     }
  15. }
  16.  
  17. int pop()
  18. {                      /* Function for POP operation */
  19.     int elem;
  20.     if(Sempty()){ printf("\n\nUnderflow!!!!\n\n");
  21.     return(-1); }
  22.     else
  23.     {
  24.         elem=s[top];
  25.         top--;
  26.         return(elem);
  27.     }
  28. }
  29.  
  30. int Sfull()
  31. {                     /* Function to Check Stack Full */
  32.     if(top == SIZE-1) return 1;
  33.     return 0;
  34. }
  35.  
  36. int Sempty()
  37. {                    /* Function to Check Stack Empty */
  38.     if(top == -1) return 1;
  39.     return 0;
  40. }
  41.  
  42. void displayStek()
  43. {                  /* Function to display status of Stack */
  44.     int i;
  45.     if(Sempty()) printf(" \n Empty Stack\n");
  46.     else
  47.     {
  48.         for(i=0;i<=top;i++)
  49.             printf("\n%d\n",s[i]);
  50.         printf("^Top\n\n");
  51.     }
  52. }
  53. void insert(int elem)
  54. {                       /* Function for Insert operation */
  55.     if( CQfull()) printf("\n\n Overflow!!!!\n\n");
  56.     else
  57.     {
  58.         if(f==-1)f=0;
  59.         r=(r+1) % SIZE;
  60.         CQ[r]=elem;
  61.     }
  62. }
  63. int Delete()
  64. {                      /* Function for Delete operation */
  65.     int elem;
  66.     if(CQempty()){ printf("\n\nUnderflow!!!!\n\n");
  67.     return(-1); }
  68.     else
  69.     {
  70.         elem=CQ[f];
  71.         if(f==r){ f=-1; r=-1;} /* Q has only one element ? */
  72.         else
  73.             f=(f+1) % SIZE;
  74.         return(elem);
  75.     }
  76. }
  77. int  CQfull()
  78. {                     /* Function to Check Circular Queue Full */
  79. /* proverava se pre povećanja r*/
  80.     if( (f==r+1) || (f == 0 && r== SIZE-1)) return 1;
  81.     return 0;
  82. }
  83.  
  84. int CQempty()
  85. {                    /* Function to Check Circular Queue Empty */
  86.     if(f== -1) return 1;
  87.     return 0;
  88. }
  89.  
  90. void displayRed()
  91. {        /* Function to display status of Circular Queue */
  92.     int i;
  93.     if(CQempty()) printf(" \n Empty Queue\n");
  94.     else
  95.     {
  96.         printf("Front[%d]->",f);
  97.         for(i=f;i!=r;i=(i+1)%SIZE)
  98.             printf("%d ",CQ[i]);
  99.         printf("%d ",CQ[i]);
  100.         printf("<-[%d]Rear\n",r);
  101.     }
  102. }
  103. void RedUmetanje()
  104. {
  105.     int i;
  106.     insert(2018);
  107.     for(i=2;i<=7;i++)
  108.     {
  109.         insert(2018*i);
  110.     }
  111. }
  112. void StekUmetanje()
  113. {
  114.     int i;
  115.     for(i=f;i!=r;i=(i+1)%SIZE)
  116.         if(CQ[i]%5==0)
  117.             push(CQ[i]);
  118. }
  119. int main()
  120. {
  121.     RedUmetanje();
  122.     displayRed();
  123.     StekUmetanje();
  124.     displayStek();
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement