document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <iostream>
  2. #include <conio.h>
  3. #ifdef __cplusplus__
  4. #include <cstdlib>
  5. #else
  6. #include <stdlib.h>
  7. #endif
  8.  
  9. #define max 6    //Mendeklarasikan max data pada queue
  10.  
  11. int value[max];
  12. int kepala = -1, ekor = -1, a;
  13.  
  14. //membuat fungsi IsEmpty
  15. bool IsEmpty(){
  16.         if (kepala == -1 && ekor == -1)
  17.                 return true;
  18.         else
  19.                 return false;
  20. }
  21.  
  22. //membuat fungsi IsFull
  23. bool IsFull(){
  24.         if (ekor == max - 1)
  25.                 return true;
  26.         else
  27.                 return false;
  28. }
  29.  
  30. //membuat enqueue, untuk memasukkan data kedalam queue / antrian
  31. void Enqueue(){
  32.         if (IsFull()) {
  33.                 cout << "Queue sudah numpuk, tunggu beberapa saat ";
  34.         }
  35.         else {
  36.                 if (IsEmpty()){
  37.                         kepala = ekor = 0;
  38.                         cout << "Masukkan value : "; cin >> value[ekor];
  39.                 }
  40.                 else {
  41.                         ekor++;
  42.                         cout << "Masukkan value : "; cin >> value[ekor];
  43.                 }
  44.         }
  45. }
  46.  
  47. //Membuat fungsi Dequeue
  48. void Dequeue(){
  49.         if (IsEmpty()){
  50.                 cout << "Queue masih kosong ! ";
  51.                 getch();
  52.         }
  53.         else {
  54.                 cout << "Value yang diambil : " << value[kepala];
  55.                 for (a = kepala; a <= ekor - 1; a++)
  56.                         value[a] = value[a + 1];
  57.                 ekor--;
  58.                 if (ekor == -1)
  59.                         kepala = -1;
  60.                 getch();
  61.         }
  62. }
  63.  
  64. //Membuat Prosedur clear
  65. void Clear(){
  66.         kepala = ekor = -1;
  67.         cout << "Queue sudah dikosongkan ! "; getch();
  68. }
  69.  
  70. //Membuat Prosedur View
  71. void View(){
  72.         if (IsEmpty()){
  73.                 cout << "Queue kosong ! ";
  74.                 getch();
  75.         }
  76.         else {
  77.                 for (a = kepala; a <= ekor; a++)
  78.                         cout << "Value pada queue ke " << a << " = " << value[a] << endl;
  79.                 getch();
  80.         }
  81. }
  82.  
  83. int main(){
  84.         int jawab;
  85.         do{
  86.                 if (system("CLS")) system("clear");
  87.                 cout << "**********Program Queue**********" << endl;
  88.                 cout << "1. Enqueue " << endl;
  89.                 cout << "2. Dequeue " << endl;
  90.                 cout << "3. Clear " << endl;
  91.                 cout << "4. View " << endl;
  92.                 cout << "5. Exit " << endl;
  93.                 cout << "Masukkan pilihan Anda : ";
  94.                 cin >> jawab;
  95.                 switch (jawab){
  96.                 case 1:
  97.                         Enqueue(); break;
  98.                 case 2:
  99.                         Dequeue(); break;
  100.                 case 3:
  101.                         Clear(); break;
  102.                 case 4:
  103.                         View(); break;
  104.                 }
  105.         } while (jawab != 5);
  106. }
');