Advertisement
khisby

Single Queue

May 20th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #define maks 5
  4. using namespace std;
  5.  
  6. struct antrian{
  7.     int top=-1;
  8.     int bottom=-1;
  9.     char data[maks];
  10. }ant;
  11.  
  12. bool isempty(){
  13.     if (ant.bottom ==  -1)
  14.     {
  15.         return true;
  16.     }else{
  17.         return false;
  18.     }
  19. }
  20.  
  21. bool isfull(){
  22.     if(ant.top + 1 == maks)
  23.     {
  24.         return true;
  25.     }else{
  26.         return false;
  27.     }
  28. }
  29.  
  30. void enqueue(char data){
  31.     if(isfull()){
  32.         cout << "Antrian Penuh.. " << endl;
  33.         system("pause");
  34.     }else{
  35.         if (ant.bottom == -1) {
  36.             ant.bottom = ant.bottom + 1;
  37.         }
  38.         ant.top = ant.top + 1;
  39.         ant.data[ant.top] = data;
  40.     }
  41. }
  42.  
  43. void dequeue(){
  44.     char data;
  45.     if(isempty()){
  46.         cout << "Antrian kosong" << endl;
  47.         system("pause");
  48.     }else{
  49.         data = ant.data[ant.top];
  50.         cout << "Data " << data << " telah di hapus" << endl;
  51.         if(ant.bottom == ant.top){
  52.             ant.data[ant.bottom] = '\0';
  53.             ant.bottom = ant.bottom - 1;
  54.         }else{
  55.             for (int i = 0; i < ant.top; ++i) {
  56.                 ant.data[i] = ant.data[i + 1];
  57.             }
  58.             ant.data[ant.top] = '\0';
  59.         }
  60.         ant.top = ant.top-1;
  61.     }
  62. }
  63.  
  64. void display(){
  65.     for (int i = 0; i <maks; i++) {
  66.         cout << "[" << ant.data[i] << "] ";
  67.     }
  68. }
  69.  
  70. int main(){
  71.     char data;
  72.     int menu;
  73.  
  74.     do{
  75.         system("cls");
  76.         cout << "Top : " << ant.top << endl;
  77.         cout << "Buttom : " << ant.bottom << endl;
  78.         cout << "Isempty ? "<< isempty() <<endl;
  79.         cout << "Isfull ? " << isfull() <<endl;
  80.         cout << "maks ? " << maks <<endl;
  81.  
  82.         display();
  83.         cout << endl;
  84.         cout << "Menu: "<<endl;
  85.         cout << "1. Tambah(push)"<<endl;
  86.         cout << "2. Hapus(POP)"<<endl;
  87.         cout << "3. Keluar"<<endl;
  88.         cout << "Masukkan menu: ";
  89.         cin >> menu;
  90.  
  91.         if(menu == 1){
  92.             cout << "Masukkan data : ";
  93.             cin >>data;
  94.             enqueue(data);
  95.         }else{
  96.             dequeue();
  97.         }
  98.     }while(menu != 3);
  99.  
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement