Advertisement
haopoka

QueueInstall_Array

Apr 14th, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <iostream>
  3. #define Max 100
  4. using namespace std;
  5. typedef int item;
  6.  
  7. struct Queue
  8. {
  9.     int Front, Rear;
  10.     item Data[Max];
  11.     int count;
  12.     Queue() {
  13.         Front = 0;
  14.         Rear = -1;
  15.         count = 0;
  16.     }
  17. };
  18.  
  19. int Isempty(Queue Q)
  20. {
  21.     if (Q.count == 0)
  22.         return 1;
  23.     return 0;
  24. }
  25.  
  26. int Isfull(Queue Q)
  27. {
  28.     if (Q.count == Max)
  29.         return 1;
  30.     return 0;
  31. }
  32.  
  33. void Push(Queue& Q, item x){
  34.     if (Isfull(Q))
  35.         cout << "Hang doi day !\n";
  36.     else {
  37.         Q.Data[++Q.Rear] = x;
  38.         Q.count++;
  39.     }
  40. }
  41.  
  42. int Pop(Queue& Q)
  43. {
  44.     if (Isempty(Q)) cout << "Hang doi rong !\n";
  45.     else
  46.     {
  47.         item x = Q.Data[Q.Front];
  48.         for (int i = Q.Front; i < Q.Rear; i++)
  49.             Q.Data[i] = Q.Data[i + 1];
  50.         Q.Rear--;
  51.         Q.count--;
  52.         return x;
  53.     }
  54. }
  55.  
  56. item Qfront(Queue Q)
  57. {
  58.     if (Isempty(Q)) cout << "Hang doi rong !\n";
  59.     else return Q.Data[Q.Front];
  60. }
  61.  
  62. void Push_Circular(Queue& Q, item x)
  63. {
  64.     if (Isfull(Q)) cout << "Hang doi day !\n";
  65.     else
  66.     {
  67.         Q.Data[(++Q.Rear) % Max] = x;
  68.         Q.count++;
  69.     }
  70. }
  71.  
  72. int Pop_Circular(Queue& Q)
  73. {
  74.     if (Isempty(Q)) cout << "Hang doi rong !\n";
  75.     item x = Q.Data[Q.Front];
  76.     Q.Front = (Q.Front++) % Max;
  77.     Q.count--;
  78.     return x;
  79. }
  80.  
  81.  
  82. void Input(Queue& Q)
  83. {
  84.     int n;
  85.     item x;
  86.     do
  87.     {
  88.         cout << "Nhap so phan tu cua Queue: ";
  89.         cin >> n;
  90.     } while (n > Max || n < 1);
  91.     for (int i = 1; i <= n; i++)
  92.     {
  93.         cout << "Nhap phan tu thu " << i << " : ";
  94.         cin >> x;
  95.         Push(Q, x);
  96.     }
  97. }
  98.  
  99. void Output(Queue Q)
  100. {
  101.     if (Isempty(Q)) cout << "Hang doi rong !\n";
  102.     else
  103.     {
  104.         for (int i = Q.Front; i <= Q.Rear; i++)
  105.             cout << Q.Data[i] << endl;
  106.     }
  107. }
  108.  
  109. int main()
  110. {
  111.     Queue Q;
  112.     Input(Q);
  113.     Output(Q);
  114.  
  115.     int lua_chon;
  116.     cout << "1.Kiem tra Queue rong hay khong?\n"<<
  117.         "2.Kiem tra Queue day hay khong ? \n"<<
  118.         "3.Them phan tu vao Queue.\n" <<
  119.         "4.Xoa phan tu trong Queue.\n" <<
  120.         "5.Xuat Queue.\n"<<
  121.         "6.Thoat." << endl;
  122.     do
  123.     {
  124.         cout << "Nhap lua chon cua ban: ";
  125.         cin >> lua_chon;
  126.         switch (lua_chon)
  127.         {
  128.         case 1: // if( luachon == 1)
  129.         {
  130.             if (Isempty(Q)) cout << "Queue rong !\n";
  131.             else cout << "Queue khong rong !\n";
  132.             break;
  133.         }
  134.         case 2:// if( luachon == 2)
  135.         {
  136.             if (Isfull(Q)) cout << "Queue day !\n";
  137.             else cout << "Queue khong day !\n";
  138.             break;
  139.         }
  140.         case 3: // if( luachon == 3)
  141.         {
  142.             item x;
  143.             cout << "Nhap phan tu can chen vao Queue: ";
  144.             cin >> x;
  145.             Push(Q, x);
  146.             Output(Q);
  147.             break;
  148.         }
  149.         case 4:// if( luachon == 4)
  150.         {
  151.             Pop(Q);
  152.             Output(Q);
  153.             break;
  154.         }
  155.         case 5:
  156.         {
  157.             Output(Q);
  158.             break;
  159.         }
  160.         default: break;
  161.         }
  162.     } while (lua_chon != 6);
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement