QUEUE


Nama                     : Haniffah Asysyarifah
NIM / Kelas          : A710170005 / A
Prodi                     : Pendidikan Teknik Informatika
Mata Kuliah          : Struktur Data
Tugas                    : Membuat Program dengan Queue

Pada Program dibawah ini merupakan Program QUEUE (Antrian) menggunakan Bahasa C++
                       
#include <iostream>
#define MAX 20 //maksimum data queue
using namespace std;

//Deklarasi struct antrian
struct Queue {
            int front, rear, data[MAX];
}Q;

//cek apakah antrian penuh
bool isFull() {
            return Q.rear == MAX;
}

//cek apakah antrian kosong
bool isEmpty() {
            return Q.rear == 0;
}

//Menampilkan Queue
void print_Queue() {
            if (isEmpty()) {
cout<< "Antrian kosong"<<endl;
            }
            else {
                        cout<< "QUEUE : ";
                        for (int i = Q.front; i<Q.rear; i++)
                        //menambahkan koma jika data tidak terdapat di antrian pertama
                                    cout<<Q.data[i] << ((Q.rear-1 == i) ? "" : ",");
                        cout<<endl;
  }
}

//manambahkan data keantrian
void enqueue() {
            if (isFull())
            {
                        cout<< "Antrian penuh!"<<endl;
            }
            else {
                        int data;
                        //menambahkan data keantrian
                        cout<< "Masukkan Data : ";cin>> data;
                        Q.data[Q.rear] = data;
                        //menempatkan tail padaelemen data terakhir yang ditambahkan
                        Q.rear++;
                        cout<< "Data ditambahkan\n";
                        print_Queue();
            }
}

// mengambil data dariantrian
void dequeue() {
            if (isEmpty())
            {
                        cout<< "Antrian masih kosong"<<endl;
            }
            else{
                        cout<< "Mengambil data \"" <<Q.data[Q.front] << "\"..." <<endl;
                        //menggeser antrian data ke head
                        for (int i = Q.front; i<Q.rear; i++)
                                    Q.data[i] = Q.data[i + 1];
                        //menempatkan tail pada data terakhir yang digeser
                        Q.rear--;
                        print_Queue();
            }
}

int main() {
            int choose;
            {
        cout<< "Nama   : Haniffah Asysyarifah"<<endl;
        cout<< "NIM    : A710170005"<<endl;
        cout<< "Kelas  : A\n"<<endl;
            } do
        {
        //Tampilan menu
                        cout<< "-------------------------------\n"
                                    << "   Menu Pilihan\n"
                                    << "-------------------------------\n"
                                    << "[1] Enqueue \n"
                                    << "[2] Dequeue\n"
                                    << "[3] Keluar \n\n"
                                    << "-------------------------------\n"
                                    << "Masukkan pilihan : "; cin>> choose;
                        switch (choose)
                        {
                        case 1:
                                    enqueue();
                                    break;
                        case 2:
                                    dequeue();
                                    break;
                        default:
                                    cout<< "Pilihan tidak tersedia";
                                    break;
                        }
            } while (choose !=3);
            return 0;
}

Gambar 1.1 program output Queue

Gambar 1.2 program output Queue

Gambar 1.3 program output Queue


Referensi :
·       Sukirman, dan Irma Yuliana.(2017).Modul Praktikum Struktur Data.Surakarta : Universitas Muhammadiyah Surakarta.
·       Wantoro, Jan dan Sukirman.(2017).Algoritma & Struktur Data dalam Bahasa C/C++.Surakarta : Muhammadiyah University Press.

Komentar

Postingan Populer