PROGRAM QUEUE DENGAN DOUBLE LINKED LIST

 

PROGRAM QUEUE DENGAN DOUBLE LINKED LIST

 

NAMA   : ELI NURHAYATI

NPM      : 20082010115

 

SOAL

Buatlah program menggunakan double linked list yang minimal memiliki menu untuk menjalankan operasi-operasi dasar Queue, antara lain: enqueue, dequeue, dan print. Studi kasusnya adalah antrian pembayaran listrik dengan data yang disimpan dalam node/ simpul adalah nomor urut antrian, dan kode pelanggan.

 

CLASS – CLASS

Class cObject

package queue;

public class cObject {

    private String nama, kode;

    private int nomor;

    //Pointer

    cObject next, prev;

   

    //Constructor

    cObject(int no, String kd, String nm){

        nomor=no; kode=kd; nama=nm; next=prev=null;

        System.out.println(">> Success added "+no+"/"+kd+"/"+nm+" <<");

    }

   

    //Method Getter

    public int getNomor(){

        return nomor;

    }

    public String getKode(){

        return kode;

    }

    public String getNama(){

        return nama;

    }

}

 

Class cQueue

package queue;

public class cQueue {

    cObject front, rear;

    int jumlah;

   

    //Constructor

    cQueue(){

        front=rear=null;

        jumlah=0;

        System.out.println(">> Object Queue Created <<");

    }

   

    //Method Enqueue

    public void enqueue(cObject neww){

        //Queue Kosong

        if(rear==null){

            front=rear=neww;

        }

        //Queue Sudah Ada Isinya

        else{

            rear.next=neww;

            neww.prev=rear;

            rear=neww;

        }

        jumlah++;

        System.out.println(">> Enqueue Success... <<");

    }

   

    //Method Dequeue

    public cObject dequeue(){

        //Queue Kosong

        if(rear==null){

            return null;

        }

        //Isi Queue = 1

        else if(front.next==null){

            cObject a=front;

            front=rear=null;

            System.out.println(">> Dequeue Success... <<");

            jumlah--;

            return a;

        }

        //Isi Queue >1

        else{

            cObject b=front;

            cObject c=front.next;

            b.next=null;

            front=c;

            front.prev=null;

            System.out.println(">> Dequeue Success");

            jumlah--;

            return b;

        }

    }

   

    //Method Print

    public void print(){

        System.out.println("             ANTRIAN PEMBAYARAN LISTRIK             ");

        System.out.println("=========================================================");

        System.out.println("|\tNomor \t|\tKode \t|      Nama Customer\t|");

        System.out.println("=========================================================");

        for(cObject a=front ; a!=null; a=a.next){

            System.out.println("|\t"+a.getNomor()+" \t|"+"\t"+a.getKode()+" \t|"+"\t"+a.getNama()+"    \t|");

        }

    }

}

 

Class apkQueue

package queue;

import java.util.Scanner;

public class apkQueue {

    public static void main(String[] args) {

       Scanner s = new Scanner(System.in);

       cQueue q = new cQueue();

       int pilih=0;

       do{

           System.out.println("\n================");

           System.out.println("   MENU QUEUE   ");

           System.out.println("================");

           System.out.println("1. Enqueue\n2. Dequeue\n3. Print Queue\n4. Exit");

           System.out.print("Input Pilihan Anda : ");

           pilih = s.nextInt();

           switch(pilih){

               case 1 :

                   System.out.print("Input No Urut          : ");

                   int number = s.nextInt();

                   System.out.print("Input Kode Pelanggan   : ");

                   String code = s.next();

                   System.out.print("Input Nama Pelanggan   : ");

                   String name = s.next();

                   cObject add = new cObject(number, code, name);

                   q.enqueue(add);

                   break;

              

               case 2 :

                   cObject a = q.dequeue();

                   if(a==null){

                       System.out.println(">> Oops... Queue Is Empty <<");

                   }else{

                       System.out.println("Data "+a.getNomor()+"/"+a.getKode()+"/"+a.getNama()+" Is Out");

                   }

                   break;

              

               case 3 :

                   q.print();

                   break;

               case 4:

                   System.out.println(" >>> THANK YOU <<<");

           }

       }while(pilih!=4);

      

    }

   

}

 

 

SCREENSHOT RUNNING PROGRAM


ENQUEUE





PRINT



DEQUEUE






EXIT


 

 

*Klik gambar untuk gambar yang lebih jelas

**Saran dan kritik anda sangat berguna untuk blog ini kedepannya

***Terima Kasiih

Komentar

Postingan populer dari blog ini

PROGRAM ANTRIAN TIKET PESAWAT

PROGRAM PALINDROME DENGAN STACK

LINKED LIST (TAMBAH DAN HAPUS DARI BELAKANG)