Advertisement
Wan_ich1

modul 3

May 22nd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. using namespace std;
  5.  
  6.     int front=-1;
  7.     int rear=-1;
  8.     int Max=5;
  9.  
  10. struct data
  11. {
  12.     int numb;
  13. };
  14.  
  15. bool isFull()
  16. {
  17.     if (rear==Max-1)
  18.     {
  19.         return true;
  20.     }
  21.     else
  22.     {
  23.         return false;
  24.     }
  25. }
  26.  
  27. bool isEmpty()
  28. {
  29.     if (rear==-1)
  30.     {
  31.         return true;
  32.     }
  33.     else
  34.     {
  35.         return false;
  36.     }
  37. }
  38.  
  39. int main()
  40. {
  41.     data Data[5];
  42.     int pil;
  43.  
  44.     do
  45.     {
  46.         system("cls");
  47.         cout << "Pilihan !!!" << endl;
  48.         cout << "1. Enqueue" << endl;
  49.         cout << "2. Dequeue" << endl;
  50.         cout << "3. Show" << endl;
  51.         cout << "4. Exit" << endl;
  52.         cout << "Masukkan pilihan = ";
  53.         cin >> pil;
  54.  
  55.         switch (pil)
  56.         {
  57.         case 1:
  58.             {
  59.                 if (isFull()==true)
  60.                 {
  61.                     cerr << "Data penuh" << endl;
  62.                 }
  63.                 else
  64.                 {
  65.                     if (front==-1)
  66.                     {
  67.                         front++;
  68.                     }
  69.                     rear++;
  70.                     cout << "Masukkan angka = ";
  71.                     cin >> Data[rear].numb;
  72.                 }
  73.                 system("pause");
  74.                 break;
  75.             }
  76.         case 2:
  77.             {
  78.                 if (isEmpty()==true)
  79.                 {
  80.                     cerr << "Data masih kosong" << endl;
  81.                 }
  82.                 else if (front==rear)
  83.                 {
  84.                     Data[front].numb='\0';
  85.                     front--;
  86.                     rear--;
  87.                     cout << "Data telah di hapus" << endl;
  88.                 }
  89.                 else
  90.                 {
  91.                     for(int i=0;i<rear;++i)
  92.                     {
  93.                         Data[i]=Data[i+1];
  94.                     }
  95.                     Data[rear].numb='\0';
  96.                     rear--;
  97.                     cout << "Data telah di hapus" <<endl;
  98.                 }
  99.                 system("pause");
  100.                 break;
  101.             }
  102.         case 3:
  103.             {
  104.                 if(isEmpty()==true)
  105.                 {
  106.                    cout << "Data masih kosong!!!" << endl;
  107.                 }
  108.                 else
  109.                 {
  110.                     for (int i=0;i<=rear;i++)
  111.                     {
  112.                         cout << "<- [ " << Data[i].numb << " ] ";
  113.                     }
  114.                     cout << endl;
  115.  
  116.                 }
  117.  
  118.  
  119.                 system("pause");
  120.                 break;
  121.             }
  122.         case 4:
  123.             {
  124.                 return 0;
  125.             }
  126.         default:
  127.             {
  128.                 cerr << "Pilihan tidak ada" << endl;
  129.                 system("pause");
  130.                 break;
  131.             }
  132.         }
  133.     }while(pil<=4);
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement