#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#define MAX 6
typedef struct
{
int data[MAX];
int head;
int tail;
}queue;
queue antrian;
void create()
{
antrian.head=antrian.tail=-1;
}
int isempty() {
if(antrian.tail==-1)
return 1;
else
return 0;
}
int isfull() {
if(antrian.tail==MAX-1)
return 1;
else
return 0;
}
void enqueue(int data)
{
if(isempty()==1)
{
antrian.head=antrian.tail=0;
antrian.data[antrian.tail]=data;
printf("%d, Sudah Masuk!",antrian.data[antrian.tail]);
void tampil();
{
int i;
if (isempty()==0)
{
for(i=antrian.head;i<=antrian.tail;i++)
{
printf(" %d ",antrian.data[i]);
}
}
else
printf("n**** QUEUE IS EMPTY ****n");
}
}
else
if(isfull()==0)
{
antrian.tail++;
antrian.data[antrian.tail]=data;
printf("%d , Sudah Masuk!",antrian.data[antrian.tail]);
}
else{
if(isfull()==1)
{
cout<<"nn**** QUEUE IS FULL , data TIDAK dapat masuk ****";
}
}
gotoxy(25,8);cout<<"PRESS any key for back to MENU";
}
int dequeue()
{
if (isempty()==1){
cout<<"n**** ERROR :: QUEUE IS EMPTY ****";
}else
if(isempty()==0){
int i;
int e=antrian.data[antrian.head];
for(i=antrian.head;i<=antrian.tail-1;i++)
{
antrian.data[i]=antrian.data[i+1];
}
antrian.tail--;
cout<<"nnData Yang Keluar => "<<e;
}
gotoxy(25,8);cout<<"PRESS any key for back to MENU";
}
void clear()
{
antrian.head=antrian.tail=-1;
printf("nn**** DATA CLEAR ****");
gotoxy(25,8);cout<<"PRESS any key for back to MENU";
}
void tampil()
{
int i;
if(isempty()==0)
{
cout<<"Data Yang ada Dalam QUEUE : "<<endl<<endl;
for(i=antrian.head;i<=antrian.tail;i++)
{
printf("| %d |",antrian.data[i]);
}
}
else
{
printf("n**** QUEUE IS EMPTY ****n");
}
gotoxy(25,8);cout<<"PRESS any key for back to MENU";
}
void main()
{
int pil;
int data;
create();
do
{
clrscr();
gotoxy(25,2);cout<<"========MENU PILIHAN========"<<endl<<endl;
gotoxy(25,4);cout<<"============================"<<endl;
gotoxy(30,6);cout<<" 1. ENQUEUE "<<endl;
gotoxy(30,7);cout<<" 2. DEQUEUE "<<endl;
gotoxy(30,8);cout<<" 3. TAMPILAN "<<endl;
gotoxy(30,9);cout<<" 4. CLEAR "<<endl;
gotoxy(30,10);cout<<" 5. KELUAR "<<endl;
gotoxy(25,12);cout<<"============================"<<endl;
gotoxy(25,14);cout<<" Masukan Pilihan Anda => ";cin>>pil;
switch(pil){
case 1:
clrscr();
printf("nn Masukan Data => "); scanf("%d",&data);
enqueue(data);
break;
case 2:
clrscr();
dequeue();
break;
case 3:
clrscr();
cout<<endl;
tampil();
break;
case 4:
clrscr();
clear();
break;
case 5:
clrscr();
gotoxy(25,8);cout<<"**** TERIMA KASIH ****"<<endl;
break;
}
getch();
} while(pil!=5);
}

contoh Program queue

  • 1.
    #include <stdio.h> #include <conio.h> #include<iostream.h> #define MAX 6 typedef struct { int data[MAX]; int head; int tail; }queue; queue antrian; void create() { antrian.head=antrian.tail=-1; } int isempty() { if(antrian.tail==-1) return 1; else return 0; }
  • 2.
    int isfull() { if(antrian.tail==MAX-1) return1; else return 0; } void enqueue(int data) { if(isempty()==1) { antrian.head=antrian.tail=0; antrian.data[antrian.tail]=data; printf("%d, Sudah Masuk!",antrian.data[antrian.tail]); void tampil(); { int i; if (isempty()==0) { for(i=antrian.head;i<=antrian.tail;i++) { printf(" %d ",antrian.data[i]); } }
  • 3.
    else printf("n**** QUEUE ISEMPTY ****n"); } } else if(isfull()==0) { antrian.tail++; antrian.data[antrian.tail]=data; printf("%d , Sudah Masuk!",antrian.data[antrian.tail]); } else{ if(isfull()==1) { cout<<"nn**** QUEUE IS FULL , data TIDAK dapat masuk ****"; } } gotoxy(25,8);cout<<"PRESS any key for back to MENU"; } int dequeue() { if (isempty()==1){ cout<<"n**** ERROR :: QUEUE IS EMPTY ****";
  • 4.
    }else if(isempty()==0){ int i; int e=antrian.data[antrian.head]; for(i=antrian.head;i<=antrian.tail-1;i++) { antrian.data[i]=antrian.data[i+1]; } antrian.tail--; cout<<"nnDataYang Keluar => "<<e; } gotoxy(25,8);cout<<"PRESS any key for back to MENU"; } void clear() { antrian.head=antrian.tail=-1; printf("nn**** DATA CLEAR ****"); gotoxy(25,8);cout<<"PRESS any key for back to MENU"; } void tampil() { int i; if(isempty()==0)
  • 5.
    { cout<<"Data Yang adaDalam QUEUE : "<<endl<<endl; for(i=antrian.head;i<=antrian.tail;i++) { printf("| %d |",antrian.data[i]); } } else { printf("n**** QUEUE IS EMPTY ****n"); } gotoxy(25,8);cout<<"PRESS any key for back to MENU"; } void main() { int pil; int data; create(); do { clrscr(); gotoxy(25,2);cout<<"========MENU PILIHAN========"<<endl<<endl; gotoxy(25,4);cout<<"============================"<<endl; gotoxy(30,6);cout<<" 1. ENQUEUE "<<endl;
  • 6.
    gotoxy(30,7);cout<<" 2. DEQUEUE"<<endl; gotoxy(30,8);cout<<" 3. TAMPILAN "<<endl; gotoxy(30,9);cout<<" 4. CLEAR "<<endl; gotoxy(30,10);cout<<" 5. KELUAR "<<endl; gotoxy(25,12);cout<<"============================"<<endl; gotoxy(25,14);cout<<" Masukan Pilihan Anda => ";cin>>pil; switch(pil){ case 1: clrscr(); printf("nn Masukan Data => "); scanf("%d",&data); enqueue(data); break; case 2: clrscr(); dequeue(); break; case 3: clrscr(); cout<<endl; tampil(); break;
  • 7.
    case 4: clrscr(); clear(); break; case 5: clrscr(); gotoxy(25,8);cout<<"****TERIMA KASIH ****"<<endl; break; } getch(); } while(pil!=5); }