simple queue


#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#define MAX 6
 
typedef struct{
int data[MAX];
int head;
int tail;
}queue;

queue antrian; //deklarasi variabel antrian
 
 void create();
 int isempty();
 int isfull();
 void enqueue(int data);
 void tampil();
 
int main()
{
    create();
    
    char menu='0';
    int data;
    
    while(menu != 'q')
    {
        cout<<"::: MENU QUEUE"<<endl;
        cout<<"1. IsEmpty"<<endl;                  
        cout<<"2. IsFull"<<endl;
        cout<<"3. EnQueue"<<endl;
        cout<<"4. dequeue"<<endl;
        cout<<"5. tampil"<<endl;
        cout<<"Pilihan =";
        cin >>menu;
        
        switch (menu)
        {
          case '1': if(isempty()==1)
                       cout<<"antrian kosong";
                    else
                       cout<<"Antrian isi";
          
                    break;
          case '3': cout<<"Masukan data=";
                    cin>>data;
                    enqueue(data);
                    getch();
                    break;
          case '5':tampil();
                   getch();
                   break;         
        } //end switch
        system("cls");
    } //end while
   
    getch();
    return 1;   
}  //end main()

void tampil()
{
     cout<<"isi antrian"<<endl;
     for(int i=antrian.head;i<=antrian.tail;i++)   
     {
       cout<<antrian.data[i]<<"-";                                        
     }
}

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]);
    }
    else if (isfull()==0)
    {
        antrian.tail++;
        antrian.data[antrian.tail]=data;
        printf("%d, Sudah Masuk!",antrian.data[antrian.tail]);   
    }
    
}

//fungsi create
void create()
{
antrian.head=antrian.tail=-1;
}

//fungsi isempty
int isempty() 
{
   if(antrian.tail== -1)
     return 1;
   else
     return 0;
}

int isfull() {
    if(antrian.tail==MAX-1)
      return 1;
    else
      return 0;
}




3 thoughts on “simple queue

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s