Contoh Program Queue Memakai Linked List dalam Bahasa Java

class simpulq{
    int data;
    simpulq prev, next;
    simpulq(int d){
        data=d;
    }
    int getdata(){ return data; }
}
class linkedlistq{
    simpulq head, tail;
    linkedlistq(){
        head=tail=null;
    }

    void insert(int d){
        simpulq baru = new simpulq(d);
        if (head==null){ head=tail=baru;
        }
        else{
            tail.next = baru;
            baru.prev = tail;
            tail = baru;
        }
    }
    void remove(){
        if (head.next==null){ head=null; tail=null;
        }
        else{
            head=head.next;
            head.prev=null;
        }
    }
    void display(){
        System.out.print("\nIsi Antrian: ");
        for(simpulq t=head; t!=null; t=t.next){
            System.out.print("["+t.getdata()+"]");
        }
    }
}
public class MyQueue {
    public static void main(String j[]){
        linkedlistq queue = new linkedlistq();
        queue.insert(8); queue.display();
        queue.insert(5); queue.display();
        queue.insert(4); queue.display();
        queue.insert(6); queue.display();
        queue.remove(); queue.display();
        queue.remove(); queue.display();
        queue.remove(); queue.display();
        queue.remove(); queue.display();
    }
}

1 comment: