Advertisement
Wan_ich1

double ended queue

May 22nd, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #define maks 5
  4.  
  5. using namespace std;
  6.  
  7. struct data
  8. {
  9.     int numb[maks];
  10.     int front;
  11.     int rear;
  12. }Data;
  13.  
  14. void inisialisasi()
  15. {
  16.     Data.front=-1;
  17.     Data.rear=maks;
  18. }
  19.  
  20. bool isFull()
  21. {
  22.     return Data.front==Data.rear-1;
  23. }
  24.  
  25. bool isEmpty()
  26. {
  27.     return Data.front==-1 && Data.rear==maks;
  28. }
  29.  
  30. void enqueFront()
  31. {
  32.     if (isFull())
  33.     {
  34.         cout << "Error: Data sudah penuh!" << endl;
  35.     }
  36.     else
  37.     {
  38.         Data.front++;
  39.         cout << "Masukkan angka = ";
  40.         cin >> Data.numb[Data.front];
  41.     }
  42. }
  43.  
  44. void enqueRear()
  45. {
  46.     if (isFull())
  47.     {
  48.         cout << "Error: Data sudah penuh!" << endl;
  49.     }
  50.     else
  51.     {
  52.         Data.rear--;
  53.         cout << "Masukkan angka = ";
  54.         cin >> Data.numb[Data.rear];
  55.     }
  56. }
  57.  
  58. void dequeFront()
  59. {
  60.     if(isEmpty())
  61.     {
  62.         cout << "Error: Data masih kosong!" << endl;
  63.     }
  64.     else
  65.     {
  66.         for (int i=0;i<Data.front;i++)
  67.         {
  68.             Data.numb[i]=Data.numb[i+1];
  69.         }
  70.         Data.front--;
  71.     }
  72. }
  73.  
  74. void dequeRear()
  75. {
  76.     if(isEmpty())
  77.     {
  78.         cout << "Error: Data masih kosong!" << endl;
  79.     }
  80.     else
  81.     {
  82.         for (int i=maks-1;i>Data.rear;i--)
  83.         {
  84.             Data.numb[i]=Data.numb[i-1];
  85.         }
  86.         Data.rear++;
  87.     }
  88. }
  89.  
  90. void show()
  91. {
  92.     if (isEmpty())
  93.     {
  94.         cout << "Error: Data masih kosong!" << endl;
  95.     }
  96.     else
  97.     {
  98.         for(int i=0;i<=Data.front;i++)
  99.         {
  100.             cout  << Data.numb[i] << " ->";
  101.         }
  102.         cout << " ";
  103.         for (int i=Data.rear;i<maks;i++)
  104.         {
  105.             cout << " <- "  << Data.numb[i];
  106.         }
  107.     }
  108. }
  109.  
  110. int main()
  111. {
  112.     int pil;
  113.     inisialisasi();
  114.     do
  115.     {
  116.         system("cls");
  117.         cout << "1. Enque front\n";
  118.         cout << "2. Enque rear\n";
  119.         cout << "3. Deque front\n";
  120.         cout << "4. Deque rear\n";
  121.         cout << "5. show data\n";
  122.         cout << "Input : ";
  123.         cin>> pil;
  124.  
  125.         switch(pil)
  126.         {
  127.         case 1:
  128.             {
  129.                 enqueFront();
  130.                 system("pause");
  131.                 break;
  132.             }
  133.         case 2:
  134.             {
  135.                 enqueRear();
  136.                 system("pause");
  137.                 break;
  138.             }
  139.         case 3:
  140.             {
  141.                 dequeFront();
  142.                 system("pause");
  143.                 break;
  144.             }
  145.         case 4:
  146.             {
  147.                 dequeRear();
  148.                 system("pause");
  149.                 break;
  150.             }
  151.         case 5:
  152.             {
  153.                 show();
  154.                 system("pause");
  155.                 break;
  156.             }
  157.         default:
  158.             {
  159.                 cout << "Error: invalid input" << endl;
  160.             }
  161.         }
  162.     }while(pil<=5);
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement