Advertisement
Guest User

Queue SVE

a guest
Jan 24th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. #define max_size 10
  5. int a[max_size];
  6. int front = -1;
  7. int rear = -1;
  8.  
  9. bool IsFull()
  10. {
  11.     return((rear + 1) % max_size == front);
  12. }
  13.  
  14. bool IsEmpty()
  15. {
  16.     return(front == -1 && rear == -1);
  17. }
  18.  
  19. void Enqueue(int x)
  20. {
  21.     if (IsFull())
  22.     {
  23.         return;
  24.     }
  25.     else if (IsEmpty())
  26.     {
  27.         front = rear = 0;
  28.     }
  29.     else
  30.     {
  31.         rear = (rear + 1) % max_size;
  32.     }
  33.     a[rear] = x;
  34. }
  35.  
  36. void Dequeue()
  37. {
  38.     if (IsEmpty())
  39.     {
  40.         return;
  41.     }
  42.     else if (front == rear)
  43.     {
  44.         front = rear = -1;
  45.     }
  46.     else
  47.     {
  48.         front = (front + 1) % max_size;
  49.     }
  50. }
  51.  
  52. int Front()
  53. {
  54.     return a[front];
  55. }
  56.  
  57. void Print()
  58. {
  59.     int count = (rear + max_size - front) % max_size + 1;
  60.     for (int i = 0; i < count; i++)
  61.     {
  62.         int index = (front + i) % max_size;
  63.         cout << a[index] << " ";
  64.     }
  65.     cout << "\n\n";
  66. }
  67.  
  68. int main()
  69. {
  70.     Enqueue(2);  Print();
  71.     Enqueue(4);  Print();
  72.     Enqueue(6);  Print();
  73.     Dequeue();   Print();
  74.     Enqueue(8);  Print();
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement