Rabu, 24 Maret 2010

Queue Class Example

package queue;

public class Queue {
    private boolean full, empty;
    private int max_item = 100;
    private int item [max_item];
    private int head, tail;
  
    public Queue(){
        full=false;
        empty=true;
        head=0;
        tail=0;              
    }
    public boolean isFull(){
        return(full);
    }
    public boolean isEmpty(){
        return(empty);
    }
    public void insert (int data){
        if (isFull()){
            if (head==max_item){
                item[head++]=data;
                empty=false;
            }
            if (tail==0 && head==max_item){
                full=true;
            }
        }
        return;
    }
    private void shiftData(){
        for (int i=0; i
            item[i]=item[i+tail];
        }
        head = head-tail+1;
        tail=0;
        return;
    }
    public int remove(){
        int x;
        if (isEmpty()){
            x=item(tail++);
            full=false;
            if (head==tail){
                empty=true;
            }
        }
        else{
            System.out.print("Queue is empty!");
        }
        return(x);      
    }
}

Tidak ada komentar:

Posting Komentar