Pengertian Queue,cara kerja dan contoh program



Queue
Queue adalah bentuk khusus dari linear list dengan operasi penyisipan dan penghapusan dari elemennya queue memiliki 1 pintu masuk dan 1 pintu keluar yaitu di ujung depan dan ujung belakang atau biasa disebut head dan tail.

Contoh cara kerja Queue:



Contoh program queue yang saya buat semoga mudah di mengerti:
#include<stdio.h>
#include<iostream>
#include<conio.h>
#define MAX 8
using namespace std;

typedef struct{
       int data[MAX];
       int head;
       int tail;
}Queue;
Queue nunggu;

void Create(){
       nunggu.head = nunggu.tail = -1;
}
int IsEmpty(){
       if (nunggu.tail == -1)
              return 1;
       else
              return 0;
}
int IsFull(){
       if (nunggu.tail == MAX - 1)
              return 1;
       else
              return 0;
}

void Enqueue(int data){
       if (IsEmpty() == 1){
              nunggu.head = nunggu.tail = 0;
              nunggu.data[nunggu.tail] = data;
              printf("%d sudah dimasukan", nunggu.data[nunggu.tail]);
       }
       else
       if (IsFull() == 0){
              nunggu.tail++;
              nunggu.data[nunggu.tail] = data;
              printf("%d sudah dimasukan", nunggu.data[nunggu.tail]);
       }
}
int Dequeue(){
       int i;
       int e = nunggu.data[nunggu.head];
       for (i = nunggu.head; i <= nunggu.tail - 1; i++){
              nunggu.data[i] = nunggu.data[i + 1];
       }
       nunggu.tail--;
       return e;
}
void Clear(){
       nunggu.head = nunggu.tail = -1;
       printf("CLEAR");
}
void Tampil(){
       if (IsEmpty() == 0){
              for (int i = nunggu.head; i <= nunggu.tail; i++){
                     printf(" %d", nunggu.data[i]);
              }
       }
       else printf("data kosong!\n");
}
int main(){
       int op, multi;

       printf("\t      =========================================================\n");
       printf("\t      |                     PROGRAM QUEUE                     |\n");
       printf("\t      |=======================================================|\n");
       printf("\t      |                                                       |\n");
       printf("\t      | >Ricky satria                      (140010039)        |\n");
       printf("\t      |                                                       |\n");
       printf("\t      |                                                       |\n");
       printf("\t      |                   Compiled With Dev ++                |\n");
       printf("\t      |========================================================\n");
       printf("\t      |                         BA141                         |\n");
       printf("\t      |           Sistem Komputer - STIKOM BALI               |\n");
       printf("\t      =========================================================\n");
       printf("\t      |               Press Any Key To Continue               |\n");
       printf("\t      =========================================================\n");

       printf("\t      "); getchar();

       system("cls");

       int pil;
       int data;
       Create();
       do{

              cout << "==============================" << endl;
              cout << "\tPROGRAM QUEUE" << endl;
              cout << "==============================\n\n" << endl;
              cout << "1. ENQUEUE\n " << endl;
              cout << "2. DEQUEUE\n " << endl;
              cout << "3. MENAMPILKN\n " << endl;
              cout << "4. BERSIHKAN\n " << endl;
              cout << "5. EXIT\n " << endl;
              cout << " Masukan Pilihan Di Atas : "; cin >> pil;
              switch (pil){

              case 1:
                     cout << "Masukan Data Cuy : "; cin >> data;
                     Enqueue(data);
                     break;

              case 2:
                     Dequeue();
                     break;

              case 3:
                     Tampil();
                     break;

              case 4:
                     Clear();
                     break;
              case 5:
                     cout << endl << "\nSEMOGA NGERTI :D";
                     break;



              }

              getchar();
       } while (pil != 5);
       return 0;
}


Hasil tampilan program di atas:




Previous
Next Post »